Skip to content

Commit 52368d7

Browse files
committed
Remove non portable memory allocations and added check to make sure maxRows is > 0 for non-ResultSet queries
1 parent ab40f24 commit 52368d7

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## node-oracledb v1.2.0 (DD Mon YYYY)
44

5+
- Remove non-portable memory allocation for queries that return NULL.
6+
7+
- Added check to make sure maxRows is greater than zero for non-ResultSet queries.
8+
59
- Fixed AIX-specific REF CURSOR related failures.
610

711
- Optimized CLOB memory allocation to account for different database-to-client character set expansions.

src/njs/src/njsConnection.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,13 @@ void Connection::DoDefines ( eBaton* executeBaton, const dpi::MetaData* meta,
13861386
Define *defines = executeBaton->defines = new Define[numCols];
13871387
int csratio = executeBaton->dpiconn->getByteExpansionRatio ();
13881388

1389+
// Check for maxRows must be greater than zero in case of non-resultSet
1390+
if ( executeBaton->maxRows == 0 )
1391+
{
1392+
executeBaton->error = NJSMessages::getErrorMsg ( errInvalidmaxRows );
1393+
return;
1394+
}
1395+
13891396
for (unsigned int col = 0; col < numCols; col++)
13901397
{
13911398
switch(meta[col].dbType)
@@ -1431,26 +1438,36 @@ void Connection::DoDefines ( eBaton* executeBaton, const dpi::MetaData* meta,
14311438
* size expansion when data is converted from the DB character set
14321439
* to AL32UTF8
14331440
*/
1434-
1435-
defines[col].maxSize = (meta[col].dbSize) * csratio;
14361441

1437-
if ( NJS_SIZE_T_OVERFLOW ( defines[col].maxSize,
1438-
executeBaton->maxRows ) )
1439-
{
1440-
executeBaton->error = NJSMessages::getErrorMsg( errResultsTooLarge );
1441-
return;
1442-
}
1443-
else
1442+
if ( meta[col].dbSize != 0 )
14441443
{
1445-
defines[col].buf = (char *)malloc( (size_t)defines[col].maxSize*
1446-
executeBaton->maxRows );
1447-
if( !defines[col].buf )
1444+
/* dbSize will be zero in case of "select null from dual" and
1445+
* malloc(0) behaves diffrently on different platforms, so avoiding it
1446+
*/
1447+
defines[col].maxSize = (meta[col].dbSize) * csratio;
1448+
1449+
if ( NJS_SIZE_T_OVERFLOW ( defines[col].maxSize,
1450+
executeBaton->maxRows ) )
14481451
{
1449-
executeBaton->error = NJSMessages::getErrorMsg(
1450-
errInsufficientMemory );
1452+
executeBaton->error = NJSMessages::getErrorMsg(
1453+
errResultsTooLarge );
14511454
return;
14521455
}
1456+
else
1457+
{
1458+
defines[col].buf = (char *)malloc( (size_t)defines[col].maxSize*
1459+
executeBaton->maxRows );
1460+
if( !defines[col].buf )
1461+
{
1462+
executeBaton->error = NJSMessages::getErrorMsg(
1463+
errInsufficientMemory );
1464+
return;
1465+
}
1466+
}
14531467
}
1468+
/* The null scenario will have indicator as -1, so memory allocation
1469+
* not required.
1470+
*/
14541471
break;
14551472
case dpi::DpiDate :
14561473
case dpi::DpiTimestamp:

src/njs/src/njsMessages.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static const char *errMsg[] =
6060
"NJS-023: concurrent operations on LOB are not allowed",
6161
"NJS-024: memory allocation failed",
6262
"NJS-025: overflow when calculating results area size",
63+
"NJS-026: maxRows must be greater than zero",
6364
};
6465

6566
string NJSMessages::getErrorMsg ( NJSErrorType err, ... )

src/njs/src/njsMessages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef enum
5959
errBusyLob,
6060
errInsufficientMemory,
6161
errResultsTooLarge,
62+
errInvalidmaxRows,
6263

6364
// New ones should be added here
6465

test/connection.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ describe('1. connection.js', function(){
254254
"SELECT * FROM oracledb_employees",
255255
{}, { maxRows: 0 },
256256
function(err, result){
257-
should.not.exist(err);
258-
should.exist(result);
259-
260-
(result.rows).should.have.length(0);
257+
should.exist(err);
258+
err.message.should.startWith('NJS-026:');
261259
done();
262260
}
263261
);

0 commit comments

Comments
 (0)