Skip to content

Commit

Permalink
Additional assertion checks wrt/ issue #9. Issue #9 looks like it cou…
Browse files Browse the repository at this point in the history
…ld be a stack smash? The assertion checks make sure we're only copying legal values and not writing past a buffer by accident.
  • Loading branch information
johnezang committed Mar 17, 2011
1 parent 524b9d7 commit 7054853
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion JSONKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -2557,13 +2557,15 @@ static int jk_encode_write(JKEncodeState *encodeState, JKEncodeCache *cacheSlot,
}

static int jk_encode_write1(JKEncodeState *encodeState, JKEncodeCache *cacheSlot, size_t startingAtIndex, id object, const char *format) {
NSCParameterAssert((encodeState != NULL) && (encodeState->atIndex < encodeState->stringBuffer.bytes.length) && (startingAtIndex <= encodeState->atIndex));
if((encodeState->atIndex + 4UL) < encodeState->stringBuffer.bytes.length) { encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = format[0]; }
else { if(JK_EXPECT_F(jk_encode_write(encodeState, cacheSlot, startingAtIndex, object, format))) { return(1); } }
jk_encode_updateCache(encodeState, cacheSlot, startingAtIndex, object);
return(0);
}

static int jk_encode_writen(JKEncodeState *encodeState, JKEncodeCache *cacheSlot, size_t startingAtIndex, id object, const char *format, size_t length) {
NSCParameterAssert((encodeState != NULL) && (encodeState->atIndex < encodeState->stringBuffer.bytes.length) && (startingAtIndex <= encodeState->atIndex));
if(JK_EXPECT_F((encodeState->stringBuffer.bytes.length - encodeState->atIndex) < (length + 4UL))) { if(jk_managedBuffer_resize(&encodeState->stringBuffer, encodeState->atIndex + 4096UL + length) == NULL) { jk_encode_error(encodeState, @"Unable to resize temporary buffer."); return(1); } }
memcpy(encodeState->stringBuffer.bytes.ptr + encodeState->atIndex, format, length);
encodeState->atIndex += length;
Expand All @@ -2576,8 +2578,9 @@ JK_STATIC_INLINE JKHash jk_encode_object_hash(void *objectPtr) {
}

JK_STATIC_INLINE void jk_encode_updateCache(JKEncodeState *encodeState, JKEncodeCache *cacheSlot, size_t startingAtIndex, id object) {
NSCParameterAssert((encodeState != NULL) && ((cacheSlot == NULL) ? 1 : object != NULL));
NSCParameterAssert(encodeState != NULL);
if(JK_EXPECT_T(cacheSlot != NULL)) {
NSCParameterAssert((object != NULL) && (startingAtIndex <= encodeState->atIndex));
cacheSlot->object = object;
cacheSlot->offset = startingAtIndex;
cacheSlot->length = (size_t)(encodeState->atIndex - startingAtIndex);
Expand All @@ -2596,7 +2599,17 @@ static int jk_encode_add_atom_to_buffer(JKEncodeState *encodeState, void *object
JKEncodeCache *cacheSlot = &encodeState->cache[objectHash % JK_ENCODE_CACHE_SLOTS];

if(JK_EXPECT_T(cacheSlot->object == object)) {
NSCParameterAssert((cacheSlot->offset < encodeState->atIndex) && ((cacheSlot->offset + cacheSlot->length) < encodeState->atIndex) &&
(cacheSlot->offset < encodeState->stringBuffer.bytes.length) && ((cacheSlot->offset + cacheSlot->length) < encodeState->stringBuffer.bytes.length) &&
((encodeState->stringBuffer.bytes.ptr + encodeState->atIndex) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + cacheSlot->offset) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + cacheSlot->offset + cacheSlot->length) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)));
if(JK_EXPECT_F(((encodeState->atIndex + cacheSlot->length + 256UL) > encodeState->stringBuffer.bytes.length)) && JK_EXPECT_F((jk_managedBuffer_resize(&encodeState->stringBuffer, encodeState->atIndex + cacheSlot->length + 1024UL) == NULL))) { jk_encode_error(encodeState, @"Unable to resize temporary buffer."); return(1); }
NSCParameterAssert(((encodeState->stringBuffer.bytes.ptr + encodeState->atIndex) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + encodeState->atIndex + cacheSlot->length) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + cacheSlot->offset) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + cacheSlot->offset + cacheSlot->length) < (encodeState->stringBuffer.bytes.ptr + encodeState->stringBuffer.bytes.length)) &&
((encodeState->stringBuffer.bytes.ptr + cacheSlot->offset + cacheSlot->length) < (encodeState->stringBuffer.bytes.ptr + encodeState->atIndex)));
memcpy(encodeState->stringBuffer.bytes.ptr + encodeState->atIndex, encodeState->stringBuffer.bytes.ptr + cacheSlot->offset, cacheSlot->length);
encodeState->atIndex += cacheSlot->length;
return(0);
Expand Down

0 comments on commit 7054853

Please sign in to comment.