mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
C++: Use appropriate sizeof in CWE-119 memcpy tests
Signed-off-by: Mingjie Shen <shen497@purdue.edu>
This commit is contained in:
@@ -30,10 +30,10 @@ void test2()
|
|||||||
char *smallbuffer = (char *)malloc(sizeof(char) * 10);
|
char *smallbuffer = (char *)malloc(sizeof(char) * 10);
|
||||||
char *bigbuffer = (char *)malloc(sizeof(char) * 20);
|
char *bigbuffer = (char *)malloc(sizeof(char) * 20);
|
||||||
|
|
||||||
memcpy(bigbuffer, smallbuffer, 10); // GOOD
|
memcpy(bigbuffer, smallbuffer, sizeof(char) * 10); // GOOD
|
||||||
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
|
memcpy(bigbuffer, smallbuffer, sizeof(char) * 20); // BAD: over-read
|
||||||
memcpy(smallbuffer, bigbuffer, 10); // GOOD
|
memcpy(smallbuffer, bigbuffer, sizeof(char) * 10); // GOOD
|
||||||
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
|
memcpy(smallbuffer, bigbuffer, sizeof(char) * 20); // BAD: over-write
|
||||||
|
|
||||||
free(bigbuffer);
|
free(bigbuffer);
|
||||||
free(smallbuffer);
|
free(smallbuffer);
|
||||||
@@ -46,10 +46,10 @@ void test3()
|
|||||||
smallbuffer = new char[10];
|
smallbuffer = new char[10];
|
||||||
bigbuffer = new char[20];
|
bigbuffer = new char[20];
|
||||||
|
|
||||||
memcpy(bigbuffer, smallbuffer, 10); // GOOD
|
memcpy(bigbuffer, smallbuffer, sizeof(char[10])); // GOOD
|
||||||
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
|
memcpy(bigbuffer, smallbuffer, sizeof(char[20])); // BAD: over-read
|
||||||
memcpy(smallbuffer, bigbuffer, 10); // GOOD
|
memcpy(smallbuffer, bigbuffer, sizeof(char[10])); // GOOD
|
||||||
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
|
memcpy(smallbuffer, bigbuffer, sizeof(char[20])); // BAD: over-write
|
||||||
|
|
||||||
delete [] bigbuffer;
|
delete [] bigbuffer;
|
||||||
delete [] smallbuffer;
|
delete [] smallbuffer;
|
||||||
|
|||||||
Reference in New Issue
Block a user