["Vadim Zhukovsky" <zva@ukrpost.net>] scanf bug

Ulrich Drepper drepper@redhat.com
Tue Jul 17 12:47:00 GMT 2001


Andreas Jaeger <aj@suse.de> writes:

> #include <stdio.h>
> #include <stdlib.h>
> 
> const char *oct_long_long = "01000000000000000000000";
> 
> void main()
> {
>   long long ll1, ll2;
>   sscanf(oct_long_long, "%Lo", &ll1);
>   sscanf(oct_long_long, "%Li", &ll2);
>   printf("%%Lo: %Ld, %%Li: %Ld\n", ll1, ll2);
> }
> ----------------------------------------------------------------
> %Lo: -9223372036854775808, %Li: 9223372036854775807

This is the correct result.  The octal number is 8000000000000000 is
hex.  I.e., bit 63 is set.  I.e., this number is not positive.  Which
in turn means that strtoll() detects an overflow since it's positive
number > LONG_LONG_MAX.  This means strtoll() returns LONG_LONG_MAX
and sets errno.

%o is an unsigned format and uses strtoull() which does not have this
problem.

I know that other implementations don't produce this result but they
are wrong, not glibc.

-- 
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------



More information about the Libc-alpha mailing list