Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: More trouble with strtoul |
| Date | 2015-03-20 09:05 -0700 |
| Organization | None to speak of |
| Message-ID | <87egojy7rj.fsf@kst-u.example.com> (permalink) |
| References | <meghap$4dk$1@speranza.aioe.org> |
jacob navia <jacob@jacob.remcomp.fr> writes:
> Consider this call
>
> strtoul("-4294967296",NULL,0)
>
> This is what lcc-win returns
>
> strtoul("-4294967296",0) len=11 res=4294967295 (0xffffffff)
> strtoul("-4294967297",0) len=11 res=4294967295 (0xffffffff)
>
> This is what gcc returns
>
> strtoul("-4294967296",0) len=11 res=1 (0x1)
> strtoul("-4294967297",0) len=11 res=1 (0x1)
I can't reproduce that result on my system. When I compile
and run this program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
int main(void) {
printf("ULONG_MAX = %lu (0x%lx)\n", ULONG_MAX, ULONG_MAX);
errno = 0;
unsigned long result = strtoul("-4294967296", NULL, 0);
printf("result = %lu (0x%lx), errno = %d",
result, result, errno);
if (errno == ERANGE) {
printf(" (ERANGE)");
}
putchar('\n');
}
I get:
ULONG_MAX = 4294967295 (0xffffffff)
result = 4294967295 (0xffffffff), errno = 34 (ERANGE)
Can you try the same program with gcc on your system?
> CONSTRAINTS:
>
> sizeof(unsigned long) == 4 (32 bits)
>
> Explanation for the lcc result:
>
> ULONG_MAX is 4294967295 (0xffffffff), so 4294967296 is clearly an overflow!
>
> In that case (an error) the software is supposed to return an error
> value (in this case ULONG_MAX).
>
> I just do not understand WHERE that "1" result from gcc is coming from.
It's not from gcc, it's from strtol(), which is implemented by the C
library. It looks like both lcc-win and the gcc/glibc implementation on
my system handle it correctly.
What gcc implementation are you using? What OS and runtime library?
[...]
> P.S. this can't be tested in a UNIX system since there sizeof(long)==8
> in modern Unix systems
If you have the right packages installed, you can compile with "-m32".
My system is Linux Mint 17, with gcc 4.8.2 and GNU libc6:i386 2.19.
I compiled the program with "gcc -std=c99 -m32".
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
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"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 08:13 +0100
Re: More trouble with strtoul Richard Heathfield <rjh@cpax.org.uk> - 2015-03-20 07:31 +0000
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-20 03:36 -0500
Re: More trouble with strtoul Richard Heathfield <rjh@cpax.org.uk> - 2015-03-20 08:42 +0000
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 10:12 +0100
Re: More trouble with strtoul Richard Heathfield <rjh@cpax.org.uk> - 2015-03-20 10:16 +0000
Re: More trouble with strtoul: BUG in MSVC jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 11:49 +0100
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-20 05:54 -0500
Re: More trouble with strtoul Barry Schwarz <schwarzb@dqel.com> - 2015-03-20 08:16 -0700
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 16:31 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 08:48 -0700
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 16:51 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 09:21 -0700
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 17:53 +0100
Re: More trouble with strtoul glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-03-20 17:23 +0000
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 18:25 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 10:35 -0700
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 08:52 +1300
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 13:41 -0700
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 08:45 +1300
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 08:47 +1300
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 21:16 +0100
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 09:33 +1300
Re: More trouble with strtoul Kaz Kylheku <kaz@kylheku.com> - 2015-03-20 20:05 +0000
Re: More trouble with strtoul Melzzzzz <mel@zzzzz.com> - 2015-03-20 21:55 +0100
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 11:13 +1300
Re: More trouble with strtoul Melzzzzz <mel@zzzzz.com> - 2015-03-20 23:41 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 16:27 -0700
Re: More trouble with strtoul Melzzzzz <mel@zzzzz.com> - 2015-03-21 01:47 +0100
Re: More trouble with strtoul Richard Heathfield <rjh@cpax.org.uk> - 2015-03-21 02:03 +0000
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 15:13 +1300
Re: More trouble with strtoul Melzzzzz <mel@zzzzz.com> - 2015-03-21 03:22 +0100
Re: More trouble with strtoul Richard Heathfield <rjh@cpax.org.uk> - 2015-03-21 02:25 +0000
Re: More trouble with strtoul Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-03-20 10:11 +0100
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-20 06:13 -0500
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 09:05 -0700
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 17:35 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 10:56 -0700
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-20 15:31 -0500
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 18:28 +0100
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 09:01 +1300
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 21:12 +0100
Re: More trouble with strtoul Kaz Kylheku <kaz@kylheku.com> - 2015-03-20 20:21 +0000
Re: More trouble with strtoul Ian Collins <ian-news@hotmail.com> - 2015-03-21 09:36 +1300
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-20 15:47 -0500
Re: More trouble with strtoul Kaz Kylheku <kaz@kylheku.com> - 2015-03-20 21:20 +0000
Re: More trouble with strtoul Robert Wessel <robertwessel2@yahoo.com> - 2015-03-21 00:24 -0500
Re: More trouble with strtoul jacob navia <jacob@jacob.remcomp.fr> - 2015-03-20 23:04 +0100
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-20 13:41 -0700
Re: More trouble with strtoul raltbos@xs4all.nl (Richard Bos) - 2015-03-21 22:49 +0000
Re: More trouble with strtoul Keith Thompson <kst-u@mib.org> - 2015-03-21 16:17 -0700
Re: More trouble with strtoul Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 12:01 -0700
csiph-web