Skip to content

Commit ea2fecc

Browse files
whitslackrustyrussell
authored andcommitted
common: set errno=0 before calling strto{l,ul,ull}
The strto{l,ul,ull} functions do not set errno upon a successful return, so a successful return from a maximally valued input could be misinterpreted as an overflow error if errno happened already to be set to ERANGE before the call. To guard against this edge case, always set errno to zero before calling these functions if checking errno afterward. Changelog-None
1 parent 22b452a commit ea2fecc

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

common/bolt11.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ struct bolt11 *bolt11_decode_nosig(const tal_t *ctx, const char *str,
833833
* anything except a `multiplier` (see table above)... MUST fail the
834834
* payment.
835835
**/
836+
errno = 0;
836837
amount = strtoull(amountstr, &end, 10);
837838
if (amount == ULLONG_MAX && errno == ERANGE)
838839
return decode_fail(b11, fail,

common/json_parse.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static void parse_number(const char **guide, u32 *number)
233233
char *endp;
234234
long int l;
235235

236+
errno = 0;
236237
l = strtol(*guide, &endp, 10);
237238
assert(endp != *guide);
238239
assert(errno != ERANGE);
@@ -518,6 +519,7 @@ bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
518519
char *end;
519520
unsigned long btc, sat;
520521

522+
errno = 0;
521523
btc = strtoul(buffer + tok->start, &end, 10);
522524
if (btc == ULONG_MAX && errno == ERANGE)
523525
return false;

common/json_parse_simple.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num)
6666
char *end;
6767
unsigned long long l;
6868

69+
errno = 0;
6970
l = strtoull(buffer + tok->start, &end, 0);
7071
if (end != buffer + tok->end)
7172
return false;
@@ -88,6 +89,7 @@ bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num)
8889
char *end;
8990
long long l;
9091

92+
errno = 0;
9193
l = strtoll(buffer + tok->start, &end, 0);
9294
if (end != buffer + tok->end)
9395
return false;
@@ -129,7 +131,7 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num)
129131
if (end != buffer + tok->end)
130132
return false;
131133

132-
/* Check for overflow */
134+
/* Check for overflow/underflow */
133135
if (errno == ERANGE)
134136
return false;
135137

0 commit comments

Comments
 (0)