From c19fed3f7594323ca65d3627b8ddb7541dfe630c Mon Sep 17 00:00:00 2001 From: James Bonfield Date: Thu, 18 Jul 2024 12:12:34 +0100 Subject: [PATCH] Fix signed overflow for hts_parse_decimal. Obviously there can still be overflows if we attempt to parse numbers which are too big, but for the legally accepted range of this, parsing "-9,223,372,036,854,775,808" as tested in test/sam.c triggered a problem as the positive version doesn't fit in "long long". We parse as unsigned and only switch to signed via the implicit return type conversion (and probably exploiting twos-complement, but that's a fair assumption). --- hts.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hts.c b/hts.c index caf85e64a..42be3f21f 100644 --- a/hts.c +++ b/hts.c @@ -3826,7 +3826,7 @@ void hts_itr_destroy(hts_itr_t *iter) } } -static inline long long push_digit(long long i, char c) +static inline unsigned long long push_digit(unsigned long long i, char c) { // ensure subtraction occurs first, avoiding overflow for >= MAX-48 or so int digit = c - '0'; @@ -3835,7 +3835,7 @@ static inline long long push_digit(long long i, char c) long long hts_parse_decimal(const char *str, char **strend, int flags) { - long long n = 0; + unsigned long long n = 0; int digits = 0, decimals = 0, e = 0, lost = 0; char sign = '+', esign = '+'; const char *s, *str_orig = str;