JSON numbers should take the following form: * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)? * * json_next_number_token() uses strtod() which allows other forms: * - numbers starting with '+' * - NaN, -NaN, infinity, -infinity * - hexadecimal numbers * - numbers with leading zeros * * json_is_invalid_number() detects "numbers" which may pass strtod()'s * error checking, but sh
| 986 | * to generate an error. |
| 987 | */ |
| 988 | static int json_is_invalid_number(json_parse_t *json) |
| 989 | { |
| 990 | const char *p = json->ptr; |
| 991 | |
| 992 | /* Reject numbers starting with + */ |
| 993 | if (*p == '+') |
| 994 | return 1; |
| 995 | |
| 996 | /* Skip minus sign if it exists */ |
| 997 | if (*p == '-') |
| 998 | p++; |
| 999 | |
| 1000 | /* Reject numbers starting with 0x, or leading zeros */ |
| 1001 | if (*p == '0') { |
| 1002 | int ch2 = *(p + 1); |
| 1003 | |
| 1004 | if ((ch2 | 0x20) == 'x' || /* Hex */ |
| 1005 | ('0' <= ch2 && ch2 <= '9')) /* Leading zero */ |
| 1006 | return 1; |
| 1007 | |
| 1008 | return 0; |
| 1009 | } else if (*p <= '9') { |
| 1010 | return 0; /* Ordinary number */ |
| 1011 | } |
| 1012 | |
| 1013 | /* Reject inf/nan */ |
| 1014 | if (!strncasecmp(p, "inf", 3)) |
| 1015 | return 1; |
| 1016 | if (!strncasecmp(p, "nan", 3)) |
| 1017 | return 1; |
| 1018 | |
| 1019 | /* Pass all other numbers which may still be invalid, but |
| 1020 | * strtod() will catch them. */ |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static void json_next_number_token(json_parse_t *json, json_token_t *token) |
| 1025 | { |
no test coverage detected