@@ -215,13 +215,18 @@ predicate noThrowInTryBlock(NewOrNewArrayExpr newExpr, BadAllocCatchBlock catchB
215215 */
216216predicate 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` are guard conditions. 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
227232from NewOrNewArrayExpr newExpr , Element element , string msg , string elementString
0 commit comments