Bug 67793 - Missing warning for C99 static array dimension mismatch
Summary: Missing warning for C99 static array dimension mismatch
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on: 50584
Blocks:
  Show dependency treegraph
 
Reported: 2015-10-01 01:28 UTC by rusty
Modified: 2016-09-03 23:44 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.3, 5.3.0, 6.0
Last reconfirmed: 2016-05-02 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rusty 2015-10-01 01:28:28 UTC
It would be great if GCC warned on this:

extern int func_(char x[32]);
int main(int argc, char *argv[])
{
	char p[8];

	return func(p);
}

Failing that, if __builtin_object_size() returned a compile-time constant where possible, we could do this (with __builtin_constant_p):

#define SIZE_CHECK(v,min) ({static_assert(__builtin_object_size((v), 0) >= (min), "too small"); (v)})

extern int func_(char x[32]);
#define func(x) func_(SIZE_CHECK((x),32))
Comment 1 Markus Trippelsdorf 2015-10-01 06:41:57 UTC
In C99 you can add "static" to the array function argument:

extern int func(char x[static 32]);

But unfortunately gcc doesn't warn for this case yet.
Clang already does:

markus@x4 /tmp % clang -c test.i
test.i:5:10: warning: array argument is too small; contains 8 elements, callee requires at least 32 [-Warray-bounds]
  return func(p);
         ^    ~
test.i:1:22: note: callee declares array parameter as static here
extern int func(char x[static 32]);
                     ^~~~~~~~~~~~
1 warning generated.
Comment 2 Martin Sebor 2016-05-03 00:04:55 UTC
See also bug 45840 for a (somewhat remotely) related request involving pointers to arrays.

I also think that warning on the test case in comment #1 would be a useful enhancement.  Not having tested it, though, I would worry that issuing a warning on the test case in comment #0 by default (or with -Wall) would cause too many false positives.  I suppose it could be something to try.
Comment 3 Martin Sebor 2016-09-03 23:44:34 UTC
(In reply to Markus Trippelsdorf from comment #1)
> In C99 you can add "static" to the array function argument:
> 
> extern int func(char x[static 32]);
> 
> But unfortunately gcc doesn't warn for this case yet.

Bug 50584 that tracks this feature request.