google-code-export / v8cgi

Automatically exported from code.google.com/p/v8cgi
BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

pgsql.cc memory allocation problems #77

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes crashes, valgrind shows memory leaks and access violation.
Here is a patch solving the problem:

diff v8cgi-original/src/lib/pgsql/pgsql.cc v8cgi/src/lib/pgsql/pgsql.cc
333c333
<     char ** params = (char **)malloc(nparams);

---
>     char ** params = (char **)malloc(nparams * sizeof(char*));
338d337
<   params[i] = new char[n + 1];
341a341,345
> 
>     for(int i = 0; i < nparams; i++)
>          free(params[i]);
>     free(params);
> 

Original issue reported on code.google.com by vahv...@gmail.com on 9 Dec 2010 at 3:30

GoogleCodeExporter commented 9 years ago
Even bigger patch, concerning not only memory leaks but null and undefined 
behavior. Should have added handling numbers also, though.

333,334c333
<     char ** params = (char **)malloc(nparams);
<     size_t n = 0;
---
>     char ** params = (char **)malloc(nparams * sizeof(char*));
336,339c335,343
<   n = tarray->Get(JS_INT(i))->ToString()->Utf8Length();
<   v8::String::Utf8Value tval(tarray->Get(JS_INT(i))->ToString());
<   params[i] = new char[n + 1];
<   params[i] = strdup(*tval);
---
>   v8::Local<v8::Value> val=tarray->Get(JS_INT(i));
>   if (val->IsUndefined()) {
>       params[i]=NULL;
>   } else if (val->IsNull()) {
>       params[i]=NULL;
>   } else {
>       v8::String::Utf8Value tval(val->ToString());
>       if (tval.length()) params[i] = strdup(*tval); else params[i]=NULL;
>   }
341a346,350
> 
>     for(int i = 0; i < nparams; i++)
>          if (params[i]) free(params[i]);
>     free(params);
> 

Original comment by vahv...@gmail.com on 9 Dec 2010 at 3:54

GoogleCodeExporter commented 9 years ago
Hi,

thanks a lot for patch - can you please add your diff as an attachment?

Original comment by ondrej.zara on 9 Dec 2010 at 6:32

GoogleCodeExporter commented 9 years ago
Fixed in r903.

Original comment by ondrej.zara on 24 Jan 2011 at 10:58