From 6e95154824392b6e202f776c17a84d00fefd60b5 Mon Sep 17 00:00:00 2001 From: Nicolas PERU Date: Wed, 25 Nov 2015 17:33:34 +0100 Subject: [PATCH] SONARJAVA-1400 Proper support of postfix operators --- .../java/org/sonar/java/se/ExplodedGraphWalker.java | 11 ++++++++++- java-squid/src/test/files/se/Reproducer.java | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/java-squid/src/main/java/org/sonar/java/se/ExplodedGraphWalker.java b/java-squid/src/main/java/org/sonar/java/se/ExplodedGraphWalker.java index 2cabdecdd91..f002ef55573 100644 --- a/java-squid/src/main/java/org/sonar/java/se/ExplodedGraphWalker.java +++ b/java-squid/src/main/java/org/sonar/java/se/ExplodedGraphWalker.java @@ -54,6 +54,7 @@ import org.sonar.plugins.java.api.tree.NewClassTree; import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.TypeCastTree; +import org.sonar.plugins.java.api.tree.UnaryExpressionTree; import org.sonar.plugins.java.api.tree.VariableTree; import org.sonar.plugins.java.api.tree.WhileStatementTree; @@ -446,7 +447,15 @@ private void executeUnaryExpression(Tree tree) { programState = unstackUnary.a; SymbolicValue unarySymbolicValue = constraintManager.createSymbolicValue(tree); unarySymbolicValue.computedFrom(unstackUnary.b); - programState = programState.stackValue(unarySymbolicValue); + if(tree.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT) + && ((UnaryExpressionTree) tree).expression().is(Tree.Kind.IDENTIFIER)) { + programState = programState.put(((IdentifierTree) ((UnaryExpressionTree) tree).expression()).symbol(), unarySymbolicValue); + } + if(tree.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT)) { + programState = programState.stackValue(unstackUnary.b.get(0)); + } else { + programState = programState.stackValue(unarySymbolicValue); + } } private void executeIdentifier(IdentifierTree tree) { diff --git a/java-squid/src/test/files/se/Reproducer.java b/java-squid/src/test/files/se/Reproducer.java index 9c565eae18d..7a450ed9cfe 100644 --- a/java-squid/src/test/files/se/Reproducer.java +++ b/java-squid/src/test/files/se/Reproducer.java @@ -72,4 +72,15 @@ public void continue_foreach(boolean a, boolean b, Map map) { } } + private void increment(int index, int index2) { + int start = index; + index++; + if(start == index) { + + } + start = index2; + if(start == index2++) { // Noncompliant + } + } + } \ No newline at end of file