繁体   English   中英

警告:“/name0”指令 output 可能被截断,将 10 个字节写入大小介于 1 和 4096 之间的区域 [-Wformat-truncation=]

[英]warning: ‘/name0’ directive output may be truncated writing 10 bytes into a region of size between 1 and 4096 [-Wformat-truncation=]

#DEFINE LENGTH 4096

warning: ‘/name0’ directive output may be truncated writing 10 bytes into a region 
of size between 1 and 4096 [-Wformat-truncation=]
 snprintf(head_list->cname, LENGTH, "%s/name0", cpath);
                                       ^~~~~~~~~~

gcc 发出此警告。 如何在不使用-Wformat-truncation=选项的情况下解决这个问题?

cpath是一个4096大小的字符

由于cpath的最大长度为 4096 个字符,因此将/name0附加到它的末尾会固有地创建一个可以超过 4096 个字符并且不能放入head_list->cname的字符串

如果您确定cpath不能超过 4090 个字符,请告诉 gcc关闭该特定行的警告

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
snprintf(head_list->cname, LENGTH, "%s/name0", cpath);
assert(strlen(cpath) <= 4090);
#pragma GCC diagnostic pop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM