Fixed reallocation of buffer in bson

This commit is contained in:
Steen Lund 2011-07-11 12:52:46 +02:00
Родитель a9f6957d30
Коммит eff88cc6cb
1 изменённых файлов: 10 добавлений и 11 удалений

Просмотреть файл

@ -459,7 +459,7 @@ void bson_append64(bson_buffer * b, const void * data){
int bson_ensure_space( bson_buffer * b, const int bytesNeeded ){
int pos = b->cur - b->buf;
char * orig = b->buf;
int new_size;
unsigned int new_size;
if (b->finished) {
b->err = BSON_OBJECT_FINISHED;
@ -469,17 +469,16 @@ int bson_ensure_space( bson_buffer * b, const int bytesNeeded ){
if (pos + bytesNeeded <= b->bufSize)
return BSON_OK;
if( b->bufSize + bytesNeeded > INT_MAX )
{
b->err = BSON_SIZE_OVERFLOW;
return BSON_ERROR;
}
new_size = 1.5 * (b->bufSize + bytesNeeded);
if( new_size < b->bufSize ) {
if( ( b->bufSize + bytesNeeded ) < INT_MAX )
new_size = INT_MAX;
else {
b->err = BSON_SIZE_OVERFLOW;
return BSON_ERROR;
}
}
if( (new_size > INT_MAX) && (b->bufSize + bytesNeeded < INT_MAX) )
new_size = INT_MAX;
b->buf = realloc(b->buf, new_size);
if (!b->buf)
bson_fatal_msg(!!b->buf, "realloc() failed");