Bug 6940

Summary: taking sizeof array parameter should trigger a warning
Product: gcc Reporter: marco.franzen
Component: cAssignee: Marek Polacek <mpolacek>
Status: RESOLVED FIXED    
Severity: enhancement CC: gcc-bugs, nathan
Priority: P3 Keywords: diagnostic
Version: 3.1   
Target Milestone: 5.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2005-12-10 05:47:17

Description marco.franzen 2002-06-05 14:16:02 UTC
Neither the C nor the C++ front-end of GCC 3.1 issues a warning for this.  (I tried -W -Wall for both.)

I know the generated code is absolutely correct, but a warning would be in order (and should probably be on by default, given it will be rare outside obfuscation contests).

Release:
gcc-3.1

How-To-Repeat:
int func(int array[], int* pointer)
{
        int n1 = sizeof(array);   // no, it isn't
        int n2 = sizeof(pointer);
	return n1 - n2;
}

int main()
{
	int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	return func (v, v);
}
Comment 1 Nathan Sidwell 2002-09-14 15:11:19 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: this seems like a sensible warning
Comment 2 Marek Polacek 2014-06-26 15:37:06 UTC
On it.
Comment 3 Marek Polacek 2014-07-06 19:00:42 UTC
Author: mpolacek
Date: Sun Jul  6 19:00:10 2014
New Revision: 212312

URL: https://gcc.gnu.org/viewcvs?rev=212312&root=gcc&view=rev
Log:
	PR c/6940
	* doc/invoke.texi: Document -Wsizeof-array-argument.
c-family/
	* c.opt (Wsizeof-array-argument): New option.
c/
	* c-decl.c (grokdeclarator): Set C_ARRAY_PARAMETER.
	* c-tree.h (C_ARRAY_PARAMETER): Define.
	* c-typeck.c (c_expr_sizeof_expr): Warn when using sizeof on an array
	function parameter.
cp/
	* cp-tree.h (DECL_ARRAY_PARAMETER_P): Define.
	* decl.c (grokdeclarator): Set DECL_ARRAY_PARAMETER_P.
	* typeck.c (cxx_sizeof_expr): Warn when using sizeof on an array
	function parameter.
testsuite/
	* c-c++-common/Wsizeof-pointer-memaccess1.c: Use
	-Wno-sizeof-array-argument.
	* c-c++-common/Wsizeof-pointer-memaccess2.c: Likewise.
	* g++.dg/warn/Wsizeof-pointer-memaccess-1.C: Likewise.
	* gcc.dg/Wsizeof-pointer-memaccess1.c: Likewise.
	* g++.dg/torture/Wsizeof-pointer-memaccess1.C: Likewise.
	* g++.dg/torture/Wsizeof-pointer-memaccess2.C: Likewise.
	* gcc.dg/torture/Wsizeof-pointer-memaccess1.c: Likewise.
	* c-c++-common/sizeof-array-argument.c: New test.
	* gcc.dg/vla-5.c: Add dg-warnings.
../libgomp/
	* testsuite/libgomp.c/appendix-a/a.29.1.c (f): Add dg-warnings.

Added:
    trunk/gcc/testsuite/c-c++-common/sizeof-array-argument.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c.opt
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-decl.c
    trunk/gcc/c/c-tree.h
    trunk/gcc/c/c-typeck.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c
    trunk/gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c
    trunk/gcc/testsuite/g++.dg/torture/Wsizeof-pointer-memaccess1.C
    trunk/gcc/testsuite/g++.dg/torture/Wsizeof-pointer-memaccess2.C
    trunk/gcc/testsuite/g++.dg/warn/Wsizeof-pointer-memaccess-1.C
    trunk/gcc/testsuite/gcc.dg/Wsizeof-pointer-memaccess1.c
    trunk/gcc/testsuite/gcc.dg/torture/Wsizeof-pointer-memaccess1.c
    trunk/gcc/testsuite/gcc.dg/vla-5.c
    trunk/libgomp/ChangeLog
    trunk/libgomp/testsuite/libgomp.c/appendix-a/a.29.1.c
Comment 4 Marek Polacek 2014-07-06 19:02:09 UTC
Fixed.