1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-23 16:42:10 +01:00

check if malloc succeeds to avoid segmentation faults when ujson runs out of memory

This commit is contained in:
Kieran O'Mahony 2012-06-11 23:38:53 +01:00
parent 9ffbfd45b5
commit 3998aa86eb
2 changed files with 41 additions and 0 deletions

@ -420,12 +420,20 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_string ( struct DecoderState *ds)
if (ds->escHeap)
{
ds->escStart = (wchar_t *) ds->dec->realloc (ds->escStart, newSize * sizeof(wchar_t));
if (!ds->escStart)
{
return SetError(ds, -1, "Could not reserve memory block");
}
}
else
{
wchar_t *oldStart = ds->escStart;
ds->escHeap = 1;
ds->escStart = (wchar_t *) ds->dec->malloc (newSize * sizeof(wchar_t));
if (!ds->escStart)
{
return SetError(ds, -1, "Could not reserve memory block");
}
memcpy (ds->escStart, oldStart, escLen * sizeof(wchar_t));
}

@ -105,12 +105,24 @@ void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
if (enc->heap)
{
enc->start = (char *) enc->realloc (enc->start, newSize);
if (!enc->start)
{
SetError (NULL, enc, "Could not reserve memory block");
return;
}
}
else
{
char *oldStart = enc->start;
enc->heap = 1;
enc->start = (char *) enc->malloc (newSize);
if (!enc->start)
{
SetError (NULL, enc, "Could not reserve memory block");
return;
}
memcpy (enc->start, oldStart, offset);
}
enc->offset = enc->start + offset;
@ -629,6 +641,10 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
*/
Buffer_Reserve(enc, 256 + (((cbName / 4) + 1) * 12));
if (enc->errorMsg)
{
return;
}
if (name)
{
@ -780,6 +796,12 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
{
value = enc->getStringValue(obj, &tc, &szlen);
Buffer_Reserve(enc, ((szlen / 4) + 1) * 12);
if (enc->errorMsg)
{
enc->endTypeContext(obj, &tc);
return;
}
Buffer_AppendCharUnchecked (enc, '\"');
@ -836,6 +858,12 @@ char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer, size_t
{
_cbBuffer = 32768;
enc->start = (char *) enc->malloc (_cbBuffer);
if (!enc->start)
{
SetError(obj, enc, "Could not reserve memory block");
return NULL;
}
enc->heap = 1;
}
else
@ -851,6 +879,11 @@ char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer, size_t
encode (obj, enc, NULL, 0);
Buffer_Reserve(enc, 1);
if (enc->errorMsg)
{
return NULL;
}
Buffer_AppendCharUnchecked(enc, '\0');
return enc->start;