Skip to content

Commit bb8b0d0

Browse files
committed
C++: Use the unary version of 'comparesEq' to handle both disjuncts.
1 parent d2a00fa commit bb8b0d0

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cpp/ql/src/Security/CWE/CWE-570/IncorrectAllocationErrorHandling.ql

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,18 @@ predicate noThrowInTryBlock(NewOrNewArrayExpr newExpr, BadAllocCatchBlock catchB
215215
*/
216216
predicate nullCheckInThrowingNew(NewOrNewArrayExpr newExpr, GuardCondition guard) {
217217
newExpr.getAllocator() instanceof ThrowingAllocator and
218-
(
219-
// Handles null comparisons.
220-
guard.ensuresEq(globalValueNumber(newExpr).getAnExpr(), any(NullValue null), _, _, _)
221-
or
222-
// Handles `if(ptr)` and `if(!ptr)` cases.
223-
guard = globalValueNumber(newExpr).getAnExpr()
224-
)
218+
// There can be many guard conditions that compares `newExpr` againgst 0.
219+
// For example, for `if(!p)` both `p` and `!p` is a guard condition. To not
220+
// produce duplicates results we pick the "first" guard condition according
221+
// to some arbitrary ordering (i.e., location information). This means `!p` is the
222+
// element that we use to construct the alert.
223+
guard =
224+
min(GuardCondition gc, int startline, int startcolumn, int endline, int endcolumn |
225+
gc.comparesEq(globalValueNumber(newExpr).getAnExpr(), 0, _, _) and
226+
gc.getLocation().hasLocationInfo(_, startline, startcolumn, endline, endcolumn)
227+
|
228+
gc order by startline, startcolumn, endline, endcolumn
229+
)
225230
}
226231

227232
from NewOrNewArrayExpr newExpr, Element element, string msg, string elementString

0 commit comments

Comments
 (0)