Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 614a8ef

Browse files
committed
Allow void* pointer arithmetic with sizeof
Technically this isn't allowed by the C spec, but it's been seen in the wild: see https://lgtm.com/projects/g/libcsp/libcsp/snapshot/3763c7b3380f95c81636de5c95156fd3ef151a21/files/src/csp_buffer.c\#x1d04047d2bb68c21:1
1 parent 4732526 commit 614a8ef

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
import cpp
1414
import IncorrectPointerScalingCommon
1515

16-
private predicate isCharPtrExpr(Expr e) {
16+
private predicate isCharSzPtrExpr(Expr e) {
1717
exists (PointerType pt
1818
| pt = e.getFullyConverted().getUnderlyingType()
19-
| pt.getBaseType().getUnspecifiedType() instanceof CharType)
19+
| pt.getBaseType().getUnspecifiedType() instanceof CharType
20+
or pt.getBaseType().getUnspecifiedType() instanceof VoidType)
2021
}
2122

2223
from Expr sizeofExpr, Expr e
2324
where
2425
// If we see an addWithSizeof then we expect the type of
25-
// the pointer expression to be char*. Otherwise it is probably
26-
// a mistake.
27-
addWithSizeof(e, sizeofExpr, _) and not isCharPtrExpr(e)
26+
// the pointer expression to be char* or void*. Otherwise it
27+
// is probably a mistake.
28+
addWithSizeof(e, sizeofExpr, _) and not isCharSzPtrExpr(e)
2829
select
2930
sizeofExpr,
3031
"Suspicious sizeof offset in a pointer arithmetic expression. " +

0 commit comments

Comments
 (0)