Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes incorrect lower bound for integers (#130) #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/yajl_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <assert.h>
#include <math.h>

#define MAX_VALUE_TO_MULTIPLY ((LLONG_MAX / 10) + (LLONG_MAX % 10))
#define MAX_VALUE_TO_MULTIPLY (LLONG_MIN / 10)

/* same semantics as strtol */
long long
Expand All @@ -42,23 +42,23 @@ yajl_parse_integer(const unsigned char *number, unsigned int length)
if (*pos == '+') { pos++; }

while (pos < number + length) {
if ( ret > MAX_VALUE_TO_MULTIPLY ) {
if ( ret < MAX_VALUE_TO_MULTIPLY ) {
errno = ERANGE;
return sign == 1 ? LLONG_MAX : LLONG_MIN;
}
ret *= 10;
if (LLONG_MAX - ret < (*pos - '0')) {
if ((sign == 1 ? -LLONG_MAX : LLONG_MIN) - ret > -(*pos - '0')) {
errno = ERANGE;
return sign == 1 ? LLONG_MAX : LLONG_MIN;
}
if (*pos < '0' || *pos > '9') {
errno = ERANGE;
return sign == 1 ? LLONG_MAX : LLONG_MIN;
}
ret += (*pos++ - '0');
ret -= (*pos++ - '0');
}

return sign * ret;
return -sign * ret;
}

unsigned char *
Expand Down
2 changes: 1 addition & 1 deletion test/parsing/cases/bignums.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ 9223372036854775807, -9223372036854775807 ]
[ 9223372036854775807, -9223372036854775808 ]
2 changes: 1 addition & 1 deletion test/parsing/cases/bignums.json.gold
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
array open '['
integer: 9223372036854775807
integer: -9223372036854775807
integer: -9223372036854775808
array close ']'
memory leaks: 0
2 changes: 1 addition & 1 deletion test/parsing/cases/low_overflow.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-9223372036854775808
-9223372036854775809
1 change: 1 addition & 0 deletions test/parsing/cases/low_overflow_regression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-92233720368547758080
2 changes: 2 additions & 0 deletions test/parsing/cases/low_overflow_regression.json.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parse error: integer overflow
memory leaks: 0