MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_to_millionths

Function json_to_millionths

common/json_parse.c:16–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14#include <errno.h>
15
16bool 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
58bool json_to_number(const char *buffer, const jsmntok_t *tok,
59 unsigned int *num)

Callers 2

param_millionthsFunction · 0.70
calc_maxfeeFunction · 0.50

Calls 3

cisdigitFunction · 0.85
mul_overflows_u64Function · 0.85
add_overflows_u64Function · 0.85

Tested by

no test coverage detected