Skip to content

C++: Fix missing guard conditions for C++ code #20138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/ql/lib/change-notes/2025-07-30-guards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The "Guards" library (`semmle.code.cpp.controlflow.Guards` and `semmle.code.cpp.controlflow.IRGuards`) now recognizes more guards. As a result, queries using those libraries will now produce fewer false positives.
103 changes: 92 additions & 11 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,16 @@ private module Cached {
ValueNumber getUnary() { result.getAnInstruction() = instr.getUnary() }
}

private class ConvertBoolToIntOrPointerInstruction extends ConvertInstruction {
ConvertBoolToIntOrPointerInstruction() {
this.getUnary().getResultIRType() instanceof IRBooleanType and
(
this.getResultIRType() instanceof IRIntegerType or
this.getResultIRType() instanceof IRAddressType
)
}
}

/**
* Holds if `left == right + k` is `areEqual` given that test is `testIsTrue`.
*
Expand Down Expand Up @@ -966,6 +976,26 @@ private module Cached {
)
or
compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), left, right, k, areEqual, value)
or
// If we have e.g.:
// ```
// x = (a == b)
// if(x != c) { ... }
// ```
// then `x != c` is true implies that `a == b` is true.
// ```
exists(Operand l, Operand r, ValueNumber vn, int c, AbstractValue v |
test.(CompareValueNumber).hasOperands(l, r) and
int_value(r.getDef()) = c and
vn.getAnInstruction() = getBooleanInstruction(l.getDef()) and
compares_eq(vn, left, right, k, areEqual, v)
|
test instanceof CompareNEValueNumber and
if c = 0 then value = v else value = v.getDualValue()
or
test instanceof CompareEQValueNumber and
if c = 0 then value = v.getDualValue() else value = v
)
}

private predicate isConvertedBool(Instruction instr) {
Expand Down Expand Up @@ -1006,19 +1036,24 @@ private module Cached {
k = k1 + k2
)
or
exists(CompareValueNumber cmp, Operand left, Operand right, AbstractValue v |
test = cmp and
pragma[only_bind_into](cmp)
.hasOperands(pragma[only_bind_into](left), pragma[only_bind_into](right)) and
isConvertedBool(left.getDef()) and
int_value(right.getDef()) = 0 and
unary_compares_eq(valueNumberOfOperand(left), op, k, areEqual, v)
// If we have e.g.:
// ```
// x = (a == 10)
// if(x != c) { ... }
// ```
// then `x != c` is true implies that `a == 10` is true.
// ```
exists(Operand l, Operand r, ValueNumber vn, int c, AbstractValue v |
test.(CompareValueNumber).hasOperands(l, r) and
int_value(r.getDef()) = c and
vn.getAnInstruction() = getBooleanInstruction(l.getDef()) and
compares_lt(vn, op, k, areEqual, v)
|
cmp instanceof CompareNEValueNumber and
v = value
test instanceof CompareNEValueNumber and
if c = 0 then value = v else value = v.getDualValue()
or
cmp instanceof CompareEQValueNumber and
v.getDualValue() = value
test instanceof CompareEQValueNumber and
if c = 0 then value = v.getDualValue() else value = v
)
or
unary_compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), op, k, areEqual, value)
Expand Down Expand Up @@ -1192,6 +1227,12 @@ private module Cached {
unary_builtin_expect_eq(test, op, k, areEqual, value)
}

private Instruction getBooleanInstruction(Instruction instr) {
result = instr.(ConvertBoolToIntOrPointerInstruction).getUnary()
or
result = getBooleanInstruction(instr.(CopyInstruction).getSourceValue())
}

/*
* Simplification of inequality expressions
* Simplify conditions in the source to the canonical form l < r + k.
Expand All @@ -1215,6 +1256,26 @@ private module Cached {
exists(AbstractValue dual | value = dual.getDualValue() |
compares_lt(test.(LogicalNotValueNumber).getUnary(), left, right, k, isLt, dual)
)
or
// If we have e.g.:
// ```
// x = (a < b)
// if(x != c) { ... }
// ```
// then `x != c` is true implies that `a < b` is true.
// ```
exists(Operand l, Operand r, ValueNumber vn, int c, AbstractValue v |
test.(CompareValueNumber).hasOperands(l, r) and
int_value(r.getDef()) = c and
vn.getAnInstruction() = getBooleanInstruction(l.getDef()) and
compares_lt(vn, left, right, k, isLt, v)
|
test instanceof CompareNEValueNumber and
if c = 0 then value = v else value = v.getDualValue()
or
test instanceof CompareEQValueNumber and
if c = 0 then value = v.getDualValue() else value = v
)
}

/** Holds if `op < k` evaluates to `isLt` given that `test` evaluates to `value`. */
Expand All @@ -1234,6 +1295,26 @@ private module Cached {
int_value(const) = k1 and
k = k1 + k2
)
or
// If we have e.g.:
// ```
// x = (a < 10)
// if(x != c) { ... }
// ```
// then `x != c` is true implies that `a < 10` is true.
// ```
exists(Operand l, Operand r, ValueNumber vn, int c, AbstractValue v |
test.(CompareValueNumber).hasOperands(l, r) and
int_value(r.getDef()) = c and
vn.getAnInstruction() = getBooleanInstruction(l.getDef()) and
compares_lt(vn, op, k, isLt, v)
|
test instanceof CompareNEValueNumber and
if c = 0 then value = v else value = v.getDualValue()
or
test instanceof CompareEQValueNumber and
if c = 0 then value = v.getDualValue() else value = v
)
}

/** `(a < b + k) => (b > a - k) => (b >= a + (1-k))` */
Expand Down
11 changes: 11 additions & 0 deletions cpp/ql/test/library-tests/controlflow/guards/Guards.expected
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
| test.c:198:8:198:8 | b |
| test.c:206:7:206:8 | ! ... |
| test.c:206:8:206:8 | c |
| test.c:213:6:213:6 | b |
| test.c:222:6:222:11 | ... != ... |
| test.c:231:6:231:6 | b |
| test.c:240:6:240:11 | ... != ... |
| test.c:249:6:249:6 | b |
| test.c:258:6:258:11 | ... != ... |
| test.cpp:18:8:18:10 | call to get |
| test.cpp:31:7:31:13 | ... == ... |
| test.cpp:42:13:42:20 | call to getABool |
Expand Down Expand Up @@ -92,3 +98,8 @@
| test.cpp:241:9:241:43 | ... && ... |
| test.cpp:241:22:241:30 | ... == ... |
| test.cpp:241:35:241:43 | ... == ... |
| test.cpp:248:6:248:11 | ... != ... |
| test.cpp:257:6:257:6 | b |
| test.cpp:266:6:266:11 | ... != ... |
| test.cpp:275:6:275:6 | b |
| test.cpp:284:6:284:11 | ... != ... |
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy