From: Gerhard Sittig Date: Thu, 19 Jul 2018 19:05:54 +0000 (+0200) Subject: strutil: handle empty fractional in parse rational X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=42408643f9dcb80d1744aaa4d08e0170594daad1;p=libsigrok.git strutil: handle empty fractional in parse rational Accept numbers like "123." where the period (dot) is present yet the fractional part is empty. Adding a period but no additional digits is a popular method of turning an otherwise integer literal into a float. Compilers and strtod() routines accept this notation, too, so we have to expect seeing such input. --- diff --git a/src/strutil.c b/src/strutil.c index 99e711d9..28f202c9 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -631,8 +631,15 @@ SR_API int sr_parse_rational(const char *str, struct sr_rational *ret) errno = 0; if (*endptr == '.') { + bool is_exp, is_eos; const char *start = endptr + 1; fractional = g_ascii_strtoll(start, &endptr, 10); + is_exp = *endptr == 'E' || *endptr == 'e'; + is_eos = *endptr == '\0'; + if (endptr == start && (is_exp || is_eos)) { + fractional = 0; + errno = 0; + } if (errno) return SR_ERR; fractional_len = endptr - start;