首页 > 解决方案 > 带有可变参数的 C printf 说明符。在哪一点上未定义的行为有问题?

问题描述

https://godbolt.org/z/qZVO3a

这是我看到的警告的最小复制。显然 UB 可能很糟糕,但我认为虽然以下许多情况都可以,但有一些非常讨厌的用途,我需要确定哪些需要采取纠正措施。

#include <stdarg.h>
#include <stdio.h>
#include <limits.h>

typedef struct _thing {

    char  first[4];
    char  second[10];
    char  last[111];
}THING;


void custom_printf(char* _format, ...) __attribute__((format(printf, 1,2)));
void custom_printf(char* _format, ...) 
{
    // get buffer from some source
    char buffer[1024];
    va_list ap;
    va_start(ap, _format);
    vsnprintf(buffer, 1024, _format, ap);
    va_end(ap);
    // use buffer for some purpose

}

int main(){

    custom_printf("HI THERE%d");
    custom_printf("HI THERE", 1);
    custom_printf("val: %d", (void*)0);
    custom_printf("val: %p", 0);
    custom_printf("val: %lld", 1);
    custom_printf("val: %s", (THING){"A", "AA", "CCCC"});
    custom_printf("val: %0.30s","HI");
    custom_printf("val: %d",LLONG_MAX);
}

看到的警告包括:


<source>: In function 'main':

<source>:26:5: warning: format '%d' expects a matching 'int' argument [-Wformat]

<source>:27:5: warning: too many arguments for format [-Wformat-extra-args]

<source>:28:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'void *' [-Wformat]

<source>:29:5: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'int' [-Wformat]

<source>:30:5: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int' [-Wformat]

<source>:31:5: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'THING' [-Wformat]

<source>:32:5: warning: '0' flag used with '%s' gnu_printf format [-Wformat]

<source>:33:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat]

<source>:34:1: warning: control reaches end of non-void function [-Wreturn-type]

Compiler returned: 0

据我了解,上面有很多 UB 的味道。环顾四周后,我发现我应该解决上述问题。现在我想最终解决所有问题,但现在我的好奇心让我想知道哪个是最糟糕的情况。我会假设第一种情况是我没有传递足够多的物品。

据我了解,在上面我有:

  1. 弹出不存在的堆栈
  2. 从堆栈中弹出的内容不够多
  3. 用前导零填充字符串
  4. 将整数转换为指针
  5. 铸造一个可以大小写的结构

综上所述,我相当肯定任何从堆栈中弹出但不存在的东西都会导致最坏的情况。但我也想知道其他严重的情况是什么。

标签: cgcc-warninggcc4.7

解决方案


在哪一点上未定义的行为有问题?

所有的UB都是有问题的。

识别特定编译器版本的 UB 效果对于解决问题有一些好处。然而,人们永远不应该依赖这种 UB 效应来持续存在。

我的回答通常基于 C,而不是 gcc 4.7。


考虑到对象不一定使用相同的机制跨类型传递。 相关真实示例float/double通过通常的堆栈传入 FP 堆栈和其他类型。 printf("%llx\n", 1.234);可能会严重失败,即使传递的大小是 8 和 8 是预期的,但它们在不同的地方。指针类型和整数之间可能会出现类似的差异(尽管这听起来像是一个独角兽平台)。


将 UB 留在代码中会导致开发效率低下。
考虑一下,如果确实找到了一些在特定情况下运行良好的 UB,那么下一个编译或版本可能会呈现不同的结果。通过修复,您可以节省时间,而不是在代码审查期间试图解释“这个 UB 没问题,我知道我测试过它”。还可以节省时间,无需找到一种方法来消除这个“好”UB 的警告。必须维护您的 UB 代码的编程团队会对之前的编码员说些坏话。


UB 缺少匹配的参数。

custom_printf("HI THERE%d");
<source>:26:5: warning: format '%d' expects a matching 'int' argument [-Wformat]

不是UB。额外的参数是可以的,但可能是编码错误 - 因此是警告。@melpomene

custom_printf("HI THERE", 1);
<source>:27:5: warning: too many arguments for format [-Wformat-extra-args]

UB。 int并且void *可能有不同的大小、合法值和函数传递机制,

custom_printf("val: %d", (void*)0);
<source>:28:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'void *' [-Wformat]

UB。与第 28 行相同

custom_printf("val: %p", 0);
<source>:29:5: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'int' [-Wformat]

UB。 int并且long long可能有不同的尺寸和功能传递机制,

custom_printf("val: %lld", 1);
<source>:30:5: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int' [-Wformat]

UB。类型在大小、合法值和函数传递机制上可能不同,

custom_printf("val: %s", (THING){"A", "AA", "CCCC"});
<source>:31:5: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'THING' [-Wformat]

UB:无效的标准说明符%0.30s,任何事情都可能发生。在为这个非标准说明符定义行为的选择系统上表现良好。

custom_printf("val: %0.30s","HI");
<source>:32:5: warning: '0' flag used with '%s' gnu_printf format [-Wformat]

UB 像第 30 行

custom_printf("val: %d",LLONG_MAX);
<source>:33:5: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat]

不是 UB 和main(). 如果调用代码使用返回值,则通常只有函数的 UB 问题。然而main(),特殊之处在于代码的行为就像 areturn 0;在结尾 - 如果该函数不以 a 结尾return

<source>:34:1: warning: control reaches end of non-void function [-Wreturn-type]

推荐阅读