我遇到了 c 库函数 strtoull 的问题,它返回了错误的输出。
int main(int argc, char *argv[])
{
unsigned long long int intValue;
if(atoi(argv[2]) == 1)
{
intValue = strtoull((const char *)argv[1], 0, 10 );
}
else
{
// ...
}
printf("intValue of %s is %llu \n", argv[1], intValue);
return 0;
}
我构建了它们并生成了 32 位和 64 位可执行文件作为 str32_new 和 str64_new。但是从 32 位 exe 收到的输出是错误的,因为返回了错误的数字:
strtoull 应该为传递的字符串“5368709120”返回编号 5368709120,但它返回给我 1073741824。
# ./str32_new "5368709120" 1
intValue of 5368709120 is 1073741824
我注意到,当我从字符串中减少一个字符时,它会显示正确的输出。
# ./str32_new "536870912" 1
intValue of 536870912 is 536870912
附加到 32 位 exe 的 glibc 是
# readelf -Wa /home/str32_new | grep strt
[39] .shstrtab STRTAB 00000000 002545 000190 00 0 0 1
[41] .strtab STRTAB 00000000 0032f8 0002a4 00 0 0 1
0804a014 00000607 R_386_JUMP_SLOT 00000000 strtoull
6: 00000000 0 FUNC GLOBAL DEFAULT UND strtoull@GLIBC_2.0 (2)
55: 00000000 0 FILE LOCAL DEFAULT ABS strtoull.c
75: 00000000 0 FUNC GLOBAL DEFAULT UND strtoull@@GLIBC_2.0
77: 08048534 915 FUNC GLOBAL DEFAULT 15 my_strtoull
附加到 64 位 exe 的 glibc 是
# readelf -Wa /home/str64_new | grep strt
[39] .shstrtab STRTAB 0000000000000000 001893 000192 00 0 0 1
[41] .strtab STRTAB 0000000000000000 002cd0 00029b 00 0 0 1
0000000000601028 0000000700000007 R_X86_64_JUMP_SLOT 0000000000000000 strtoull + 0
7: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strtoull@GLIBC_2.2.5 (2)
57: 0000000000000000 0 FILE LOCAL DEFAULT ABS strtoull.c
73: 00000000004006cc 804 FUNC GLOBAL DEFAULT 15 my_strtoull
82: 0000000000000000 0 FUNC GLOBAL DEFAULT UND strtoull@@GLIBC_2.2.5
64 位 exe 显示正确的输出,但在某些系统上它也表现异常。为什么 32 位 exe 中的 strtoull 行为如此以及如何解决此问题?