fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

11K
active users

Kees Cook :tux:

Dear C Lazy Web,

How do I define an array of nonstring char arrays?

just a char array, valid:
char str[4] __attribute__((nonstring)):

array of char arrays, cursed:
char multi[10][4] __attribute__((nonstring));

I've tried typedefs and moving the attribute around. No luck. What am I missing?

Here's a godbolt:
godbolt.org/z/4Mb61heG1
I'd want to see a warning for both strlen() instances...

godbolt.orgCompiler Explorer - C (x86-64 gcc (trunk)) struct foo { char str[8] __attribute__((nonstring)); char multi[3][8] __attribute__((nonstring)); int a; }; int len(struct foo *p) { return strlen(p->str) + strlen(p->multi[2]); }

@kees I think this does not work because the attribute does not apply to types and also not to objects with a type which is not a char arrays. The workaround by @vegard makes sense.

@uecker @vegard Maybe that's the solution: the attribute needs to be able to be applied to multidimensional char arrays. Without this, I can't fully apply my proposed fix of -Wunterminated-string-initialization to the Linux kernel (which has multidimensional arrays of C strings in some places and multidimensional arrays of non-strings in other places).
gcc.gnu.org/bugzilla/show_bug.
git.kernel.org/pub/scm/linux/k

gcc.gnu.org117178 – -Wunterminated-string-initialization should ignore trailing NUL byte for nonstring char arrays