public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/25702]  New: feature request: generate a warning for sizeof on a pointer
@ 2006-01-06 22:09 meklund at cisco dot com
  2006-01-06 22:12 ` [Bug c/25702] " pinskia at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 13+ messages in thread
From: meklund at cisco dot com @ 2006-01-06 22:09 UTC (permalink / raw)
  To: gcc-bugs

One common mistake in C is to do sizeof(x) where x is a pointer and
what was really wanted was sizeof(*x).  Similar to requiring
parentheses around assignments in conditionals, I'd like an optional
warning to be added when sizeof is done on a variable that is a
pointer.  For example, in the code below, the first sizeof would
give a warning.  To prevent this warning, sizeof(char *) can be
used instead.

void fu (void)
{   
    char *x;
    printf(" x %d\n", sizeof(x));
    printf("char * %d\n", sizeof(char *));
    printf("char * %d\n", sizeof("hello world"));
}

I've done a proof of concept for gcc 3.3 as below.  I haven't done             
extensive testing and I didn't make the warning optional.

*** c-parse.y   2005/12/05 22:19:42     1.1
--- c-parse.y   2005/12/05 22:54:43
***************
*** 494,502 ****
--- 494,506 ----  
                  if (TREE_CODE ($2) == COMPONENT_REF
                      && DECL_C_BIT_FIELD (TREE_OPERAND ($2, 1)))
                    error ("`sizeof' applied to a bit-field");
+                   warning("`sizeof' reference #1");
+                   if (TREE_CODE (TREE_TYPE ($2)) == POINTER_TYPE)
+                     warning("`sizeof' applied to a pointer variable");
                  $$ = c_sizeof (TREE_TYPE ($2)); }
        | sizeof '(' typename ')'  %prec HYPERUNARY
                { skip_evaluation--;
+                   warning("`sizeof' reference #2");
                  $$ = c_sizeof (groktypename ($3)); }
        | alignof unary_expr  %prec UNARY
                { skip_evaluation--;


-- 
           Summary: feature request: generate a warning for sizeof on a
                    pointer
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: meklund at cisco dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25702



^ permalink raw reply	[flat|nested] 13+ messages in thread
[parent not found: <bug-25702-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2015-08-12 12:47 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-06 22:09 [Bug c/25702] New: feature request: generate a warning for sizeof on a pointer meklund at cisco dot com
2006-01-06 22:12 ` [Bug c/25702] " pinskia at gcc dot gnu dot org
2006-01-06 22:24 ` meklund at cisco dot com
2006-01-17 15:36 ` meklund at cisco dot com
2007-11-28 17:34 ` manu at gcc dot gnu dot org
2007-11-28 19:43 ` meklund at cisco dot com
2008-01-26 12:27 ` rguenth at gcc dot gnu dot org
2008-11-14 23:52 ` pinskia at gcc dot gnu dot org
2009-08-08 22:35 ` steven at gcc dot gnu dot org
     [not found] <bug-25702-4@http.gcc.gnu.org/bugzilla/>
2013-01-04 15:38 ` forgcc at calmarius dot net
2013-01-04 15:55 ` jakub at gcc dot gnu.org
2015-08-12 11:12 ` mpolacek at gcc dot gnu.org
2015-08-12 12:47 ` meklund at cisco dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).