strutil: accept trailing whitespace after number text

Some SCPI based drivers fail to convert response data, because strutil
conversion helpers sr_atol() and sr_atof() don't like trailing spaces
after the number's text that successfully got converted.

It's yet to get determined whether all call sites of the conversion
routines like their eating adjacent whitespace. But given that the
conversion routine explicitly checks for end of the string after the
number, no call site should expect or even depend on trailing text to
keep its whitespace.

See bug #788 for a discussion and example data.
This commit is contained in:
Gerhard Sittig 2018-02-04 20:17:08 +01:00 committed by Uwe Hermann
parent 751ba4c8ed
commit b8278e0943
1 changed files with 7 additions and 0 deletions

View File

@ -18,6 +18,7 @@
*/ */
#include <config.h> #include <config.h>
#include <ctype.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -67,6 +68,9 @@ SR_PRIV int sr_atol(const char *str, long *ret)
errno = 0; errno = 0;
tmp = strtol(str, &endptr, 10); tmp = strtol(str, &endptr, 10);
while (endptr && isspace(*endptr))
endptr++;
if (!endptr || *endptr || errno) { if (!endptr || *endptr || errno) {
if (!errno) if (!errno)
errno = EINVAL; errno = EINVAL;
@ -129,6 +133,9 @@ SR_PRIV int sr_atod(const char *str, double *ret)
errno = 0; errno = 0;
tmp = strtof(str, &endptr); tmp = strtof(str, &endptr);
while (endptr && isspace(*endptr))
endptr++;
if (!endptr || *endptr || errno) { if (!endptr || *endptr || errno) {
if (!errno) if (!errno)
errno = EINVAL; errno = EINVAL;