<html>
<head>
<base href="http://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - No warning for implicit void* cast"
href="http://llvm.org/bugs/show_bug.cgi?id=17418">17418</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>No warning for implicit void* cast
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.3
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Frontend
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>j.david.lists@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>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:
<a href="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">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</a>
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!</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>