Skip to content

Commit 082068f

Browse files
committed
Improve memory checking
1 parent e0c6ec6 commit 082068f

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/dpi/src/dpiStmtImpl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,16 @@ sb4 StmtImpl::defineCallback ( void *ctxp, OCIDefine *definep, ub4 iter,
747747
void **bufpp, ub4 **alenpp, ub1 *piecep,
748748
void **indpp, ub2 **rcodepp )
749749
{
750-
sb4 rc = OCI_CONTINUE;
750+
sb4 rc = OCI_CONTINUE;
751+
int cbret = 0 ;
751752

752753
DpiDefineCallbackCtx *ctx = (DpiDefineCallbackCtx *)ctxp;
753754

754-
ctx->callbackfn ( ctx->data, ctx->definePos, iter, &(ctx->prevIter), bufpp,
755-
(void **) alenpp, (void**)indpp, rcodepp );
755+
cbret = ctx->callbackfn ( ctx->data, ctx->definePos, iter, &(ctx->prevIter),
756+
bufpp, (void **) alenpp, (void**)indpp, rcodepp );
756757
*piecep = OCI_NEXT_PIECE; // always ask for next piece
757758

758-
if (!(*bufpp ))
759+
if ( cbret )
759760
{
760761
/*
761762
* In case of memory allocation failures return error to OCI, which will

src/njs/src/njsConnection.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,12 @@ void Connection::GetInBindParamsScalar(Local<Value> v8val, Bind* bind,
11851185
/* This has to be allocated after stmt is initialized */
11861186
bind->dttmarr = NULL ;
11871187
bind->extvalue = (long double *) malloc (sizeof ( long double ) );
1188+
if ( !bind->extvalue )
1189+
{
1190+
executeBaton -> error = NJSMessages::getErrorMsg (
1191+
errInsufficientMemory );
1192+
goto exitGetInBindParamsScalar;
1193+
}
11881194
bind->value = NULL;
11891195
bind->type = dpi::DpiTimestampLTZ;
11901196
*(bind->len) = 0;
@@ -1478,6 +1484,12 @@ void Connection::GetInBindParamsArray(Local<Array> va8vals, Bind *bind,
14781484
bufferSize = static_cast<size_t>(arrayElementSize *
14791485
bind->maxArraySize);
14801486
buffer = reinterpret_cast<char*>(malloc(bufferSize));
1487+
if ( !buffer )
1488+
{
1489+
executeBaton->error = NJSMessages::getErrorMsg (
1490+
errInsufficientMemory );
1491+
goto exitGetInBindParamsArray;
1492+
}
14811493
bind->value = buffer;
14821494
break;
14831495

@@ -5373,6 +5385,12 @@ void Connection::cbDynBufferAllocate ( void *ctx, bool dmlReturning,
53735385
else
53745386
{
53755387
bind->value = (void *)malloc ( (size_t)(bind->maxSize) * nRows ) ;
5388+
if ( !bind->value )
5389+
{
5390+
executeBaton->error = NJSMessages::getErrorMsg (
5391+
errInsufficientMemory );
5392+
goto exitcbDynBufferAllocate;
5393+
}
53765394
*(bind->len) = (unsigned int)bind->maxSize;
53775395
}
53785396
break;
@@ -5514,7 +5532,7 @@ int Connection::cbDynBufferGet ( void *ctx, DPI_SZ_TYPE nRows,
55145532
rcodepp (INOUT) - pointer to specify return code (NOT USED)
55155533
55165534
RETURNS
5517-
-NONE-
5535+
0 on success and -1 on memory allocation failures.
55185536
55195537
NOTE:
55205538
The callback is called repeteatedly for the same row with iter (0 based)
@@ -5523,7 +5541,7 @@ int Connection::cbDynBufferGet ( void *ctx, DPI_SZ_TYPE nRows,
55235541
is passed to the callback, new set of buffer(s) has to be provided and
55245542
initialized.
55255543
*/
5526-
void Connection::cbDynDefine ( void *octxp, unsigned long definePos,
5544+
int Connection::cbDynDefine ( void *octxp, unsigned long definePos,
55275545
unsigned long iter, unsigned long *prevIter,
55285546
void **bufpp, unsigned long **alenpp,
55295547
void **indpp, unsigned short **rcodepp )
@@ -5532,6 +5550,8 @@ void Connection::cbDynDefine ( void *octxp, unsigned long definePos,
55325550
Define *define = &(executeBaton->defines[definePos]);
55335551
unsigned long maxLen = 0;
55345552
char **buf = (char **)define->buf ;
5553+
char *tmp = NULL ; // to presever ptr for realloc
5554+
int ret = 0;
55355555

55365556
if ( *prevIter != iter )
55375557
{
@@ -5545,16 +5565,28 @@ void Connection::cbDynDefine ( void *octxp, unsigned long definePos,
55455565
maxLen = ( ( ( unsigned long ) (**alenpp ) ) + NJS_ITER_SIZE );
55465566
}
55475567

5568+
tmp = buf[iter]; // preserve the current memory address
5569+
55485570
// allocate or reallocate buffer
55495571
buf[iter] = (char *) ( ( !buf[iter] ) ?
55505572
malloc ( maxLen ) : realloc ( buf[iter], maxLen ) ) ;
5573+
if ( !buf[iter] )
5574+
{
5575+
// If realloc fails, the IN parameter requires to be freed and untouched
5576+
// restore the pointer and return error.
5577+
buf[iter] = tmp ;
5578+
ret = -1;
5579+
}
5580+
else
5581+
{
5582+
define->len[iter] = maxLen;
5583+
define->ind[iter] = 0; // default value for indicator
55515584

5552-
define->len[iter] = maxLen;
5553-
define->ind[iter] = 0; // defalt value for indicator
5554-
5555-
*bufpp = (void *) buf[iter]; // memory for this iter
5556-
*alenpp = (unsigned long *) &(define->len[iter]) ; // size for this iter
5557-
*indpp = (void *) &(define->ind[iter]); // indicator
5585+
*bufpp = (void *) buf[iter]; // memory for this iter
5586+
*alenpp = (unsigned long *) &(define->len[iter]) ; // size for this iter
5587+
*indpp = (void *) &(define->ind[iter]); // indicator
5588+
}
5589+
return ret ;
55585590
}
55595591

55605592

src/njs/src/njsConnection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class Connection: public Nan::ObjectWrap
678678
void **bufpp, void **alenpp, void **indpp,
679679
unsigned short **rcode, unsigned char *piecep );
680680

681-
static void cbDynDefine ( void *octxp, unsigned long definePos,
681+
static int cbDynDefine ( void *octxp, unsigned long definePos,
682682
unsigned long iter, unsigned long *prevIter,
683683
void **bufpp, unsigned long **alenpp,
684684
void **indpp, unsigned short **rcodepp );

0 commit comments

Comments
 (0)