X86_64平台下strtoul诡异问题,都是#include惹的祸?

在CentOS5.4环境中,使用gcc4.1.2编译器时,作者遇到了一个奇怪的问题:调用strtoul函数解析特定字符串时,得到的结果并非预期值。通过深入探究发现,未包含<stdlib.h>头文件会导致编译器内部处理错误,进而影响函数的行为。加入该头文件后,问题得以解决。
环境CentOS 5.4 X86_64 gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
碰到一个strtoul的诡异问题,简化出来就是这个程序

#include <stdio.h>

int main(){
char *a = "4297757104";
unsigned long int l = strtoul(a, NULL, 0);
printf("%lu\n", l);
return 0;
}

结果输出2789808而非4297757104。查了半天手册也没发现什么不对的,发现手册中有#include <stdlib.h> ,加上去居然就对了。百思不解,原来编译也能通过,为什么加个头文件就导致程序行为改变了?

之前也听说过GCC对某些函数会做内部特殊处理。既然程序行为改变,二进制级别上肯定就有区别。先用objdump -T long 将动态链接的函数导出一看,都一样。干脆直接objdum -S long> long.s 把程序反汇编,然后diff查看,结果发现没有加include的版本,多了cltq 这么条指令
40050b: e8 e8 fe ff ff callq 4003f8 <strtoul@plt>
400510: 48 98 cltq
就是罪魁祸首了。这条指令是将eax寄存器的值符号扩展到rax。
4297757104的二进制是100000000001010101001000110110000,其中低32位截取下来是00000000001010101001000110110000,然后符号扩展到64位,只要将高32位补0即可。换算下来,刚好就是2789808。也就是说,这条指令把rax高32位的数据给清掉了。

