Skip to content

Commit b293dfe

Browse files
committed
C++: Fix CWE-119 memcpy tests
sizeof(pointer) only gives the pointer size, not the buffer size, so use explicit 10/20 lengths in tests.cpp and update OverflowBuffer.expected to accept the resulting memcpy diagnostics. Signed-off-by: Mingjie Shen <shen497@purdue.edu>
1 parent aa3000d commit b293dfe

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/OverflowBuffer.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
| overflowdestination.cpp:46:2:46:7 | call to memcpy | This 'memcpy' operation accesses 128 bytes but the $@ is only 64 bytes. | overflowdestination.cpp:40:7:40:10 | dest | destination buffer |
22
| tests.cpp:23:2:23:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:19:7:19:17 | smallbuffer | source buffer |
33
| tests.cpp:25:2:25:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:19:7:19:17 | smallbuffer | destination buffer |
4+
| tests.cpp:34:2:34:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:30:30:30:35 | call to malloc | source buffer |
5+
| tests.cpp:36:2:36:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:30:30:30:35 | call to malloc | destination buffer |
6+
| tests.cpp:50:2:50:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:46:16:46:27 | new[] | source buffer |
7+
| tests.cpp:52:2:52:7 | call to memcpy | This 'memcpy' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:46:16:46:27 | new[] | destination buffer |
48
| tests.cpp:172:23:172:31 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:170:17:170:41 | {...} | array |
59
| tests.cpp:176:23:176:30 | access to array | This array indexing operation accesses byte offset 31 but the $@ is only 24 bytes. | tests.cpp:170:17:170:41 | {...} | array |
610
| tests.cpp:222:3:222:8 | call to memset | This 'memset' operation accesses 33 bytes but the $@ is only 32 bytes. | tests.cpp:214:8:214:14 | buffer1 | destination buffer |

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/tests.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ void test2()
3030
char *smallbuffer = (char *)malloc(sizeof(char) * 10);
3131
char *bigbuffer = (char *)malloc(sizeof(char) * 20);
3232

33-
memcpy(bigbuffer, smallbuffer, sizeof(smallbuffer)); // GOOD
34-
memcpy(bigbuffer, smallbuffer, sizeof(bigbuffer)); // BAD: over-read [NOT DETECTED]
35-
memcpy(smallbuffer, bigbuffer, sizeof(smallbuffer)); // GOOD
36-
memcpy(smallbuffer, bigbuffer, sizeof(bigbuffer)); // BAD: over-write [NOT DETECTED]
33+
memcpy(bigbuffer, smallbuffer, 10); // GOOD
34+
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
35+
memcpy(smallbuffer, bigbuffer, 10); // GOOD
36+
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
3737

3838
free(bigbuffer);
3939
free(smallbuffer);
@@ -46,10 +46,10 @@ void test3()
4646
smallbuffer = new char[10];
4747
bigbuffer = new char[20];
4848

49-
memcpy(bigbuffer, smallbuffer, sizeof(smallbuffer)); // GOOD
50-
memcpy(bigbuffer, smallbuffer, sizeof(bigbuffer)); // BAD: over-read [NOT DETECTED]
51-
memcpy(smallbuffer, bigbuffer, sizeof(smallbuffer)); // GOOD
52-
memcpy(smallbuffer, bigbuffer, sizeof(bigbuffer)); // BAD: over-write [NOT DETECTED]
49+
memcpy(bigbuffer, smallbuffer, 10); // GOOD
50+
memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read
51+
memcpy(smallbuffer, bigbuffer, 10); // GOOD
52+
memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write
5353

5454
delete [] bigbuffer;
5555
delete [] smallbuffer;

0 commit comments

Comments
 (0)