Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in yajl_tree_parse with invalid JSON in v2.0.4 and older #102

Open
andrew78 opened this issue Apr 18, 2013 · 1 comment
Open

Comments

@andrew78
Copy link

This patch will fix it.

Index: yajl_tree.c
===================================================================
--- yajl_tree.c (revision xxx)
+++ yajl_tree.c (revision xxx)
@@ -144,6 +144,9 @@
 
     v = stack->value;
 
+    if (stack->key != NULL)
+      free(stack->key);
+
     free (stack);
 
     return (v);
@@ -444,6 +447,11 @@
              snprintf(error_buffer, error_buffer_size, "%s", internal_err_str);
              YA_FREE(&(handle->alloc), internal_err_str);
         }
+        while(ctx.stack != NULL)
+        {
+          yajl_val v = context_pop(&ctx);
+          yajl_tree_free(v);
+        }
         yajl_free (handle);
         return NULL;
     }

@DhirajS
Copy link

DhirajS commented Jun 12, 2015

In some corner cases when the stack has been emptied due to an error, the ctx.root can still be leaking memory. I have found this patch to fix all the leaks we have encountered. Love to hear more thoughts on this.

diff --git a/yajl_tree.c b/yajl_tree.c
index e63240c..ed09349 100644
--- a/yajl_tree.c
+++ b/yajl_tree.c
@@ -143,6 +143,7 @@ static yajl_val context_pop(context_t *ctx)
     ctx->stack = stack->next;

     v = stack->value;
+    free(stack->key);

     free (stack);

@@ -444,6 +445,12 @@ yajl_val yajl_tree_parse (const char *input,
              snprintf(error_buffer, error_buffer_size, "%s", internal_err_str);
              YA_FREE(&(handle->alloc), internal_err_str);
         }
+        while(ctx.stack != NULL)
+        {
+          yajl_val v = context_pop(&ctx);
+          yajl_tree_free(v);
+        }
+        yajl_tree_free(ctx.root);
         yajl_free (handle);
         return NULL;
     }

robohack added a commit to robohack/yajl that referenced this issue Jul 17, 2023
- use wrapped malloc() et al wrappers consistently
- update example/parse_config.c to do memory leak detection
- add a regression test using example/parse_config

Several issues in lloyd/yajl complained about this leak, and comments in
lloyd/yajl#102 showed a mostly correct fix though none of these issues
mentioned or actually fixed the directly related error reporting
problem.

Fixes lloyd/yajl#102, fixes lloyd/yajl#113, fixes lloyd/yajl#168, fixes
lloyd/yajl#191, fixes lloyd/yajl#223, fixes lloyd/yajl#250.  Also fixes
lloy/yajl#185.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants