Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: More trouble with strtoul Date: Fri, 20 Mar 2015 08:48:33 -0700 Organization: None to speak of Lines: 56 Message-ID: <87iodvy8j2.fsf@kst-u.example.com> References: <6ilnga1dp3mj4vaj8oee3jqvmf73qus1jt@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="c77ff4d35e33d1c939d3461ebde8dd0d"; logging-data="24096"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1W54bBRc+/DBGmRiZ9+i3" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:qtEGtVHc7YgLLWElMz6C17sWsFA= sha1:v/4KHPuqf75KfI6q28u4O2agwLE= Xref: csiph.com comp.lang.c:58510 jacob navia writes: > Le 20/03/2015 09:36, Robert Wessel a écrit : >> does the GCC version set ERRNO correctly? > > YES! THAT's the thing to test. > > I copiled following program with gcc: > > 1 #include > 2 #include > 3 #include > 4 #include > 5 int main(int argc,char *argv[]) > 6 { > 7 char *end; > 8 char *s = "-2147483647"; > 9 if (argc > 1) > 10 s = argv[1]; > 11 errno = 0; > 12 unsigned long m = strtoul(s,&end,0); > 13 printf("%lu %lld",m,end-s); > 14 printf(" errno = %s\n",strerror(errno)); > 15 } > > and obtained > > 1 1 11 errno = Result too large Is that the exact code you ran? I get several warnings when I compile it. On line 13, "%lld" is the incorrect format for end-s (unless ptrdiff_t happens to be long long). I suggest: printf("%lu %d", m, (int)end-s); > Then, since I set errno to zero at line 11, that gcc is setting errno > correctly meaning that this is an error, but not returning the error > result, meaning it is negating something after the overflow. On line 14, strerror is undeclared; you need #include . Also, the printf call on line 13 is likely to clobber the value of errno, so depending on the implementation the output from line 14 may be meaningless. > Is this a bug in gcc? If it's a bug, it's not in gcc. All gcc does is generate a call to the strtoul function. The implementation of the function is in the library. What implementation are you using? On most Linux systems, strtoul is implemented by glic (GNU libc). If you happen to be using Cygwin, the library implementation is "newlib", if I recall correctly. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"