This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


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   `------------------------


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]