在 mingw64-gcc 上可能存在可变参数的错误
Possible bug with variadic arguments on mingw64-gcc
我有一个烦人的错误,我试图追踪它,然后我创建了一个示例,但我仍然不能 100% 确定这是否是编译器问题。
让我给你一些关于我首先使用的版本的信息。
x86_64-w64-mingw32-g++ --version
x86_64-w64-mingw32-g++.exe (Rev1, Built by MSYS2 project) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我知道它不是最新版本,但它是您可以为 MSY 获得的最新版本。
这是示例代码:
#include <cstdint>
#include <stdio.h>
#include <string.h>
#include <cstdarg>
void test1(){
uint64_t a = 0x3333333333333333;
uint64_t b = 1;
uint64_t c = 2;
uint64_t d = 3;
printf("output should be:n3 2 1 0 3333333333333333n");
printf("but output is:n%llx %llx %llx %llx %llxn",d,c,b,0,a);
}
void test(uint64_t x1,uint64_t x2,uint64_t x3,uint64_t x4,uint64_t x5,uint64_t x6,
uint64_t x21,uint64_t x22,uint64_t x23,uint64_t x24,uint64_t x25,uint64_t x26,
uint64_t x31,uint64_t x32,uint64_t x33,uint64_t x34,uint64_t x35,uint64_t x36,
uint64_t x41,uint64_t x42,uint64_t x43,uint64_t x44,uint64_t x45,uint64_t x46){
printf("startn");
}
void test_(){
test(0x7777777777777771,0x7777777777777772,0x7777777777777773,0x7777777777777774,0x7777777777777775,0x7777777777777776,
0x7777777777777771,0x7777777777777772,0x7777777777777773,0x7777777777777774,0x7777777777777775,0x7777777777777776,
0x7777777777777771,0x7777777777777772,0x7777777777777773,0x7777777777777774,0x7777777777777775,0x7777777777777776,
0x7777777777777771,0x7777777777777772,0x7777777777777773,0x7777777777777774,0x7777777777777775,0x7777777777777776);
}
int main(int argc,char** argv){
test_();
test1();
}
并编译并执行它:
x86_64-w64-mingw32-g++ -O0 test.cpp && ./a.exe
现在令人惊讶的部分来了,输出是:
start
output should be:
3 2 1 0 3333333333333333
but output is:
3 2 1 7777777700000000 3333333333333333
在上面的例子中,我使用 printf 来生成和可视化问题。
它可能发生在任何其他函数上,而不是使用变分参数的 printf。
例如:void blah(a,b,...)
出于某种原因,编译器做了这个意想不到的事情。 可悲的是,谷歌搜索并没有把我引向正确的方向。
这让我想到了一个问题,这是否真的是编译器的问题(linux 没有这样的问题(,或者它是否是一个编程错误(比如忘记转换 0 数字(。
看看反汇编的代码,我可以看到产生问题的部分:
objdump -M intel -S ./a.exe|egrep -A 30 'test1.+:'
0000000000401570 <_Z5test1v>:
401570: 55 push rbp
401571: 48 89 e5 mov rbp,rsp
401574: 48 83 ec 50 sub rsp,0x50
401578: 48 b8 33 33 33 33 33 movabs rax,0x3333333333333333
40157f: 33 33 33
401582: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
401586: 48 c7 45 f0 01 00 00 mov QWORD PTR [rbp-0x10],0x1
40158d: 00
40158e: 48 c7 45 e8 02 00 00 mov QWORD PTR [rbp-0x18],0x2
401595: 00
401596: 48 c7 45 e0 03 00 00 mov QWORD PTR [rbp-0x20],0x3
40159d: 00
40159e: 48 8d 0d 5b 7a 00 00 lea rcx,[rip+0x7a5b] # 409000 <.rdata>
4015a5: e8 a6 66 00 00 call 407c50 <_Z6printfPKcz>
4015aa: 4c 8b 45 f0 mov r8,QWORD PTR [rbp-0x10]
4015ae: 48 8b 4d e8 mov rcx,QWORD PTR [rbp-0x18]
4015b2: 48 8b 45 e0 mov rax,QWORD PTR [rbp-0x20]
4015b6: 48 8b 55 f8 mov rdx,QWORD PTR [rbp-0x8]
4015ba: 48 89 54 24 28 mov QWORD PTR [rsp+0x28],rdx
4015bf: c7 44 24 20 00 00 00 mov DWORD PTR [rsp+0x20],0x0
4015c6: 00
4015c7: 4d 89 c1 mov r9,r8
4015ca: 49 89 c8 mov r8,rcx
4015cd: 48 89 c2 mov rdx,rax
4015d0: 48 8d 0d 59 7a 00 00 lea rcx,[rip+0x7a59] # 409030 <.rdata+0x30>
4015d7: e8 74 66 00 00 call 407c50 <_Z6printfPKcz>
4015dc: 90 nop
4015dd: 48 83 c4 50 add rsp,0x50
4015e1: 5d pop rbp
4015e2: c3 ret
而且我完全不知道为什么它在偏移量 4015bf 上使用该 dword。 也许有人可以阐明我的问题,或者能够使用较新的mingw版本对其进行测试。
(我已经尝试过使用 ubuntu 的"仿生海狸"docker 映像,但遗憾的是结果相同......好吧,无论如何,它具有相同版本的x86_64-W64-mingW32-G++(
参数类型不匹配:
printf("but output is:n%llx %llx %llx %llx %llxn",d,c,b,0,a);
值 0 的类型为int
,但%llx
格式说明符需要类型为unsigned long long int
的变量。 使用错误的格式说明符会调用未定义的行为。
由于printf
是一个可变参数函数,因此它无法自动将此值转换为正确的类型。 因此,您需要使用正确的格式说明符:
printf("but output is:n%llx %llx %llx %d %llxn",d,c,b,0,a);
或者投射有问题的论点
printf("but output is:n%llx %llx %llx %llu %llxn",d,c,b,(unsigned long long)0,a);
或者(在常量的情况下(使用正确的类型后缀
printf("but output is:n%llx %llx %llx %llu %llxn",d,c,b,0ULL,a);
printf
中的 0 类型错误,int
不是long long
。尝试改用0ll
作为文字。
当我在clang
中编译时,我收到以下警告:
varby.cpp:12:63:警告:format 指定类型"无符号长长",但参数的类型为"int" [-Wformat]
这可能是问题的根源,因为0
参数类型错误。
通过根据需要将其设置为长来修复它:
printf("but output is:n%llx %llx %llx %llx %llxn",d,c,b,0LL,a);
一个好的经验法则是,百万分之一的错误是由编译器引起的,所以总是假设这是你的错,直到可以证明不是这样。在这种情况下,打开更多警告或尝试在另一个编译器中重现它会发现问题。
- C++声明模板参数阴影模板参数错误
- 为什么 CRTP 模板C++给出无效参数错误?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 参数错误可能与类型不匹配有关?
- printf 和 strftime 的参数错误无效
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 如何修复"没有重载函数需要 2 个参数"错误C++
- 术语不计算为函数采用 1 个参数错误?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- cudaFreeHost() 无效参数错误
- 精神语法不会编译:函数模板参数错误?
- C 功能具有参考参数错误的迭代器错误.寻求解释
- 使用getDefaultCommConfig使用无效的参数错误
- C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配
- 使用SWIG生成的Python库时,向量分配器参数错误
- 错误C2664:无法转换参数错误
- C 作为参数错误的功能
- 命令行参数错误
- 增强Python参数错误