Skip to content

Commit

Permalink
[3e7eca8c8c] Prevent overflow in size values passed to allocators.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgp@users.sourceforge.net committed Jul 29, 2015
2 parents 74f9398 + 0386626 commit 29f1233
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions generic/tclLiteral.c
Expand Up @@ -717,16 +717,22 @@ ExpandLocalLiteralArray(
LiteralEntry *currArrayPtr = envPtr->literalArrayPtr;
LiteralEntry *newArrayPtr;
int i;
unsigned int newSize = (currBytes <= UINT_MAX / 2) ? 2*currBytes : UINT_MAX;

if (currBytes == newSize) {
Tcl_Panic("max size of Tcl literal array (%d literals) exceeded",
currElems);
}

if (envPtr->mallocedLiteralArray) {
newArrayPtr = ckrealloc(currArrayPtr, 2 * currBytes);
newArrayPtr = ckrealloc(currArrayPtr, newSize);
} else {
/*
* envPtr->literalArrayPtr isn't a ckalloc'd pointer, so we must
* code a ckrealloc equivalent for ourselves.
*/

newArrayPtr = ckalloc(2 * currBytes);
newArrayPtr = ckalloc(newSize);
memcpy(newArrayPtr, currArrayPtr, currBytes);
envPtr->mallocedLiteralArray = 1;
}
Expand All @@ -751,7 +757,7 @@ ExpandLocalLiteralArray(
}

envPtr->literalArrayPtr = newArrayPtr;
envPtr->literalArrayEnd = (2 * currElems);
envPtr->literalArrayEnd = newSize / sizeof(LiteralEntry);
}

/*
Expand Down Expand Up @@ -942,6 +948,16 @@ RebuildLiteralTable(
* constants for new array size.
*/

if (oldSize > UINT_MAX/(4 * sizeof(LiteralEntry *))) {
/*
* Memory allocator limitations will not let us create the
* next larger table size. Best option is to limp along
* with what we have.
*/

return;
}

tablePtr->numBuckets *= 4;
tablePtr->buckets = ckalloc(tablePtr->numBuckets * sizeof(LiteralEntry*));
for (count=tablePtr->numBuckets, newChainPtr=tablePtr->buckets;
Expand Down

0 comments on commit 29f1233

Please sign in to comment.