Skip to content

Commit 794aae2

Browse files
committed
Support unlimited JSON number length.
1 parent 6e70271 commit 794aae2

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

json_parser.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ static const double __power_of_10[309] = {
339339
static int __parse_json_number(const char *cursor, const char **end,
340340
double *num)
341341
{
342-
const char *integer = cursor;
343342
unsigned int digit;
343+
long long exp = 0;
344344
long long mant;
345345
int figures;
346-
int exp = 0;
346+
int minus;
347347
double n;
348348

349-
if (*cursor == '-')
349+
minus = (*cursor == '-');
350+
if (minus)
350351
cursor++;
351352

352353
mant = *cursor - '0';
@@ -383,17 +384,17 @@ static int __parse_json_number(const char *cursor, const char **end,
383384

384385
if (*cursor == '.')
385386
{
387+
const char *frac;
388+
386389
cursor++;
387390
if (!isdigit(*cursor))
388391
return -2;
389392

393+
frac = cursor;
390394
if (figures == 0)
391395
{
392396
while (*cursor == '0')
393-
{
394-
exp--;
395397
cursor++;
396-
}
397398
}
398399

399400
while (1)
@@ -403,28 +404,25 @@ static int __parse_json_number(const char *cursor, const char **end,
403404
{
404405
mant = 10 * mant + digit;
405406
figures++;
406-
exp--;
407407
cursor++;
408408
}
409409
else
410410
break;
411411
}
412412

413+
exp -= cursor - frac;
413414
while (isdigit(*cursor))
414415
cursor++;
415416
}
416417

417-
if (cursor - integer > 1000000)
418-
return -2;
419-
420418
if (*cursor == 'E' || *cursor == 'e')
421419
{
422-
int neg;
423-
int e;
420+
long long e;
421+
char sign;
424422

425423
cursor++;
426-
neg = (*cursor == '-');
427-
if (neg || *cursor == '+')
424+
sign = *cursor;
425+
if (sign == '+' || sign == '-')
428426
cursor++;
429427

430428
if (!isdigit(*cursor))
@@ -435,7 +433,7 @@ static int __parse_json_number(const char *cursor, const char **end,
435433
while (1)
436434
{
437435
digit = (unsigned int)(*cursor - '0');
438-
if (digit <= 9 && e < 2000000)
436+
if (digit <= 9 && e < 100000000000000000)
439437
{
440438
e = 10 * e + digit;
441439
cursor++;
@@ -447,7 +445,7 @@ static int __parse_json_number(const char *cursor, const char **end,
447445
while (isdigit(*cursor))
448446
cursor++;
449447

450-
if (neg)
448+
if (sign == '-')
451449
e = -e;
452450

453451
exp += e;
@@ -488,7 +486,7 @@ static int __parse_json_number(const char *cursor, const char **end,
488486
n = 0.0;
489487
}
490488

491-
if (*integer == '-')
489+
if (minus)
492490
n = -n;
493491

494492
*num = n;

0 commit comments

Comments
 (0)