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
| 1239 | * to generate an error. |
| 1240 | */ |
| 1241 | static int json_is_invalid_number(json_parse_t *json) |
| 1242 | { |
| 1243 | const char *p = json->ptr; |
| 1244 | |
| 1245 | /* Reject numbers starting with + */ |
| 1246 | if (*p == '+') |
| 1247 | return 1; |
| 1248 | |
| 1249 | /* Skip minus sign if it exists */ |
| 1250 | if (*p == '-') |
| 1251 | p++; |
| 1252 | |
| 1253 | /* Reject numbers starting with 0x, or leading zeros */ |
| 1254 | if (*p == '0') { |
| 1255 | int ch2 = *(p + 1); |
| 1256 | |
| 1257 | if ((ch2 | 0x20) == 'x' || /* Hex */ |
| 1258 | ('0' <= ch2 && ch2 <= '9')) /* Leading zero */ |
| 1259 | return 1; |
| 1260 | |
| 1261 | return 0; |
| 1262 | } else if (*p <= '9') { |
| 1263 | return 0; /* Ordinary number */ |
| 1264 | } |
| 1265 | |
| 1266 | /* Reject inf/nan */ |
| 1267 | if (!strncasecmp(p, "inf", 3)) |
| 1268 | return 1; |
| 1269 | if (!strncasecmp(p, "nan", 3)) |
| 1270 | return 1; |
| 1271 | |
| 1272 | /* Pass all other numbers which may still be invalid, but |
| 1273 | * strtod() will catch them. */ |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static void json_next_number_token(json_parse_t *json, json_token_t *token) |
| 1278 | { |