Skip to content

Commit ab714e6

Browse files
committed
c-family, tree: Allow nonstring attribute on multidimensional arrays [PR117178]
As requested in the PR117178 thread, the following patch allows nonstring attribute also on multi-dimensional arrays (with cv char/signed char/unsigned char as innermost element type) and pointers to such multi-dimensional arrays or pointers to single-dimensional cv char/signed char/unsigned char arrays. Given that (unfortunately) nonstring is a decl attribute rather than type attribute, I think restricting it to single-dimensional arrays makes no sense, even multi-dimensional ones can be used for storage of non-nul terminated strings. I really don't know what the kernel plans are, whether they'll go with -Wno-unterminated-string-initialization added in Makefiles, or whether the plan is to use nonstring attributes to quiet the warning. In the latter case, some of the nonstring attributes will need to be conditional on gcc version, because gcc before this patch will reject it on multidimensional arrays. 2025-03-08 Jakub Jelinek <[email protected]> PR c/117178 gcc/ * tree.cc (get_attr_nonstring_decl): Look through all ARRAY_REFs, not just one and handle COMPONENT_REF and MEM_REF after skipping those rather than only when there wasn't ARRAY_REF. Formatting fix. gcc/c-family/ * c-attribs.cc (handle_nonstring_attribute): Allow the attribute also on multi-dimensional arrays with char/signed char/unsigned char element type or pointers to such single and multi-dimensional arrays. gcc/testsuite/ * c-c++-common/attr-nonstring-7.c: Remove one xfail. * c-c++-common/attr-nonstring-9.c: New test. * c-c++-common/attr-nonstring-10.c: New test. * c-c++-common/attr-nonstring-11.c: New test. * c-c++-common/attr-nonstring-12.c: New test. * c-c++-common/attr-nonstring-13.c: New test. * c-c++-common/attr-nonstring-14.c: New test. * c-c++-common/attr-nonstring-15.c: New test. * c-c++-common/attr-nonstring-16.c: New test.
1 parent 6229689 commit ab714e6

11 files changed

+1237
-11
lines changed

gcc/c-family/c-attribs.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -5117,8 +5117,9 @@ handle_nonstring_attribute (tree *node, tree name, tree ARG_UNUSED (args),
51175117
if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
51185118
{
51195119
/* Accept the attribute on arrays and pointers to all three
5120-
narrow character types. */
5121-
tree eltype = TREE_TYPE (type);
5120+
narrow character types, including multi-dimensional arrays
5121+
or pointers to them. */
5122+
tree eltype = strip_array_types (TREE_TYPE (type));
51225123
eltype = TYPE_MAIN_VARIANT (eltype);
51235124
if (eltype == char_type_node
51245125
|| eltype == signed_char_type_node
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Test to exercise attribute "nonstring".
2+
{ dg-do compile }
3+
{ dg-options "-O2 -Wattributes -Wstringop-truncation -ftrack-macro-expansion=0" } */
4+
5+
#define ATTR(list) __attribute__ (list)
6+
#define NONSTR ATTR ((nonstring))
7+
#define strncpy(d, s, n) (__builtin_strncpy ((d), (s), (n)), sink (d))
8+
9+
void sink (void*);
10+
11+
/* Global string with an known bound. */
12+
extern char gns3[][3] NONSTR;
13+
14+
/* Global non-string pointers. */
15+
extern NONSTR char (*pns_1)[1];
16+
extern char (* NONSTR pns_2)[2];
17+
extern char (*pns_3)[3] NONSTR;
18+
19+
struct MemArrays
20+
{
21+
NONSTR char ma3[2][3];
22+
char NONSTR ma4[3][4];
23+
char ma5[4][5] NONSTR;
24+
};
25+
26+
27+
void test_array (const char *s, unsigned n)
28+
{
29+
const char s7[] = "1234567";
30+
31+
strncpy (gns3[2], "", 0);
32+
strncpy (gns3[2], "a", 1);
33+
strncpy (gns3[2], "a", 2);
34+
strncpy (gns3[2], "a", 3);
35+
strncpy (gns3[2], "ab", 3);
36+
strncpy (gns3[2], "abc", 3);
37+
}
38+
39+
40+
void test_pointer (const char *s, unsigned n)
41+
{
42+
const char s7[] = "1234567";
43+
44+
strncpy (*pns_1, "a", 1);
45+
strncpy (*pns_2, "ab", 2);
46+
strncpy (*pns_3, "abc", 3);
47+
strncpy (*pns_3, s7, 3);
48+
49+
strncpy (*pns_1, s, 1);
50+
strncpy (*pns_2, s, 1);
51+
strncpy (*pns_3, s, 1);
52+
53+
strncpy (*pns_1, s, n);
54+
strncpy (*pns_2, s, n);
55+
strncpy (*pns_3, s, n);
56+
}
57+
58+
59+
void test_member_array (struct MemArrays *p, const char *s, unsigned n)
60+
{
61+
const char s7[] = "1234567";
62+
63+
strncpy (p->ma3[1], "", 0);
64+
strncpy (p->ma3[1], "a", 1);
65+
strncpy (p->ma4[2], "ab", 2);
66+
strncpy (p->ma5[3], "abc", 3);
67+
68+
strncpy (p->ma3[1], s, 1);
69+
strncpy (p->ma4[2], s, 1);
70+
strncpy (p->ma5[3], s, 1);
71+
72+
strncpy (p->ma3[1], s7, n);
73+
strncpy (p->ma4[2], s7, n);
74+
strncpy (p->ma5[3], s7, n);
75+
}

0 commit comments

Comments
 (0)