Title
sizeof(sizeof(...)) should not be used
Description
<p> A call to <code>sizeof(sizeof(...))</code> is equivalent to <code>sizeof(size_t)</code>, and therefore indicates a misuse of the <code>sizeof</code> construct. </p> <p>The following code:</p> <pre> #include <string.h> int main(int argc, char* argv[]) { char buffer[42]; char buffer2[sizeof(sizeof(buffer))]; /* Non-Compliant - a single sizeof() was intended */ memcpy(buffer, "Hello, world!", strlen("Hello, world!")+1); memcpy(buffer2, buffer, sizeof(buffer)); /* Buffer overflow */ return 0; } </pre> <p>should be refactored into:</p> <pre> #include <string.h> int main(int argc, char* argv[]) { char buffer[42]; char buffer2[sizeof(buffer)]; /* Compliant */ memcpy(buffer, "Hello, world!", strlen("Hello, world!")+1); memcpy(buffer2, buffer, sizeof(buffer)); return 0; } </pre>
Message
Remove the inner sizeof.
Severity
Critical
In Sonar way?
Yes
- implements
-
RSPEC-1913 "sizeof(sizeof(...))" should not be used
- Deprecated