https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=288440
Bug ID: 288440
Summary: sscanf("003", "%i", &val); returns 1 but misparses
with val == 0 instead of val == 3
Product: Base System
Version: CURRENT
Hardware: Any
URL: https://github.com/dgibson/dtc/issues/165
OS: Any
Status: New
Keywords: standards
Severity: Affects Some People
Priority: ---
Component: bin
Assignee: [email protected]
Reporter: [email protected]
FreeBSD's sscanf (and likely all siblings) are broken and can't parse "003" as
3 through %i. I suspect vfscanf.c's parseint()/parseint_fsm() which apparently
don't skip the center 0 and returns with *base == 0 and *state == any, instead
of setting the base to 8: if it has seen 00, it hasn't seen 0b/0B/0x/0X, so
base 2 and base 16 are out of the question, and from the leading 0 prefix it's
clear we're parsing octal input.
Also, sscanf is in this situation (%i format string) supposed to scan as-if
strtol were being used, and strtol works properly in this special case.
Use case, see URL.
Demo program:
// this is try.c
// compile and run: cc -o try try.c && ./try
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main() {
int val;
int len;
int retval;
retval = sscanf("003", "%i%n", &val, &len);
printf("sscanf, retval = %d, val = %d, len = %d\n", retval, val, len);
long longval;
char *startptr = "003";
char *endptr;
errno = 0;
longval = strtol(startptr, &endptr, 0);
printf("strtol, longval = %ld, len = %d, errno = %d\n", longval,
(int)(endptr - startptr), errno);
return 0;
}
--
You are receiving this mail because:
You are the assignee for the bug.