Skip to content

Commit abd0a07

Browse files
committed
Addressing false positive with strncpy.
1 parent 9105375 commit abd0a07

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

cpp/ql/lib/semmle/code/cpp/security/BufferAccess.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ class StrncpyBA extends BufferAccess {
125125
result = this.(FunctionCall).getArgument(0) and
126126
bufferDesc = "destination buffer" and
127127
accessType = 2
128-
or
129-
result = this.(FunctionCall).getArgument(1) and
130-
bufferDesc = "source buffer" and
131-
accessType = 2
128+
// Ignore this case as reading past the source null terminator is not the behavior of strncpy
129+
// or
130+
// result = this.(FunctionCall).getArgument(1) and
131+
// bufferDesc = "source buffer" and
132+
// accessType = 2
132133
}
133134

134135
override Expr getSizeExpr() { result = this.(FunctionCall).getArgument(2) }

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ edges
3131
| main.cpp:9:29:9:32 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | |
3232
| main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | **argv | provenance | |
3333
| main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | *argv | provenance | |
34-
| main.cpp:10:20:10:23 | **argv | tests.cpp:657:32:657:35 | **argv | provenance | |
35-
| main.cpp:10:20:10:23 | *argv | tests.cpp:657:32:657:35 | **argv | provenance | |
36-
| main.cpp:10:20:10:23 | *argv | tests.cpp:657:32:657:35 | *argv | provenance | |
34+
| main.cpp:10:20:10:23 | **argv | tests.cpp:664:32:664:35 | **argv | provenance | |
35+
| main.cpp:10:20:10:23 | *argv | tests.cpp:664:32:664:35 | **argv | provenance | |
36+
| main.cpp:10:20:10:23 | *argv | tests.cpp:664:32:664:35 | *argv | provenance | |
3737
| overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | provenance | |
3838
| overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | *argv | provenance | |
3939
| test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | provenance | |
@@ -46,12 +46,12 @@ edges
4646
| tests.cpp:628:14:628:14 | *s [*home] | tests.cpp:628:14:628:19 | *home | provenance | |
4747
| tests.cpp:628:14:628:14 | *s [*home] | tests.cpp:628:16:628:19 | *home | provenance | |
4848
| tests.cpp:628:16:628:19 | *home | tests.cpp:628:14:628:19 | *home | provenance | |
49-
| tests.cpp:657:32:657:35 | **argv | tests.cpp:682:9:682:15 | *access to array | provenance | |
50-
| tests.cpp:657:32:657:35 | **argv | tests.cpp:683:9:683:15 | *access to array | provenance | |
51-
| tests.cpp:657:32:657:35 | *argv | tests.cpp:682:9:682:15 | *access to array | provenance | |
52-
| tests.cpp:657:32:657:35 | *argv | tests.cpp:683:9:683:15 | *access to array | provenance | |
53-
| tests.cpp:682:9:682:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54-
| tests.cpp:683:9:683:15 | *access to array | tests.cpp:622:19:622:24 | *source | provenance | |
49+
| tests.cpp:664:32:664:35 | **argv | tests.cpp:689:9:689:15 | *access to array | provenance | |
50+
| tests.cpp:664:32:664:35 | **argv | tests.cpp:690:9:690:15 | *access to array | provenance | |
51+
| tests.cpp:664:32:664:35 | *argv | tests.cpp:689:9:689:15 | *access to array | provenance | |
52+
| tests.cpp:664:32:664:35 | *argv | tests.cpp:690:9:690:15 | *access to array | provenance | |
53+
| tests.cpp:689:9:689:15 | *access to array | tests.cpp:613:19:613:24 | *source | provenance | |
54+
| tests.cpp:690:9:690:15 | *access to array | tests.cpp:622:19:622:24 | *source | provenance | |
5555
| tests_restrict.c:15:41:15:44 | **argv | tests_restrict.c:15:41:15:44 | **argv | provenance | |
5656
| tests_restrict.c:15:41:15:44 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | |
5757
nodes
@@ -85,10 +85,10 @@ nodes
8585
| tests.cpp:628:14:628:14 | *s [*home] | semmle.label | *s [*home] |
8686
| tests.cpp:628:14:628:19 | *home | semmle.label | *home |
8787
| tests.cpp:628:16:628:19 | *home | semmle.label | *home |
88-
| tests.cpp:657:32:657:35 | **argv | semmle.label | **argv |
89-
| tests.cpp:657:32:657:35 | *argv | semmle.label | *argv |
90-
| tests.cpp:682:9:682:15 | *access to array | semmle.label | *access to array |
91-
| tests.cpp:683:9:683:15 | *access to array | semmle.label | *access to array |
88+
| tests.cpp:664:32:664:35 | **argv | semmle.label | **argv |
89+
| tests.cpp:664:32:664:35 | *argv | semmle.label | *argv |
90+
| tests.cpp:689:9:689:15 | *access to array | semmle.label | *access to array |
91+
| tests.cpp:690:9:690:15 | *access to array | semmle.label | *access to array |
9292
| tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv |
9393
| tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv |
9494
| tests_restrict.c:15:41:15:44 | *argv | semmle.label | *argv |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,13 @@ void test26(bool cond)
654654
if (ptr[-1] == 0) { return; } // GOOD: accesses buffer[1]
655655
}
656656

657+
void test27(){
658+
char *src = "";
659+
char *dest = "abcdefgh";
660+
661+
strncpy(dest, src, strlen(dest)); // GOOD, strncpy will not read past null terminator of source
662+
}
663+
657664
int tests_main(int argc, char *argv[])
658665
{
659666
long long arr17[19];

0 commit comments

Comments
 (0)