Wildfire: fix memory leak.
[citadel.git] / libcitadel / lib / wildfire.c
index 9fe53faa2ea8def2a371c29c81fa1b938f62ce6e..1958f5920202085358cf509045f8071452f067f4 100644 (file)
@@ -1,7 +1,3 @@
-/*
- * $Id: wildfire.c 6962 2009-01-18 19:33:45Z dothebart $
- */
-
 /*@{*/
 
 #include "sysdep.h"
 #endif
 
 #include "libcitadel.h"
-
+#include "libcitadellocal.h"
 
 
 ConstStr WF_MsgStrs[] = {
+       {HKEY("LOG")},
        {HKEY("INFO")},
        {HKEY("WARN")},
        {HKEY("ERROR")},
-       {HKEY("LOG")},
        {HKEY("TRACE")},
        {HKEY("EXCEPTION")}
 };
@@ -161,9 +157,13 @@ static int addr2lineBacktrace(StrBuf *Function,
        pche = strchr(pch, ':');
        FlushStrBuf(FileName);
        StrBufAppendBufPlain(FileName, pch, pche - pch, 0);
-       pche++;
-       *FunctionLine = atoi(pche);
-
+       if (pche != NULL)
+       {
+               pche++;
+               *FunctionLine = atoi(pche);
+       }
+       else 
+               *FunctionLine = 0;
        return 1;
 }
 
@@ -194,6 +194,14 @@ static int ParseBacktrace(char *Line,
 long BaseFrames = 0;
 StrBuf *FullBinaryName = NULL;
 
+void WildFireShutdown(void)
+{
+       close(addr2line_write_pipe[0]);
+       close(addr2line_read_pipe[0]);
+
+       FreeStrBuf(&FullBinaryName);
+}
+
 void WildFireInitBacktrace(const char *argvNull, int AddBaseFrameSkip)
 {
 
@@ -218,7 +226,7 @@ void WildFireInitBacktrace(const char *argvNull, int AddBaseFrameSkip)
        BaseFrames --;
        BaseFrames += AddBaseFrameSkip;
        strings = backtrace_symbols(stack_frames, size);
-       for (i = 0; i < size; i++) {
+       for (i = 1; i < size; i++) {
                if (strings != NULL){
                        ParseBacktrace(strings[i], Function, 
                                       FileName, 
@@ -258,9 +266,9 @@ void WildFireInitBacktrace(const char *argvNull, int AddBaseFrameSkip)
 }
 
 
-JsonValue *WildFireException(StrBuf *Message,
-                            const char *Filename, long FileLen,
+JsonValue *WildFireException(const char *Filename, long FileLen,
                             long LineNo,
+                            StrBuf *Message,
                             int StackOffset)
 {
        JsonValue *ExcClass;
@@ -365,6 +373,8 @@ void WildFireSerializePayload(StrBuf *JsonBuffer, StrBuf *OutBuf, int *MsgCount,
        const char *Cat;
        StrBuf *Header;
 
+       if (OutBuf == NULL)
+               Header = NewStrBuf();
        if (*MsgCount == 0) {
                if (OutBuf != NULL) {
                        StrBufAppendBufPlain(OutBuf, 
@@ -384,7 +394,6 @@ void WildFireSerializePayload(StrBuf *JsonBuffer, StrBuf *OutBuf, int *MsgCount,
                                                     "http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1\r\n"), 0);
                }
                else {
-                       Header = NewStrBuf();
                        AddHdr("X-Wf-Protocol-1", 
                               "http://meta.wildfirehq.org/Protocol/JsonStream/0.2");
                        AddHdr("X-Wf-1-Plugin-1",
@@ -437,6 +446,9 @@ void WildFireSerializePayload(StrBuf *JsonBuffer, StrBuf *OutBuf, int *MsgCount,
        if (OutBuf == NULL) {
                FreeStrBuf(&Header);
        }
+       FreeStrBuf(&N);
+       FreeStrBuf(&Buf);
+       FreeStrBuf(&HeaderName);
 }