gpt4 book ai didi

c - 警告 : call to __builtin___strncat_chk might overflow destination buffer [enabled by default] In function ‘strncat’

转载 作者:行者123 更新时间:2023-11-30 16:23:17 25 4
gpt4 key购买 nike

我在编译“C”语言文件时收到此警告。

In function ‘strncat’,inlined from ‘O281DC3F563F92003x’ at util.c:817:13:
/usr/arm-linux-gnueabihf/include/bits/string3.h:152:3: warning: call to
__builtin___strncat_chk might overflow destination buffer [enabled by
default]

In function ‘strncat’,inlined from ‘UFE191C0002FB606Eb’ at util.c:3231:25:
/usr/arm-linux-gnueabihf/include/bits/string3.h:152:3: warning: call to
__builtin___strncat_chk might overflow destination buffer [enabled by
default]

In function ‘strncat’,

如何删除这些警告?

最佳答案

正如 this answer 中提到的,您可以轻松地将 strncat 的用法替换为 snprintf,它接受缓冲区站点的参数并使其可以安全使用。

strncat 示例:

#define BUF_SIZE 32
char buf[BUF_SIZE];

strncpy(buf, "foo", sizeof(buf) -1);
strncat(buf, "bar", sizeof(buf) - strlen(buf) -1);

更安全的snprintf示例:

#define BUF_SIZE 32
char buf[BUF_SIZE];

snprintf(buf, sizeof(buf), "%s%s", "foo", "bar");

此方法的缺点是,如果您确实想连接为以下内容,则必须使用中间缓冲区:

snprintf(buf, sizeof(buf), "%s%s", buf, "bar");

导致未定义的行为。

关于c - 警告 : call to __builtin___strncat_chk might overflow destination buffer [enabled by default] In function ‘strncat’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54037668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com