[LLVMbugs] [Bug 17418] New: No warning for implicit void* cast

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Sep 30 15:58:38 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=17418

            Bug ID: 17418
           Summary: No warning for implicit void* cast
           Product: clang
           Version: 3.3
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: j.david.lists at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

With -Weverything enabled, the following code does not generate a warning:

-------------------------- Cut Here ----------------------------
#include <stdlib.h>

int main() {
    int *x;

    x = malloc(sizeof(char));

    return 0;

}
-------------------------- Cut Here ----------------------------

$ clang -std=c11 -Weverything -g -O -c warn.c
$ 

Under gcc, a warning can be produced if -Wc++-compat is enabled:

$ gcc -Wc++-compat -g -O -c warn.c
warn.c: In function 'main':
warn.c:7: warning: request for implicit conversion from 'void *' to 'int *' not
permitted in C++

Gcc's reason for emitting the warning is that casting from void * to another
pointer type is allowed in C, but is forbidden in C++.  

However, this practice is widely discouraged in C as well, due to the
possibility of disaster (as shown in the example above).  E.g. see CERT's
recommendation that the result of malloc should always be immediately cast:

https://www.securecoding.cert.org/confluence/display/seccode/MEM02-C.+Immediately+cast+the+result+of+a+memory+allocation+function+call+into+a+pointer+to+the+allocated+type

In Gcc this warning must be individually enabled, it is not part of -Wall
-Wextra or -pedantic.  

The same behavior for clang would be highly desirable, as although this allows
the compiler to catch errors, for every site where explicitly casting void* is
required, there is another site where casting the result of malloc is verboten
to avoid hiding the case stdlib.h wasn't included.  (A strong argument can be
made that adding a warning won't affect the latter site since even -Wall
already generates a warning for omitting stdlib.h in that case.)

Hence a -Wimplicit-void-cast flag that generates warnings in this case would be
a welcome addition to the clang frontend, particularly if feature-parity with
GCC's -Wc++-compat flag is not desirable for other reasons.

Thank you for your consideration!

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130930/5cd015bd/attachment.html>


More information about the llvm-bugs mailing list