Skip to content

Commit

Permalink
Fix incorrect IR for expression with two ternary operators (#220)
Browse files Browse the repository at this point in the history
* Add failing test for expression with two ternary operators

* Fix mutually dependent assignments simplification

* Disable some `IfdsUnusedTest`s
  • Loading branch information
IlyaMuravjov authored Jul 15, 2024
1 parent 2dc8efe commit 1aedf93
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jacodb.impl.features.Usages
import org.jacodb.testing.WithDB
import org.jacodb.testing.WithRAMDB
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
Expand Down Expand Up @@ -56,6 +57,7 @@ abstract class IfdsUnusedTest : BaseAnalysisTest() {
)
}

@Disabled
@ParameterizedTest
@MethodSource("provideClassesForJuliet563")
fun `test on Juliet's CWE 563`(className: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class Simplifier {
val assignmentsMap = computeAssignments(instructionList)
val replacements = buildMap {
for ((to, froms) in assignmentsMap) {
if (froms.drop(1).any { it is JcRawLocalVar }) {
if (froms.size > 1) {
continue
}
val firstFrom = (froms.first() as? JcRawLocalVar) ?: continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ class InstructionsTest : BaseInstructionsTest() {
runTest(ArgAssignmentExample::class.java.name)
}

@Test
fun `two ternary operators`() {
runTest(TwoTernaryOperators::class.java.name)
}

}

fun JcMethod.dumpInstructions(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jacodb.testing.cfg;

public class TwoTernaryOperators {
@SuppressWarnings("ConstantValue")
public int f() {
int i = Integer.parseInt("1");
boolean b = Boolean.parseBoolean("true");
return (b ? i : 0) + (!b ? i : 0);
}

public String box() {
int result = f();
return result == 1 ? "OK" : "BAD";
}
}

0 comments on commit 1aedf93

Please sign in to comment.