Skip to content

Commit 7db2b2c

Browse files
committed
C++: Make the two queries more alike.
1 parent 4630c69 commit 7db2b2c

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

cpp/ql/src/Critical/SizeCheck.ql

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
import cpp
1616

1717
class Allocation extends FunctionCall {
18-
Allocation() {
19-
exists(string name |
20-
this.getTarget().hasGlobalOrStdName(name) and
21-
(name = "malloc" or name = "calloc" or name = "realloc")
22-
)
23-
}
18+
Allocation() { this.getTarget().hasGlobalOrStdName(["malloc", "calloc", "realloc"]) }
2419

2520
private string getName() { this.getTarget().hasGlobalOrStdName(result) }
2621

cpp/ql/src/Critical/SizeCheck2.ql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,21 @@ predicate baseType(Allocation alloc, Type base) {
4444
)
4545
}
4646

47+
predicate decideOnSize(Type t, int size) {
48+
// If the codebase has more than one type with the same name, it can have more than one size.
49+
size = min(t.getSize())
50+
}
51+
4752
from Allocation alloc, Type base, int basesize, int allocated
4853
where
4954
baseType(alloc, base) and
5055
allocated = alloc.getSize() and
56+
decideOnSize(base, basesize) and
5157
// If the codebase has more than one type with the same name, check if any matches
5258
not exists(int size | base.getSize() = size |
5359
size = 0 or
5460
(allocated / size) * size = allocated
55-
) and
56-
basesize = min(base.getSize())
61+
)
5762
select alloc,
5863
"Allocated memory (" + allocated.toString() + " bytes) is not a multiple of the size of '" +
5964
base.getName() + "' (" + basesize.toString() + " bytes)."

0 commit comments

Comments
 (0)