Conversation

TIL you can leverage some compiler bounds checking in C by specifying array/buffer length in the function parameters, like `char* eightcc_to_str(uint64_t word, char buf[9])`

unfortunately it doesn't work for the return value

2
0
0

i.e. if you write to buf[8] inside of this function but you pass a buf[8] (or smaller) from somewhere else, the compiler will warn with Wstringop-overflow

2
0
0
@mntmn Are you surely sure this is good idea and is not passing arrays by value?
0
0
0

@mntmn that still allows passing in NULL, because of course it does – though can disallow that by char buf[static 9], e.g.

foo.c:6:9: warning: argument 1 null where non-null expected [-Wnonnull]
    6 |         nya(NULL);
      |         ^~~
foo.c:3:12: note: in a call to function ‘nya’ declared ‘nonnull’
    3 | extern int nya(char buf[static 9]);

now something else really cool we can also do, using variably-modified types (VMTs), is void foo(size_t count, int an_array[static count]);

then whenever possible stringop-overflow will try checking if the passed in size value matches

(see also, runtime checks! in https://uecker.codeberg.page/2025-07-09.html)

0
1
0

@mntmn
what would it do for a return value?

Would it warn when you return a pointer to an array smaller than 8, and when the caller tries to index further than 8 into it?

1
0
0