In JSONParser::parse_number(), only allow a single '-' at start

- also, don't allow multiple '.' decimal points. Add unit tests to
  cover these cases.
This commit is contained in:
Glen Fraser 2017-03-14 13:01:09 +01:00
parent 561c5bc981
commit 491b95099d
2 changed files with 26 additions and 4 deletions

View File

@ -569,13 +569,15 @@ struct JSONParser {
bool isDouble = false;
bool isNegative = false;
long exp = 0;
if( offset < str.size() && str[offset] == '-' ) {
isNegative = true;
++offset;
}
for (; offset < str.size() ;) {
c = str[offset++];
if( c == '-' ) {
isNegative = true;
} else if( c >= '0' && c <= '9' ) {
if( c >= '0' && c <= '9' ) {
val += c;
} else if( c == '.' ) {
} else if( c == '.' && !isDouble ) {
val += c;
isDouble = true;
} else {

View File

@ -1,2 +1,22 @@
assert_equal(from_json("1.234"), 1.234)
assert_equal(from_json("-1.234"), -1.234)
auto caught = false;
try {
from_json("-1-5.3");
}
catch(e) {
assert_equal("JSON ERROR: Number: unexpected character '-'", e.what());
caught = true;
}
assert_equal(caught, true);
caught = false;
try {
from_json("-15.3.2");
}
catch(e) {
assert_equal("JSON ERROR: Number: unexpected character '.'", e.what());
caught = true;
}
assert_equal(caught, true);