Skip to content

Commit 169bbcd

Browse files
authored
Merge pull request #682 from geoffw0/suspiciousaddsizeof
CPP: Fix false positive in SuspiciousAddWithSizeof.ql
2 parents 0432b01 + bff23f5 commit 169bbcd

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
| **Query** | **Expected impact** | **Change** |
1616
|----------------------------|------------------------|------------------------------------------------------------------|
17+
| Suspicious add with sizeof (`cpp/suspicious-add-sizeof`) | Fewer false positives | Pointer arithmetic on `char * const` expressions (and other variations of `char *`) are now correctly excluded from the results. |
1718
| Suspicious pointer scaling (`cpp/suspicious-pointer-scaling`) | Fewer false positives | False positives involving types that are not uniquely named in the snapshot have been fixed. |
1819
| Lossy function result cast (`cpp/lossy-function-result-cast`) | Fewer false positive results | The whitelist of rounding functions built into this query has been expanded. |
1920
| Unused static variable (`cpp/unused-static-variable`) | Fewer false positive results | Variables with the attribute `unused` are now excluded from the query. |

cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import IncorrectPointerScalingCommon
1515

1616
private predicate isCharSzPtrExpr(Expr e) {
1717
exists (PointerType pt
18-
| pt = e.getFullyConverted().getUnderlyingType()
19-
| pt.getBaseType().getUnspecifiedType() instanceof CharType
20-
or pt.getBaseType().getUnspecifiedType() instanceof VoidType)
18+
| pt = e.getFullyConverted().getType().getUnspecifiedType()
19+
| pt.getBaseType() instanceof CharType
20+
or pt.getBaseType() instanceof VoidType)
2121
}
2222

2323
from Expr sizeofExpr, Expr e

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| test.cpp:30:25:30:35 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
55
| test.cpp:38:30:38:40 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
66
| test.cpp:61:27:61:37 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
7+
| test.cpp:89:43:89:55 | sizeof(MyABC) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is myInt *const. |

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,32 @@ void test7(int i) {
6464
v = *(int *)(voidPointer + i); // GOOD (actually rather dubious, but this could be correct code)
6565
v = *(int *)(voidPointer + (i * sizeof(int))); // GOOD
6666
}
67+
68+
typedef unsigned long size_t;
69+
70+
void *malloc(size_t size);
71+
72+
class MyABC
73+
{
74+
public:
75+
int a, b, c;
76+
};
77+
78+
typedef unsigned char myChar;
79+
typedef unsigned int myInt;
80+
81+
class MyTest8Class
82+
{
83+
public:
84+
MyTest8Class() :
85+
myCharsPointer((myChar *)malloc(sizeof(MyABC) * 2)),
86+
myIntsPointer((myInt *)malloc(sizeof(MyABC) * 2))
87+
{
88+
myChar *secondPtr = myCharsPointer + sizeof(MyABC); // GOOD
89+
myInt *secondPtrInt = myIntsPointer + sizeof(MyABC); // BAD
90+
}
91+
92+
private:
93+
myChar * const myCharsPointer;
94+
myInt * const myIntsPointer;
95+
};

0 commit comments

Comments
 (0)