| 14 | #include <errno.h> |
| 15 | |
| 16 | bool json_to_millionths(const char *buffer, const jsmntok_t *tok, |
| 17 | u64 *millionths) |
| 18 | { |
| 19 | int decimal_places = -1; |
| 20 | bool has_digits = 0; |
| 21 | |
| 22 | *millionths = 0; |
| 23 | for (int i = tok->start; i < tok->end; i++) { |
| 24 | if (cisdigit(buffer[i])) { |
| 25 | has_digits = true; |
| 26 | /* Ignore too much precision */ |
| 27 | if (decimal_places >= 0 && ++decimal_places > 6) |
| 28 | continue; |
| 29 | if (mul_overflows_u64(*millionths, 10)) |
| 30 | return false; |
| 31 | *millionths *= 10; |
| 32 | if (add_overflows_u64(*millionths, buffer[i] - '0')) |
| 33 | return false; |
| 34 | *millionths += buffer[i] - '0'; |
| 35 | } else if (buffer[i] == '.') { |
| 36 | if (decimal_places != -1) |
| 37 | return false; |
| 38 | decimal_places = 0; |
| 39 | } else |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | if (!has_digits) |
| 44 | return false; |
| 45 | |
| 46 | if (decimal_places == -1) |
| 47 | decimal_places = 0; |
| 48 | |
| 49 | while (decimal_places < 6) { |
| 50 | if (mul_overflows_u64(*millionths, 10)) |
| 51 | return false; |
| 52 | *millionths *= 10; |
| 53 | decimal_places++; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool json_to_number(const char *buffer, const jsmntok_t *tok, |
| 59 | unsigned int *num) |
no test coverage detected