Skip to content

Commit

Permalink
fix: for memory leak issue #576
Browse files Browse the repository at this point in the history
  • Loading branch information
bimalkjha committed Dec 9, 2019
1 parent d2bdca7 commit 585b3b0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
14 changes: 8 additions & 6 deletions src/odbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ Column* ODBC::GetColumns(SQLHSTMT hStmt, short* colCount)

void ODBC::FreeColumns(Column* columns, short* colCount)
{
DEBUG_PRINTF("ODBC::GetColumns - Entry\n");
for(int i = 0; i < *colCount; i++) {
delete [] columns[i].name;
delete [] columns[i].type_name;
Expand All @@ -400,6 +401,7 @@ void ODBC::FreeColumns(Column* columns, short* colCount)
delete [] columns;
columns = NULL;
*colCount = 0;
DEBUG_PRINTF("ODBC::GetColumns - Exit\n");
}

/*
Expand Down Expand Up @@ -590,7 +592,7 @@ Local<Value> ODBC::GetColumnValue( SQLHSTMT hStmt, Column column,
int newbufflen = bufferLength + terCharLen;
int secondGetData = 0;
len = 0;
if (!buffer) {
if (buffer == NULL) {
buffer = (uint16_t*)malloc(newbufflen);
if(buffer == NULL)
{
Expand Down Expand Up @@ -628,7 +630,7 @@ Local<Value> ODBC::GetColumnValue( SQLHSTMT hStmt, Column column,
else
{
memcpy(tmp_out_ptr, (char *) buffer, bufferLength);
free((uint8_t *)buffer);
free((uint16_t *)buffer);
buffer = tmp_out_ptr;
len = 0;
ret = SQLGetData( hStmt,
Expand All @@ -646,6 +648,7 @@ Local<Value> ODBC::GetColumnValue( SQLHSTMT hStmt, Column column,
}

if((int)len == SQL_NULL_DATA) {
FREE(buffer);
return scope.Escape(Nan::Null());
}
// In case of secondGetData, we already have result from first getdata
Expand All @@ -661,15 +664,14 @@ Local<Value> ODBC::GetColumnValue( SQLHSTMT hStmt, Column column,
str = Nan::New((char *) buffer).ToLocalChecked();
#endif
}
if(tmp_out_ptr) {
free(tmp_out_ptr); // tmp_out_ptr is pointing to buffer too.
buffer = NULL;
}
FREE(buffer);

//return scope.Escape(Nan::CopyBuffer((char*)buffer, 39767).ToLocalChecked());
}
else
{
DEBUG_PRINTF("ODBC::GetColumnValue - An error has occurred, ret = %i\n", ret);
FREE(buffer);
//an error has occured
//possible values for ret are SQL_ERROR (-1) and SQL_INVALID_HANDLE (-2)
//If we have an invalid handle, then stuff is way bad and we should abort
Expand Down
16 changes: 16 additions & 0 deletions src/odbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,19 @@ struct query_request {
static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete))

#endif

// Macro to fee the memory allocated by GETCPPSTR
#ifdef UNICODE
#define FREE(var) \
if (var != NULL) { \
free((uint16_t *)var); \
var = NULL; \
}
#else
#define FREE(var) \
if (var != NULL) { \
free((char *)var); \
var = NULL; \
}
#endif

38 changes: 23 additions & 15 deletions src/odbc_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ void ODBCConnection::UV_AfterOpen(uv_work_t* req, int status)

delete data->cb;

free(data->connection);
free(data);
free(req);
FREE(data->connection);
FREE(data);
FREE(req);
DEBUG_PRINTF("ODBCConnection::UV_AfterOpen - Exit\n");
}

Expand Down Expand Up @@ -434,7 +434,7 @@ NAN_METHOD(ODBCConnection::OpenSync)
#endif*/
}

free(connectionString);
FREE(connectionString);
DEBUG_PRINTF("ODBCConnection::OpenSync - Exit\n");

if (err) {
Expand Down Expand Up @@ -677,7 +677,7 @@ NAN_METHOD(ODBCConnection::CreateDbSync)
}
}

