Skip to content

Commit

Permalink
Decoder now properly handles detection of trailing commas in arrays a…
Browse files Browse the repository at this point in the history
…nd trailing data at end of input
  • Loading branch information
Jonas Tarnstrom committed Aug 14, 2012
1 parent 684a4ab commit 49aa2c8
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/ultrajsondec.c
Expand Up @@ -67,6 +67,11 @@ static JSOBJ SetError( struct DecoderState *ds, int offset, const char *message)
return NULL;
}

static void ClearError( struct DecoderState *ds)
{
ds->dec->errorOffset = 0;
ds->dec->errorStr = NULL;
}

FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric ( struct DecoderState *ds)
{
Expand Down Expand Up @@ -637,24 +642,38 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_array( struct DecoderState *ds)
{
JSOBJ itemValue;
JSOBJ newObj = ds->dec->newArray();
int len = 0;

ds->lastType = JT_INVALID;
ds->start ++;


while (1)//(*ds->start) != '\0')
{
SkipWhitespace(ds);

if ((*ds->start) == ']')
{
ds->start++;
return newObj;
}

itemValue = decode_any(ds);

if (itemValue == NULL)
{
switch (*(ds->start++))
{
case ']':
if (len > 0)
{
ds->dec->releaseObject(newObj);
return SetError(ds, -1, "Unexpected trailing comma in array");
}
// We end up here when decoding empty lists "[]"
ClearError(ds);
return newObj;

default:
ds->dec->releaseObject(newObj);
return SetError(ds, -1, "Unexpected character in found when decoding array value");
}


ds->dec->releaseObject(newObj);
return NULL;
}
Expand All @@ -675,6 +694,8 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_array( struct DecoderState *ds)
ds->dec->releaseObject(newObj);
return SetError(ds, -1, "Unexpected character in found when decoding array value");
}

len ++;
}

ds->dec->releaseObject(newObj);
Expand Down Expand Up @@ -793,7 +814,7 @@ FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_any(struct DecoderState *ds)
// White space
ds->start ++;
break;

default:
return SetError(ds, -1, "Expected object or value");
}
Expand Down Expand Up @@ -828,5 +849,12 @@ JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuf
{
dec->free(ds.escStart);
}

if (ds.start != ds.end && ret)
{
dec->releaseObject(ret);
return SetError(&ds, -1, "Trailing data");
}

return ret;
}

0 comments on commit 49aa2c8

Please sign in to comment.