aboutsummaryrefslogtreecommitdiff
path: root/dev-libs/libstrophe/files/libstrophe-fix-memory-leak-in-logging.patch
blob: 4537d8faba8b99068f395bef1cf398510aeced75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
diff --git a/src/ctx.c b/src/ctx.c
index 48e87ad..5a24687 100644
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -249,30 +249,38 @@ void xmpp_log(const xmpp_ctx_t * const ctx,
 {
     int oldret, ret;
     char smbuf[1024];
-    char *buf;
-    va_list copy;
+    char *bigbuf = NULL;
+    va_list ap_copy;
 
-    buf = smbuf;
-    va_copy(copy, ap);
-    ret = xmpp_vsnprintf(buf, 1023, fmt, ap);
+    va_copy(ap_copy, ap);
+    ret = xmpp_vsnprintf(smbuf, 1023, fmt, ap_copy);
+    va_end(ap_copy);
     if (ret > 1023) {
-	va_copy(ap, copy);
-	buf = (char *)xmpp_alloc(ctx, ret + 1);
-	if (!buf) {
-	    buf = NULL;
-	    xmpp_error(ctx, "log", "Failed allocating memory for log message.");
-	    return;
-	}
-	oldret = ret;
-	ret = xmpp_vsnprintf(buf, ret + 1, fmt, ap);
-	if (ret > oldret) {
-	    xmpp_error(ctx, "log", "Unexpected error");
-	    return;
-	}
+        bigbuf = (char *)xmpp_alloc(ctx, ret + 1);
+        if (!bigbuf) {
+            bigbuf = NULL;
+            xmpp_error(ctx, "log", "Failed allocating memory for log message.");
+            return;
+        }
+        oldret = ret;
+		va_copy(ap_copy, ap);
+        ret = xmpp_vsnprintf(bigbuf, ret + 1, fmt, ap_copy);
+    	va_end(ap_copy);
+
+        if (ret > oldret) {
+            xmpp_error(ctx, "log", "Unexpected error");
+            return;
+        }
+
+        if (ctx->log->handler) {
+            ctx->log->handler(ctx->log->userdata, level, area, bigbuf);
+        }
+        xmpp_free(ctx, bigbuf);
+    } else {
+        if (ctx->log->handler) {
+            ctx->log->handler(ctx->log->userdata, level, area, smbuf);
+        }
     }
-
-    if (ctx->log->handler)
-        ctx->log->handler(ctx->log->userdata, level, area, buf);
 }
 
 /** Write to the log at the ERROR level.