waveto / node-compress

A streaming compression / gzip library for node.js
MIT License
125 stars 49 forks source link

gzip.deflate memory leak #14

Open dmich opened 12 years ago

dmich commented 12 years ago

we see a memory leaks in gzip.deflate. Are there any known issues? Also question (why you never delete the temporary buffer allocated at line 154?) 154 char* buf = new char[len]; 155 ssize_t written = DecodeWrite(buf, len, args[0], enc); 156 assert(written == len); 157
158 char* out; 159 int out_size; 160 int r = gzip->GzipDeflate(buf, len, &out, &out_size); 161
162 if (out_size==0) { 163 return scope.Close(String::New("")); 164 } 165
166 Local outString = Encode(out, out_size, BINARY); 167 free(out); 168 return scope.Close(outString);

dmich commented 12 years ago

fix:

char* buf = new char[len];

if(buf == NULL){
  Local<Value> exception = Exception::TypeError(String::New("Out of memory"));
  return ThrowException(exception);
}

ssize_t written = DecodeWrite(buf, len, args[0], enc);
assert(written == len);

char* out;
int out_size;
int r = gzip->GzipDeflate(buf, len, &out, &out_size);

if (out_size==0) {
    if(out != NULL)
      free(out);
    if(buf != NULL)
      delete buf;
  return scope.Close(String::New(""));
}

Local<Value> outString = Encode(out, out_size, BINARY);

if(out != NULL)
  free(out);
if(buf != NULL)
  delete buf;

return scope.Close(outString);

}

dmich commented 12 years ago

same for GunzipInflate(const Arguments& args)