Skip to content

Commit 3977689

Browse files
authored
Merge pull request #15117 from jketema/buffer
C++: Only consider the maximum buffer size for badly bounded write
2 parents 1ea1130 + 0b1b1be commit 3977689

File tree

4 files changed

+7
-5
lines changed

4 files changed

+7
-5
lines changed

cpp/ql/src/Security/CWE/CWE-120/BadlyBoundedWrite.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import semmle.code.cpp.security.BufferWrite
2424
from BufferWrite bw, int destSize
2525
where
2626
bw.hasExplicitLimit() and // has an explicit size limit
27-
destSize = getBufferSize(bw.getDest(), _) and
27+
destSize = max(getBufferSize(bw.getDest(), _)) and
2828
bw.getExplicitLimit() > destSize // but it's larger than the destination
2929
select bw,
3030
"This '" + bw.getBWDesc() + "' operation is limited to " + bw.getExplicitLimit() +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `cpp/badly-bounded-write` query could report false positives when a pointer was first initialized with a literal and later assigned a dynamically allocated array. These false positives now no longer occur.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
| tests2.cpp:59:3:59:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 2 bytes. |
2-
| tests2.cpp:63:3:63:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 3 bytes. |
31
| tests.c:43:3:43:10 | call to snprintf | This 'call to snprintf' operation is limited to 111 bytes but the destination is only 110 bytes. |
42
| tests.c:46:3:46:10 | call to snprintf | This 'call to snprintf' operation is limited to 111 bytes but the destination is only 110 bytes. |

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/tests2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ void test3() {
5656
dest1 = (char*)malloc(sizeof(src));
5757
if (!dest1)
5858
return;
59-
snprintf(dest1, sizeof(src), "%s", src); // GOOD [FALSE POSITIVE]
59+
snprintf(dest1, sizeof(src), "%s", src); // GOOD
6060
dest2 = (char*)malloc(3);
6161
if (!dest2)
6262
return;
63-
snprintf(dest2, sizeof(src), "%s", src); // BAD (but with duplicate alerts)
63+
snprintf(dest2, sizeof(src), "%s", src); // BAD [NOT DETECTED]
6464
}

0 commit comments

Comments
 (0)