https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118095
Bug ID: 118095
Summary: nonstring attribute cannot be applied to array of char
arrays
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: kees at outflux dot net
Target Milestone: ---
While this is legal:
char tag[4] __attribute__((nonstring));
this is not:
char tags[10][4] __attribute__((nonstring));
Given both existing checks for passing nonstrings to str functions and the
coming fixes for -Wunterminated-string-initialization (see bug #117178), there
needs to be a way to apply nonstring to these constructs.
This patch makes it possible to apply:
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 7fd480e6d41b..42a52bcd48da 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -5089,7 +5089,7 @@ handle_nonstring_attribute (tree *node, tree name, tree
ARG_UNUSED (args),
{
/* Accept the attribute on arrays and pointers to all three
narrow character types. */
- tree eltype = TREE_TYPE (type);
+ tree eltype = strip_array_types (type);
eltype = TYPE_MAIN_VARIANT (eltype);
if (eltype == char_type_node
|| eltype == signed_char_type_node
But it seems to apply to the top of the multidimensional array, not the
individual char arrays within it:
struct foo {
char tag[4] __attribute__((nonstring));
char tags[10][4] __attribute__((nonstring));
int a;
};
int len(struct foo *p)
{
return strlen(p->tag) +
strlen(p->tags[2]);
}
The above only warns for "tag", not for the character array at "tags[2]".
Forcing it to use the first char array through the top-level object name does
see the attribute, though:
strlen((char *)p->tags);
I'm not sure the correct way to mark the lower char arrays with nonstring
through handle_nonstring_attribute().