| 48 | } |
| 49 | |
| 50 | bool json_to_millionths(const char *buffer, const jsmntok_t *tok, |
| 51 | u64 *millionths) |
| 52 | { |
| 53 | int decimal_places = -1; |
| 54 | bool has_digits = 0; |
| 55 | |
| 56 | *millionths = 0; |
| 57 | for (int i = tok->start; i < tok->end; i++) { |
| 58 | if (isdigit(buffer[i])) { |
| 59 | has_digits = true; |
| 60 | /* Ignore too much precision */ |
| 61 | if (decimal_places >= 0 && ++decimal_places > 6) |
| 62 | continue; |
| 63 | if (mul_overflows_u64(*millionths, 10)) |
| 64 | return false; |
| 65 | *millionths *= 10; |
| 66 | if (add_overflows_u64(*millionths, buffer[i] - '0')) |
| 67 | return false; |
| 68 | *millionths += buffer[i] - '0'; |
| 69 | } else if (buffer[i] == '.') { |
| 70 | if (decimal_places != -1) |
| 71 | return false; |
| 72 | decimal_places = 0; |
| 73 | } else |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | if (!has_digits) |
| 78 | return false; |
| 79 | |
| 80 | if (decimal_places == -1) |
| 81 | decimal_places = 0; |
| 82 | |
| 83 | while (decimal_places < 6) { |
| 84 | if (mul_overflows_u64(*millionths, 10)) |
| 85 | return false; |
| 86 | *millionths *= 10; |
| 87 | decimal_places++; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool json_to_number(const char *buffer, const jsmntok_t *tok, |
| 93 | unsigned int *num) |
no test coverage detected