diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll index 29cdcfedda0f..c068425a96e2 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll @@ -171,6 +171,20 @@ module Raw { // forwarded the result of another translated expression. instruction = translatedExpr.getInstruction(_) ) + or + // Consider the snippet `if(x) { ... }` where `x` is an integer. + // In C++ there is a `BoolConversion` conversion on `x` which generates a + // `CompareNEInstruction` whose `getInstructionUnconvertedResultExpression` + // is the `BoolConversion`. Thus, calling `getInstructionConvertedResultExpression` on + // the `CompareNEInstruction` gives `x` in C++ code. + // However, in C there is no such conversion to return. So instead we have + // to map the result of `getInstructionConvertedResultExpression` on + // the `CompareNEInstruction` to `x` manually. + exists(TranslatedValueCondition translatedValueCondition | + translatedValueCondition = getTranslatedCondition(result) and + translatedValueCondition.shouldGenerateCompareNE() and + instruction = translatedValueCondition.getInstruction(ValueConditionCompareTag()) + ) } cached diff --git a/cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected b/cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected index b08194691c6d..12e33f3a0a89 100644 --- a/cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected +++ b/cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected @@ -20,12 +20,21 @@ astGuards | test.c:109:9:109:14 | ... == ... | | test.c:109:9:109:23 | ... \|\| ... | | test.c:109:19:109:23 | ... < ... | +| test.c:126:7:126:7 | 1 | +| test.c:126:7:126:28 | ... && ... | +| test.c:126:12:126:26 | call to test3_condition | +| test.c:131:7:131:7 | b | +| test.c:137:7:137:7 | 0 | | test.c:146:7:146:8 | ! ... | +| test.c:152:10:152:10 | x | +| test.c:152:10:152:15 | ... && ... | +| test.c:152:15:152:15 | y | | test.c:156:9:156:19 | ... == ... | | test.c:159:9:159:19 | ... == ... | | test.c:162:9:162:18 | ... < ... | | test.c:165:9:165:18 | ... < ... | | test.c:175:13:175:32 | ... == ... | +| test.c:181:9:181:9 | x | | test.cpp:18:8:18:10 | call to get | | test.cpp:31:7:31:13 | ... == ... | | test.cpp:42:13:42:20 | call to getABool | @@ -138,8 +147,24 @@ astGuardsCompare | 109 | y < 0+0 when ... < ... is true | | 109 | y >= 0+0 when ... < ... is false | | 109 | y >= 0+0 when ... \|\| ... is false | +| 126 | 1 != 0 when 1 is true | +| 126 | 1 != 0 when ... && ... is true | +| 126 | 1 == 0 when 1 is false | +| 126 | call to test3_condition != 0 when ... && ... is true | +| 126 | call to test3_condition != 0 when call to test3_condition is true | +| 126 | call to test3_condition == 0 when call to test3_condition is false | +| 131 | b != 0 when b is true | +| 131 | b == 0 when b is false | +| 137 | 0 != 0 when 0 is true | +| 137 | 0 == 0 when 0 is false | | 146 | x != 0 when ! ... is false | | 146 | x == 0 when ! ... is true | +| 152 | x != 0 when ... && ... is true | +| 152 | x != 0 when x is true | +| 152 | x == 0 when x is false | +| 152 | y != 0 when ... && ... is true | +| 152 | y != 0 when y is true | +| 152 | y == 0 when y is false | | 156 | ... + ... != x+0 when ... == ... is false | | 156 | ... + ... == x+0 when ... == ... is true | | 156 | x != ... + ...+0 when ... == ... is false | @@ -178,6 +203,8 @@ astGuardsCompare | 175 | call to foo != 0+0 when ... == ... is false | | 175 | call to foo == 0 when ... == ... is true | | 175 | call to foo == 0+0 when ... == ... is true | +| 181 | x != 0 when x is true | +| 181 | x == 0 when x is false | astGuardsControl | test.c:7:9:7:13 | ... > ... | false | 10 | 11 | | test.c:7:9:7:13 | ... > ... | true | 7 | 9 | @@ -249,13 +276,29 @@ astGuardsControl | test.c:109:9:109:14 | ... == ... | false | 113 | 113 | | test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 | | test.c:109:19:109:23 | ... < ... | false | 113 | 113 | +| test.c:126:7:126:7 | 1 | true | 126 | 126 | +| test.c:126:7:126:7 | 1 | true | 126 | 128 | +| test.c:126:7:126:7 | 1 | true | 131 | 131 | +| test.c:126:7:126:7 | 1 | true | 131 | 132 | +| test.c:126:7:126:7 | 1 | true | 134 | 123 | +| test.c:126:7:126:28 | ... && ... | true | 126 | 128 | +| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 | +| test.c:131:7:131:7 | b | true | 131 | 132 | +| test.c:137:7:137:7 | 0 | false | 142 | 136 | | test.c:146:7:146:8 | ! ... | true | 146 | 147 | +| test.c:152:10:152:10 | x | true | 151 | 152 | +| test.c:152:10:152:10 | x | true | 152 | 152 | +| test.c:152:10:152:15 | ... && ... | true | 151 | 152 | +| test.c:152:15:152:15 | y | true | 151 | 152 | | test.c:156:9:156:19 | ... == ... | true | 156 | 157 | | test.c:159:9:159:19 | ... == ... | true | 159 | 160 | | test.c:162:9:162:18 | ... < ... | true | 162 | 163 | | test.c:165:9:165:18 | ... < ... | true | 165 | 166 | | test.c:175:13:175:32 | ... == ... | false | 175 | 175 | | test.c:175:13:175:32 | ... == ... | true | 175 | 175 | +| test.c:181:9:181:9 | x | false | 183 | 184 | +| test.c:181:9:181:9 | x | true | 181 | 182 | +| test.c:181:9:181:9 | x | true | 186 | 180 | | test.cpp:18:8:18:10 | call to get | true | 19 | 19 | | test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 | | test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 | @@ -477,9 +520,27 @@ astGuardsEnsure_const | test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 109 | 109 | | test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 | | test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 | +| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 | +| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 | +| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 | +| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 | +| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 | | test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 | +| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 151 | 152 | +| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 152 | 152 | +| test.c:152:10:152:15 | ... && ... | test.c:152:10:152:10 | x | != | 0 | 151 | 152 | +| test.c:152:10:152:15 | ... && ... | test.c:152:15:152:15 | y | != | 0 | 151 | 152 | +| test.c:152:15:152:15 | y | test.c:152:15:152:15 | y | != | 0 | 151 | 152 | | test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | 0 | 175 | 175 | | test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | 0 | 175 | 175 | +| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 181 | 182 | +| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 186 | 180 | +| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | == | 0 | 183 | 184 | | test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 | | test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 | | test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 34 | 34 | diff --git a/cpp/ql/test/library-tests/controlflow/guards/Guards.expected b/cpp/ql/test/library-tests/controlflow/guards/Guards.expected index 9fe0a81430da..c72ecf5d1186 100644 --- a/cpp/ql/test/library-tests/controlflow/guards/Guards.expected +++ b/cpp/ql/test/library-tests/controlflow/guards/Guards.expected @@ -19,8 +19,15 @@ | test.c:109:9:109:14 | ... == ... | | test.c:109:9:109:23 | ... \|\| ... | | test.c:109:19:109:23 | ... < ... | +| test.c:126:7:126:7 | 1 | +| test.c:126:7:126:28 | ... && ... | +| test.c:126:12:126:26 | call to test3_condition | +| test.c:131:7:131:7 | b | +| test.c:137:7:137:7 | 0 | | test.c:146:7:146:8 | ! ... | +| test.c:152:8:152:8 | p | | test.c:158:8:158:9 | ! ... | +| test.c:164:8:164:8 | s | | test.c:170:8:170:9 | ! ... | | test.cpp:18:8:18:10 | call to get | | test.cpp:31:7:31:13 | ... == ... | diff --git a/cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected b/cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected index e97045715998..42bec6497b22 100644 --- a/cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected +++ b/cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected @@ -149,13 +149,21 @@ | 111 | 0.0 == i+0 when ... != ... is false | | 111 | i != 0.0+0 when ... != ... is true | | 111 | i == 0.0+0 when ... != ... is false | +| 126 | 1 != 0 when 1 is true | +| 126 | 1 != 0 when ... && ... is true | +| 126 | 1 == 0 when 1 is false | +| 126 | call to test3_condition != 0 when ... && ... is true | +| 126 | call to test3_condition != 0 when call to test3_condition is true | +| 126 | call to test3_condition == 0 when call to test3_condition is false | | 131 | ... + ... != a+0 when call to __builtin_expect is false | | 131 | ... + ... == a+0 when call to __builtin_expect is true | | 131 | a != ... + ...+0 when call to __builtin_expect is false | | 131 | a != b+42 when call to __builtin_expect is false | | 131 | a == ... + ...+0 when call to __builtin_expect is true | | 131 | a == b+42 when call to __builtin_expect is true | +| 131 | b != 0 when b is true | | 131 | b != a+-42 when call to __builtin_expect is false | +| 131 | b == 0 when b is false | | 131 | b == a+-42 when call to __builtin_expect is true | | 131 | call to __builtin_expect != 0 when call to __builtin_expect is true | | 131 | call to __builtin_expect == 0 when call to __builtin_expect is false | @@ -169,6 +177,8 @@ | 135 | b == a+-42 when call to __builtin_expect is false | | 135 | call to __builtin_expect != 0 when call to __builtin_expect is true | | 135 | call to __builtin_expect == 0 when call to __builtin_expect is false | +| 137 | 0 != 0 when 0 is true | +| 137 | 0 == 0 when 0 is false | | 141 | 42 != a+0 when call to __builtin_expect is false | | 141 | 42 == a+0 when call to __builtin_expect is true | | 141 | a != 42 when call to __builtin_expect is false | @@ -187,7 +197,11 @@ | 145 | call to __builtin_expect == 0 when call to __builtin_expect is false | | 146 | x != 0 when ! ... is false | | 146 | x == 0 when ! ... is true | +| 152 | p != 0 when p is true | +| 152 | p == 0 when p is false | | 158 | p != 0 when ! ... is false | | 158 | p == 0 when ! ... is true | +| 164 | s != 0 when s is true | +| 164 | s == 0 when s is false | | 170 | s != 0 when ! ... is false | | 170 | s == 0 when ! ... is true | diff --git a/cpp/ql/test/library-tests/controlflow/guards/GuardsControl.expected b/cpp/ql/test/library-tests/controlflow/guards/GuardsControl.expected index 44c6bf1f802a..3de83c6e2beb 100644 --- a/cpp/ql/test/library-tests/controlflow/guards/GuardsControl.expected +++ b/cpp/ql/test/library-tests/controlflow/guards/GuardsControl.expected @@ -68,8 +68,19 @@ | test.c:109:9:109:14 | ... == ... | false | 113 | 113 | | test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 | | test.c:109:19:109:23 | ... < ... | false | 113 | 113 | +| test.c:126:7:126:7 | 1 | true | 126 | 126 | +| test.c:126:7:126:7 | 1 | true | 126 | 128 | +| test.c:126:7:126:7 | 1 | true | 131 | 131 | +| test.c:126:7:126:7 | 1 | true | 131 | 132 | +| test.c:126:7:126:7 | 1 | true | 134 | 123 | +| test.c:126:7:126:28 | ... && ... | true | 126 | 128 | +| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 | +| test.c:131:7:131:7 | b | true | 131 | 132 | +| test.c:137:7:137:7 | 0 | false | 142 | 136 | | test.c:146:7:146:8 | ! ... | true | 146 | 147 | +| test.c:152:8:152:8 | p | true | 152 | 154 | | test.c:158:8:158:9 | ! ... | true | 158 | 160 | +| test.c:164:8:164:8 | s | true | 164 | 166 | | test.c:170:8:170:9 | ! ... | true | 170 | 172 | | test.cpp:18:8:18:10 | call to get | true | 19 | 19 | | test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 | diff --git a/cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected b/cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected index 8d4d78f02a14..6963216d077d 100644 --- a/cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected +++ b/cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected @@ -261,8 +261,20 @@ unary | test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 | | test.c:109:9:109:23 | ... \|\| ... | test.c:109:19:109:19 | y | >= | 0 | 113 | 113 | | test.c:109:19:109:23 | ... < ... | test.c:109:19:109:19 | y | >= | 0 | 113 | 113 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 | +| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 | +| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 | +| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 | +| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 | +| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 | +| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 | | test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 | +| test.c:152:8:152:8 | p | test.c:152:8:152:8 | p | != | 0 | 152 | 154 | | test.c:158:8:158:9 | ! ... | test.c:158:9:158:9 | p | == | 0 | 158 | 160 | +| test.c:164:8:164:8 | s | test.c:164:8:164:8 | s | != | 0 | 164 | 166 | | test.c:170:8:170:9 | ! ... | test.c:170:9:170:9 | s | == | 0 | 170 | 172 | | test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 | | test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 |