Skip to content

Commit 385f22c

Browse files
daviesrobwhitwham
authored andcommitted
Speed up hts_parse_decimal() handling of oversized exponents
The loop to apply exponent values could be made to run for a very long time (although not forever) if the exponent value in the number passed to the function was very large. As in both the +ve and -ve exponent cases this will eventually result in `n` becoming zero, it's possible to short-cut the rest of the loop as the final value will no longer change. (While the -ve case is obvious, the +ve one is a bit more complicated due to wrap-around. However, as each multiplication by 10 adds a factor of 2 (as 10 = 5 + 2), after at most 64 enough 2's will have accumulated in the product to make it divide exactly into 2^64 and the result will be zero.) Note that this does not try to add overflow detection, which would require more extensive changes and is left to future work. Signed-off-by: Rob Davies <rmd+git@sanger.ac.uk>
1 parent 942f3be commit 385f22c

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Bug fixes
9494
BGZF::errcode field after calling bgzf_close() as it may no longer be valid.
9595
(PR #2035, #2042)
9696

97+
* Speed up hts_parse_decimal() handling of oversized exponents
98+
(PR #2045)
99+
97100
Documentation updates
98101
---------------------
99102

hts.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,8 +3923,8 @@ long long hts_parse_decimal(const char *str, char **strend, int flags)
39233923
}
39243924

39253925
e -= decimals;
3926-
while (e > 0) n *= 10, e--;
3927-
while (e < 0) lost += n % 10, n /= 10, e++;
3926+
while (e > 0 && n) n *= 10, e--;
3927+
while (e < 0 && n) lost += n % 10, n /= 10, e++;
39283928

39293929
if (lost > 0) {
39303930
hts_log_warning("Discarding fractional part of %.*s", (int)(s - str), str);

0 commit comments

Comments
 (0)