#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 is throwing this warning. How to fix this without using -Wformat-truncation= option?

cpath is a character with 4096 size

1

There are 1 best solutions below

0
phuclv On

As cpath can be maximum 4096 characters long, appending /name0 to the end of it inherently create a string that can be longer than 4096 characters and can't fit into head_list->cname

If you're sure that cpath can't be longer than 4090 characters then tell gcc to turn off warning for that specific line

#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