Skip to content

Commit 25d03c6

Browse files
jkbonfielddaviesrob
authored andcommitted
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).
1 parent a314b21 commit 25d03c6

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

hts.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3826,7 +3826,7 @@ void hts_itr_destroy(hts_itr_t *iter)
38263826
}
38273827
}
38283828

3829-
static inline long long push_digit(long long i, char c)
3829+
static inline unsigned long long push_digit(unsigned long long i, char c)
38303830
{
38313831
// ensure subtraction occurs first, avoiding overflow for >= MAX-48 or so
38323832
int digit = c - '0';
@@ -3835,7 +3835,7 @@ static inline long long push_digit(long long i, char c)
38353835

38363836
long long hts_parse_decimal(const char *str, char **strend, int flags)
38373837
{
3838-
long long n = 0;
3838+
unsigned long long n = 0;
38393839
int digits = 0, decimals = 0, e = 0, lost = 0;
38403840
char sign = '+', esign = '+';
38413841
const char *s, *str_orig = str;

0 commit comments

Comments
 (0)