Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hts_parse_decimal and make synced BCF targets parsing more robust #1396

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -3482,7 +3482,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;
int decimals = 0, e = 0, lost = 0;
int decimals = 0, e = 0, lost = 0, has_digit = 0;
char sign = '+', esign = '+';
const char *s;

Expand All @@ -3491,13 +3491,20 @@ long long hts_parse_decimal(const char *str, char **strend, int flags)

if (*s == '+' || *s == '-') sign = *s++;
while (*s)
if (isdigit_c(*s)) n = push_digit(n, *s++);
if (isdigit_c(*s)) n = push_digit(n, *s++), has_digit = 1;
else if (*s == ',' && (flags & HTS_PARSE_THOUSANDS_SEP)) s++;
else break;

if (*s == '.') {
s++;
while (isdigit_c(*s)) decimals++, n = push_digit(n, *s++);
while (isdigit_c(*s)) decimals++, n = push_digit(n, *s++), has_digit = 1;
}

// there must have been a digit or else cannot be a valid number
if ( !has_digit )
{
if ( strend ) *strend = (char*)str;
return 0;
}

if (*s == 'E' || *s == 'e') {
Expand Down
9 changes: 6 additions & 3 deletions synced_bcf_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,15 +1107,15 @@ static int _regions_parse_line(char *line, int ichr, int ifrom, int ito, char **
if ( k==l )
{
*from = *to = hts_parse_decimal(ss, &tmp, 0);
if ( tmp==ss ) return -1;
if ( tmp==ss || (*tmp && *tmp!='\t') ) return -1;
}
else
{
if ( k==ifrom )
*from = hts_parse_decimal(ss, &tmp, 0);
else
*to = hts_parse_decimal(ss, &tmp, 0);
if ( ss==tmp ) return -1;
if ( ss==tmp || (*tmp && *tmp!='\t') ) return -1;

for (i=k; i<l && *se; i++)
{
Expand All @@ -1127,7 +1127,7 @@ static int _regions_parse_line(char *line, int ichr, int ifrom, int ito, char **
*to = hts_parse_decimal(ss, &tmp, 0);
else
*from = hts_parse_decimal(ss, &tmp, 0);
if ( ss==tmp ) return -1;
if ( ss==tmp || (*tmp && *tmp!='\t') ) return -1;
}

ss = se = line;
Expand Down Expand Up @@ -1193,7 +1193,10 @@ bcf_sr_regions_t *bcf_sr_regions_init(const char *regions, int is_file, int ichr
hts_close(reg->file); reg->file = NULL; free(reg);
return NULL;
}
ito = ifrom;
}
else if ( ito<0 )
ito = abs(ito);
if ( !ret ) continue;
if ( is_bed ) from++;
*chr_end = 0;
Expand Down