common: avoid locale dependent strtod(3)

Replace `json_to_double()` (which uses `strtod(3)`) with our own
floating-point parsing function `json_to_millionths()` that
specifically expects to receive such a number that can fit in a
64 bit integer after being multiplied by 1 million.

The main piece of the code in this patch comes from
https://github.com/ElementsProject/lightning/pull/3535#discussion_r381041419

Changelog-None
This commit is contained in:
Rusty Russell
2020-02-19 11:11:36 +01:00
parent 89ceb273f5
commit 73ad9b5c0a
4 changed files with 104 additions and 11 deletions

View File

@@ -69,8 +69,13 @@ bool json_to_u32(const char *buffer, const jsmntok_t *tok,
bool json_to_u16(const char *buffer, const jsmntok_t *tok,
uint16_t *num);
/* Extract double from this (must be a number literal) */
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);
/*
* Extract a non-negative (either 0 or positive) floating-point number from this
* (must be a number literal), multiply it by 1 million and return it as an
* integer. Any fraction smaller than 0.000001 is ignored.
*/
bool json_to_millionths(const char *buffer, const jsmntok_t *tok,
u64 *millionths);
/* Extract signed integer from this (may be a string, or a number literal) */
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num);