Skip to content

Commit

Permalink
Fix signed overflow for hts_parse_decimal.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jkbonfield committed Jul 18, 2024
1 parent f6bec84 commit c19fed3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down

0 comments on commit c19fed3

Please sign in to comment.