教训:还是那两句老话
1)编译时一定要加上 -Wall,加上之后就会报一个 warning: implicit declaration of function 'strtoul'。否则,怎么死的都不知道。
2)不要忽略任何warning
不过,这个warning对于我等新手来说,很容易无视,也很难明白其中含义
In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:202: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__locale:36: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:95:12: error: unknown type name '_locale_t'; did you mean 'locale_t'? 95 | locale_t(_locale_t __xlocale, const char* __xlocale_str) | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:91:7: note: 'locale_t' declared here 91 | class locale_t { | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:140:12: error: unknown type name '_locale_t'; did you mean 'locale_t'? 140 | operator _locale_t() const { return __locale_; } | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:91:7: note: 'locale_t' declared here 91 | class locale_t { | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:149:3: error: unknown type name '_locale_t'; did you mean 'locale_t'? 149 | _locale_t __locale_; | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:91:7: note: 'locale_t' declared here 91 | class locale_t { | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:149:13: error: field has incomplete type 'locale_t' 149 | _locale_t __locale_; | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:91:7: note: definition of 'locale_t' is not complete until the closing '}' 91 | class locale_t { | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:199:53: error: unknown type name '_locale_t' 199 | inline _LIBCPP_HIDE_FROM_ABI int islower_l(int __c, _locale_t __loc) { return _islower_l((int)__c, __loc); } | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:201:53: error: unknown type name '_locale_t' 201 | inline _LIBCPP_HIDE_FROM_ABI int isupper_l(int __c, _locale_t __loc) { return _isupper_l((int)__c, __loc); } | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:233:95: error: unknown type name 'va_list' 233 | _LIBCPP_EXPORTED_FROM_ABI int vasprintf_l(char** __ret, locale_t __loc, const char* __format, va_list __ap); | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:719:68: error: use of undeclared identifier '_strtoi64_l' 719 | long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE); | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:189:19: note: expanded from macro 'strtoll_l' 189 | #define strtoll_l _strtoi64_l | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:751:68: error: use of undeclared identifier '_strtoui64_l' 751 | unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE); | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:190:20: note: expanded from macro 'strtoull_l' 190 | #define strtoull_l _strtoui64_l | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:781:10: error: use of undeclared identifier '_strtod_l' 781 | return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE); | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:191:18: note: expanded from macro 'strtod_l' 191 | #define strtod_l _strtod_l | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:1023:7: error: use of undeclared identifier '_sscanf_l' 1023 | if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1) | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__locale_dir/locale_base_api/bsd_locale_defaults.h:34:32: note: expanded from macro '__libcpp_sscanf_l' 34 | #define __libcpp_sscanf_l(...) sscanf_l(__VA_ARGS__) | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:227:38: note: expanded from macro 'sscanf_l' 227 | #define sscanf_l(__s, __l, __f, ...) _sscanf_l(__s, __f, __l, __VA_ARGS__) | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:1106:12: error: use of undeclared identifier '_isxdigit_l'; did you mean 'isxdigit'? 1106 | if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE)) | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:204:20: note: expanded from macro 'isxdigit_l' 204 | #define isxdigit_l _isxdigit_l | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/cctype:122:9: note: 'isxdigit' declared here 122 | using ::isxdigit _LIBCPP_USING_IF_EXISTS; | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:1106:12: error: too many arguments to function call, expected 1, have 2; did you mean '::std::isxdigit'? 1106 | if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE)) | ^~~~~~~~~~ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:204:20: note: expanded from macro 'isxdigit_l' 204 | #define isxdigit_l _isxdigit_l | ^~~~~~~~~~~ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/cctype:122:9: note: '::std::isxdigit' declared here 122 | using ::isxdigit _LIBCPP_USING_IF_EXISTS; | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:1110:12: error: use of undeclared identifier '_isdigit_l'; did you mean 'isdigit'? 1110 | if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE)) | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:203:19: note: expanded from macro 'isdigit_l' 203 | #define isdigit_l _isdigit_l | ^ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/cctype:115:9: note: 'isdigit' declared here 115 | using ::isdigit _LIBCPP_USING_IF_EXISTS; | ^ In file included from D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:7: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/vector:325: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_bool.h:20: In file included from C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__format/formatter_integral.h:35: C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/locale:1110:12: error: too many arguments to function call, expected 1, have 2; did you mean '::std::isdigit'? 1110 | if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE)) | ^~~~~~~~~ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/__support/win32/locale_win32.h:203:19: note: expanded from macro 'isdigit_l' 203 | #define isdigit_l _isdigit_l | ^~~~~~~~~~ C:\Program Files (x86)\TOSUN\TSMaster\Data\compilers\llvm_mingw_x86\bin/../include/c++/v1/cctype:115:9: note: '::std::isdigit' declared here 115 | using ::isdigit _LIBCPP_USING_IF_EXISTS; | ^ D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:67:17: error: use of undeclared identifier 'vsnprintf_s' 67 | while ((ret = vsnprintf_s(&buf[0], buf.size(), buf.size(), fmt, ap)) == -1) { | ^ D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:87:17: error: use of undeclared identifier 'vsnprintf_s' 87 | while ((ret = vsnprintf_s(&buf[0], buf.size(), buf.size(), fmt, ap)) == -1) { | ^ D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:104:110: error: format specifies type 'unsigned int' but the argument has type 's32' (aka 'long') [-Werror,-Wformat] 104 | sprintf(s, "%s %d %03X Tx d %d [%02X %02X %02X %02X %02X %02X %02X %02X]", ADesc, ACAN->FIdxChn, ACAN->FIdentifier, ACAN->FDLC, ACAN->FData[0], ACAN->FData[1], ACAN->FData[2], ACAN->FData[3], ACAN->FData[4], ACAN->FData[5], ACAN->FData[6], ACAN->FData[7]); | ~~~~ ^~~~~~~~~~~~~~~~~ | %03lX D:\TS_Master\TSMaster\Configuration\TOSUN\TSMaster\MiniProgram\Build\CCode7142\CCode7142Extern.cpp:106:68: error: format specifies type 'unsigned int' but the argument has type 's32' (aka 'long') [-Werror,-Wformat] 106 | sprintf(s, "%s %d %03X Tx r %d", ADesc, ACAN->FIdxChn, ACAN->FIdentifier, ACAN->FDLC); | ~~~~ ^~~~~~~~~~~~~~~~~ | %03lX fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated.
最新发布
09-26
/opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_chip_open': core.c:(.text+0x60): undefined reference to `__lstat64_time64' core.c:(.text+0x82): undefined reference to `__stat64_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '256', this reader only handles version 2, 3 and 4 information. core.c:(.text+0x1c6): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_update': core.c:(.text+0x2ea): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_values': core.c:(.text+0x504): undefined reference to `__ioctl_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_event_single': core.c:(.text+0x6e0): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_get_value_bulk': core.c:(.text+0x942): undefined reference to `__ioctl_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '55303', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o):core.c:(.text+0x968): more undefined references to `__ioctl_time64' follow /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '6656', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_event_wait_bulk': core.c:(.text+0xf30): undefined reference to `__ppoll64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-helpers.o): In function `gpiod_chip_open_lookup': helpers.c:(.text+0x178): undefined reference to `__isoc23_strtoul' collect2: 错误: ld 返回 1 make: *** [Makefile:63:build/bin/demo] 错误 1
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值