diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edf183cc..672228a42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - #1114 Script search results dialogs closing on swf close - #1159 Regexp syntax hilight when not a regexp (only division) - #1227 AS3 avoid recursion (stackoverflow) caused by newfunction instruction +- #1360 Precedence of increment/decrement operations ## [13.0.2] - 2021-02-10 ### Changed diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/DecrementAVM2Item.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/DecrementAVM2Item.java index e4a5e1cdb..d68f322e7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/DecrementAVM2Item.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/DecrementAVM2Item.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.abc.avm2.model; import com.jpexs.decompiler.flash.helpers.GraphTextWriter; @@ -34,7 +35,13 @@ public class DecrementAVM2Item extends AVM2Item { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - value.toString(writer, localData); + if (value.getPrecedence() > precedence) { + writer.append("("); + value.toString(writer, localData); + writer.append(")"); + } else { + value.toString(writer, localData); + } return writer.append(" - 1"); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/IncrementAVM2Item.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/IncrementAVM2Item.java index cb6b92304..28d410027 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/IncrementAVM2Item.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/model/IncrementAVM2Item.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.abc.avm2.model; import com.jpexs.decompiler.flash.helpers.GraphTextWriter; @@ -34,7 +35,13 @@ public class IncrementAVM2Item extends AVM2Item { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - value.toString(writer, localData); + if (value.getPrecedence() > precedence) { + writer.append("("); + value.toString(writer, localData); + writer.append(")"); + } else { + value.toString(writer, localData); + } return writer.append(" + 1"); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/DecrementActionItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/DecrementActionItem.java index 54adb4705..ed469588f 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/DecrementActionItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/DecrementActionItem.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.action.model; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; @@ -48,7 +49,13 @@ public class DecrementActionItem extends ActionItem { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - object.toString(writer, localData); + if (object.getPrecedence() > precedence) { + writer.append("("); + object.toString(writer, localData); + writer.append(")"); + } else { + object.toString(writer, localData); + } return writer.append(" - 1"); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/IncrementActionItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/IncrementActionItem.java index d2c81e98a..c9afc828d 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/IncrementActionItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/IncrementActionItem.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.action.model; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; @@ -48,7 +49,13 @@ public class IncrementActionItem extends ActionItem { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - object.toString(writer, localData); + if (object.getPrecedence() > precedence) { + writer.append("("); + object.toString(writer, localData); + writer.append(")"); + } else { + object.toString(writer, localData); + } return writer.append(" + 1"); } diff --git a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3AssembledDecompileTest.java b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3AssembledDecompileTest.java index d1c84c8f1..d7541037d 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3AssembledDecompileTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/decompiler/flash/as3decompile/ActionScript3AssembledDecompileTest.java @@ -27,6 +27,14 @@ public class ActionScript3AssembledDecompileTest extends ActionScript3DecompileT false); } + @Test + public void testDecrementPrecedence() { + decompileMethod("assembled", "testDecrementPrecedence", "var _loc2_:int = 10;\r\n" + + "var _loc1_:int = 5;\r\n" + + "var _loc3_:* = _loc2_ & (1 << _loc1_) - 1;\r\n", + false); + } + @Test public void testDeobfuscatorJumpsExceptionStart() { decompileMethod("assembled", "testDeobfuscatorJumpsExceptionStart", "try\r\n" diff --git a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.abc b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.abc index 452721813..abe150dee 100644 Binary files a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.abc and b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.abc differ diff --git a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.asasm b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.asasm index bf7085e30..3eed3fd5d 100644 --- a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.asasm +++ b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/as3_assembled-0.main.asasm @@ -25,5 +25,6 @@ program #include "tests/TestTryCatchLoopBreakLevel2.script.asasm" #include "tests/TestSlots.script.asasm" #include "tests/TestSlots.init/inline_method.method.asasm" + #include "tests/TestDecrementPrecedence.script.asasm" ; place to add next end ; program diff --git a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.class.asasm b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.class.asasm new file mode 100644 index 000000000..4f475b045 --- /dev/null +++ b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.class.asasm @@ -0,0 +1,74 @@ +class + refid "tests:TestDecrementPrecedence" + instance QName(PackageNamespace("tests"), "TestDecrementPrecedence") + extends QName(PackageNamespace(""), "Object") + flag SEALED + flag PROTECTEDNS + protectedns ProtectedNamespace("tests:TestDecrementPrecedence") + iinit + refid "tests:TestDecrementPrecedence/instance/init" + body + maxstack 1 + localcount 1 + initscopedepth 4 + maxscopedepth 5 + code + getlocal0 + pushscope + + getlocal0 + constructsuper 0 + + returnvoid + end ; code + end ; body + end ; method + trait method QName(PackageNamespace(""), "run") + method + refid "tests:TestDecrementPrecedence/instance/run" + returns QName(PackageNamespace(""), "void") + body + maxstack 2 + localcount 4 + initscopedepth 4 + maxscopedepth 5 + code + getlocal0 + pushscope + + pushbyte 10 + setlocal2 + pushbyte 5 + setlocal1 + + + getlocal2 + pushbyte 1 + getlocal1 + lshift + decrement + bitand + setlocal 3 + + returnvoid + end ; code + end ; body + end ; method + end ; trait + end ; instance + cinit + refid "tests:TestDecrementPrecedence/class/init" + body + maxstack 1 + localcount 1 + initscopedepth 3 + maxscopedepth 4 + code + getlocal0 + pushscope + + returnvoid + end ; code + end ; body + end ; method +end ; class diff --git a/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.script.asasm b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.script.asasm new file mode 100644 index 000000000..119b5d8f1 --- /dev/null +++ b/libsrc/ffdec_lib/testdata/as3_assembled/abc/as3_assembled-0/tests/TestDecrementPrecedence.script.asasm @@ -0,0 +1,29 @@ +script + sinit + refid "tests:TestDecrementPrecedence/init" + body + maxstack 2 + localcount 1 + initscopedepth 1 + maxscopedepth 3 + code + getlocal0 + pushscope + + findpropstrict Multiname("TestDecrementPrecedence", [PackageNamespace("tests")]) + getlex QName(PackageNamespace(""), "Object") + pushscope + + getlex Multiname("Object", [PrivateNamespace(null, "tests:TestDecrementPrecedence"), PackageNamespace(""), PackageNamespace("tests"), PackageInternalNs("tests"), Namespace("http://adobe.com/AS3/2006/builtin")]) + newclass "tests:TestDecrementPrecedence" + popscope + initproperty QName(PackageNamespace("tests"), "TestDecrementPrecedence") + + returnvoid + end ; code + end ; body + end ; method + trait class QName(PackageNamespace("tests"), "TestDecrementPrecedence") + #include "TestDecrementPrecedence.class.asasm" + end ; trait +end ; script diff --git a/libsrc/ffdec_lib/testdata/as3_assembled/bin/as3_assembled.swf b/libsrc/ffdec_lib/testdata/as3_assembled/bin/as3_assembled.swf index 7ba011d84..de3f5e9a8 100644 Binary files a/libsrc/ffdec_lib/testdata/as3_assembled/bin/as3_assembled.swf and b/libsrc/ffdec_lib/testdata/as3_assembled/bin/as3_assembled.swf differ