Bug 67799 - The function strtoul and strtoull in <cstdlib> do not work correctly
Summary: The function strtoul and strtoull in <cstdlib> do not work correctly
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-01 15:06 UTC by 8826055
Modified: 2015-10-01 17:22 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Preprocessed File (87.56 KB, text/plain)
2015-10-01 15:06 UTC, 8826055
Details

Note You need to log in before you can comment on or make changes to this bug.
Description 8826055 2015-10-01 15:06:58 UTC
Created attachment 36433 [details]
Preprocessed File

In general, they convert a char sequence representing a negative value to its complement, while it should be the maximum positive value which can be represented by unsigned long (or unsigned long long) by www.cplusplus.com

Here is the code:
#include <iostream>
#include <climits>
#include <cstdlib>
using namespace std;
int main() {
	cout << strtoul("-2", nullptr, 10) << endl;
	cout << ULONG_MAX << endl;
	cout << strtoull("-2", nullptr, 10) << endl;
	cout << ULLONG_MAX << endl;
	return 0;
}
and the output is:
4294967294
4294967295
18446744073709551614
18446744073709551615

I use the IDE CodeBlocks to help compile the source file. The following is the build log.

Using built-in specs.
COLLECT_GCC=g++.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-5.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64 --with-gxx-include-dir=/mingw64/x86_64-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/include -I/c/mingw520/prerequisites/x86_64-zlib-static/include -I/c/mingw520/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/include -I/c/mingw520/prerequisites/x86_64-zlib-static/include -I/c/mingw520/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/lib -L/c/mingw520/prerequisites/x86_64-zlib-static/lib -L/c/mingw520/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 5.2.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 
COLLECT_GCC_OPTIONS='-Wall' '-fexceptions' '-v' '-save-temps' '-O2' '-std=c++14' '-c' '-o' 'obj\Release\main.o' '-shared-libgcc' '-mtune=core2' '-march=nocona'
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/5.2.0/cc1plus.exe -E -quiet -v -iprefix D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/ -D_REENTRANT D:\C++Program\test\main.cpp -mtune=core2 -march=nocona -std=c++14 -Wall -fexceptions -O2 -fpch-preprocess -o main.ii
ignoring duplicate directory "D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/5.2.0/include"
ignoring nonexistent directory "C:/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../include"
ignoring duplicate directory "D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/5.2.0/include-fixed"
ignoring duplicate directory "D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "C:/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/include
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/include-fixed
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/include
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../x86_64-w64-mingw32/include/c++
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/lib/gcc/../../x86_64-w64-mingw32/include/c++/backward
End of search list.
COLLECT_GCC_OPTIONS='-Wall' '-fexceptions' '-v' '-save-temps' '-O2' '-std=c++14' '-c' '-o' 'obj\Release\main.o' '-shared-libgcc' '-mtune=core2' '-march=nocona'
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/5.2.0/cc1plus.exe -fpreprocessed main.ii -quiet -dumpbase main.cpp -mtune=core2 -march=nocona -auxbase-strip obj\Release\main.o -O2 -Wall -std=c++14 -version -fexceptions -o main.s
GNU C++14 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) version 5.2.0 (x86_64-w64-mingw32)
	compiled by GNU C version 5.2.0, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++14 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) version 5.2.0 (x86_64-w64-mingw32)
	compiled by GNU C version 5.2.0, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 21af4d84309555a285c9e50ab0605aa1
COLLECT_GCC_OPTIONS='-Wall' '-fexceptions' '-v' '-save-temps' '-O2' '-std=c++14' '-c' '-o' 'obj\Release\main.o' '-shared-libgcc' '-mtune=core2' '-march=nocona'
 D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/bin/as.exe -v -o obj\Release\main.o main.s
GNU assembler version 2.25 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.25
COMPILER_PATH=D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/5.2.0/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../libexec/gcc/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/lib/../lib/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../lib/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/lib/;D:/mingw-w64/x86_64-5.2.0-posix-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.2.0/../../../
COLLECT_GCC_OPTIONS='-Wall' '-fexceptions' '-v' '-save-temps' '-O2' '-std=c++14' '-c' '-o' 'obj\Release\main.o' '-shared-libgcc' '-mtune=core2' '-march=nocona'
g++.exe  -o bin\Release\test.exe obj\Release\main.o  -s  
Output file is bin\Release\test.exe with size 18.50 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Comment 1 Jonathan Wakely 2015-10-01 15:13:58 UTC
If you look in <cstdlib> you will see these functions are not provided by GCC's header, they come from your C library, so this is nothing to do with GCC.

In any case, cplusplus.com is wrong, the functions behave as required by the C standard:

  If the subject sequence begins with a minus sign, the value resulting from
  the conversion is negated (in the return type).
Comment 2 8826055 2015-10-01 15:52:41 UTC
(In reply to Jonathan Wakely from comment #1)
> If you look in <cstdlib> you will see these functions are not provided by
> GCC's header, they come from your C library, so this is nothing to do with
> GCC.
> 
> In any case, cplusplus.com is wrong, the functions behave as required by the
> C standard:
> 
>   If the subject sequence begins with a minus sign, the value resulting from
>   the conversion is negated (in the return type).

But the C standard also says "If the correct value is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type and sign of the value, if any)".
Comment 3 Jonathan Wakely 2015-10-01 16:06:52 UTC
(In reply to Jonathan Wakely from comment #1)
> If you look in <cstdlib> you will see these functions are not provided by
> GCC's header, they come from your C library, so this is nothing to do with
> GCC.

Again, those functions have nothing to do with GCC.
Comment 4 8826055 2015-10-01 16:11:50 UTC
(In reply to Jonathan Wakely from comment #3)
> (In reply to Jonathan Wakely from comment #1)
> > If you look in <cstdlib> you will see these functions are not provided by
> > GCC's header, they come from your C library, so this is nothing to do with
> > GCC.
> 
> Again, those functions have nothing to do with GCC.

Thanks, I see it. I just want to confirm that such implementation conflicts with the C standard.
Comment 5 Martin Sebor 2015-10-01 17:22:04 UTC
(In reply to 8826055 from comment #4)

For POSIX, see http://austingroupbugs.net/view.php?id=700

For GLIBC see http://www.gnu.org/software/libc/manual/html_node/Parsing-of-Integers.html