free(databaseNameString);
FREE(databaseNameString);
if ( !( info[1]->IsNull() ) ) free(codeSetString);
if ( !( info[1]->IsNull() ) ) free(modeString);
DEBUG_PRINTF("ODBCConnection::CreateDbSync - Exit\n");
Expand Down Expand Up @@ -769,7 +769,7 @@ NAN_METHOD(ODBCConnection::DropDbSync)
}
}

free(databaseNameString);
FREE(databaseNameString);
DEBUG_PRINTF("ODBCConnection::DropDbSync - Exit\n");

if (err) {
Expand Down Expand Up @@ -1168,14 +1168,14 @@ void ODBCConnection::UV_AfterQuery(uv_work_t* req, int status)
FREE_PARAMS( data->params, data->paramCount ) ;
}

free(data->sql);
free(data->catalog);
free(data->schema);
free(data->table);
free(data->type);
free(data->column);
free(data);
free(req);
FREE(data->sql);
FREE(data->catalog);
FREE(data->schema);
FREE(data->table);
FREE(data->type);
FREE(data->column);
FREE(data);
FREE(req);

//scope.Close(Undefined());
DEBUG_PRINTF("ODBCConnection::UV_AfterQuery - Exit\n");
Expand Down Expand Up @@ -1318,7 +1318,7 @@ NAN_METHOD(ODBCConnection::QuerySync)
FREE_PARAMS( params, paramCount ) ;
}

free((char*)sql);
FREE(sql);

//check to see if there was an error during execution
if (ret != SQL_SUCCESS) {
Expand Down Expand Up @@ -1458,6 +1458,10 @@ void ODBCConnection::UV_Tables(uv_work_t* req)

// this will be checked later in UV_AfterQuery
data->result = ret;
FREE(data->catalog);
FREE(data->schema);
FREE(data->table);
FREE(data->type);
DEBUG_PRINTF("ODBCConnection::UV_Tables - Exit\n");
}

Expand Down Expand Up @@ -1546,6 +1550,10 @@ void ODBCConnection::UV_Columns(uv_work_t* req)

// this will be checked later in UV_AfterQuery
data->result = ret;
FREE(data->catalog);
FREE(data->schema);
FREE(data->table);
FREE(data->column);
DEBUG_PRINTF("ODBCConnection::UV_Columns - Exit\n");
}

Expand Down
10 changes: 8 additions & 2 deletions src/odbc_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ void ODBCResult::Free()
m_canFreeHandle = 0;
}

if (bufferLength > 0) {
if (bufferLength != 0) {
bufferLength = 0;
free(buffer);
}
if (buffer != NULL) {
free((uint16_t *)buffer);
buffer = NULL;
}
}
Expand Down Expand Up @@ -291,6 +293,7 @@ void ODBCResult::UV_AfterFetch(uv_work_t* work_req, int status)
}
else {
ODBC::FreeColumns(data->objResult->columns, &data->objResult->colCount);
FREE(data->objResult->buffer);

Local<Value> info[2];

Expand Down Expand Up @@ -399,6 +402,7 @@ NAN_METHOD(ODBCResult::FetchSync)
}
else {
ODBC::FreeColumns(objResult->columns, &objResult->colCount);
FREE(objResult->buffer);

//if there was an error, pass that as arg[0] otherwise Null
if (error) {
Expand Down Expand Up @@ -593,6 +597,7 @@ void ODBCResult::UV_AfterFetchAll(uv_work_t* work_req, int status)

data->cb->Call(3, info);
ODBC::FreeColumns(self->columns, &self->colCount);
FREE(self->buffer);
delete data->cb;
data->rows.Reset();
data->objError.Reset();
Expand Down Expand Up @@ -695,6 +700,7 @@ NAN_METHOD(ODBCResult::FetchAllSync)
}
}
ODBC::FreeColumns(self->columns, &self->colCount);
FREE(self->buffer);

//throw the error object if there were errors
if (errorCount > 0) {
Expand Down
6 changes: 4 additions & 2 deletions src/odbc_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ void ODBCStatement::Free()
m_hSTMT = (SQLHSTMT)NULL;
}

if (bufferLength > 0) {
if(buffer) free(buffer);
if(buffer != NULL) {
free((uint16_t *)buffer);
buffer = NULL;
}
if (bufferLength > 0) {
bufferLength = 0;
}
DEBUG_PRINTF("ODBCStatement::Free - Exit\n");
Expand Down

0 comments on commit 585b3b0

Please sign in to comment.