Skip to content

Commit

Permalink
fixes Issue #339
Browse files Browse the repository at this point in the history
  • Loading branch information
chibash committed Oct 16, 2020
1 parent b70a41b commit 1c4e31b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Readme.html
Expand Up @@ -293,7 +293,7 @@ <h2>Changes</h2>

<p>-version 3.28
<ul>
<li>GitHub Issue #328.
<li>GitHub Issue #328, #339.
</ul>

<p>-version 3.27 on March 19, 2020
Expand Down
Binary file modified javassist.jar
Binary file not shown.
9 changes: 7 additions & 2 deletions src/main/javassist/bytecode/StackMapTable.java
Expand Up @@ -881,7 +881,11 @@ void update(int pos, int offsetDelta, int base, int entry) {
position = oldPos + offsetDelta + (oldPos == 0 ? 0 : 1);
boolean match;
if (exclusive)
match = oldPos < where && where <= position;
// We optimize this expression by hand:
// match = (oldPos == 0 && where == 0 && (0 < position || 0 == position))
// || oldPos < where && where <= position;
match = (oldPos == 0 && where == 0)
|| oldPos < where && where <= position;
else
match = oldPos <= where && where < position;

Expand Down Expand Up @@ -931,7 +935,8 @@ void update(int pos, int offsetDelta) {
position = oldPos + offsetDelta + (oldPos == 0 ? 0 : 1);
boolean match;
if (exclusive)
match = oldPos < where && where <= position;
match = (oldPos == 0 && where == 0)
|| oldPos < where && where <= position;
else
match = oldPos <= where && where < position;

Expand Down
75 changes: 75 additions & 0 deletions src/test/javassist/bytecode/StackMapTest.java
Expand Up @@ -848,4 +848,79 @@ public void testIssue328() throws Exception {
cc.writeFile();
Object t1 = make(cc.getName());
}

public static class C8 {
int value = 0;
int loop = 0;
int loop2 = 1;
public void foo(int i) { value += i; }
public void bar(int i) { value += i; }
public void foo2(int i) { value += i; }
public void bar2(int i) { value += i; }
}

public static class C9 extends C8 {
public void foo(int i) {
while(true) {
loop--;
if (loop < 0)
break;
}
value += i;
}
public void bar(int i) {
value += i;
for (int k = 0; i < 10; k++)
loop += k;
}
public void foo2(int i) {
while(true) {
loop2--;
if (loop2 < 0)
break;
}
value += i;
for (int k = 0; k < 3; k++)
loop2--;
}
public void bar2(int i) {
value += i;
for (int k = 0; i < 10; k++)
loop += k;
}
public int run() {
foo(1);
bar(10);
foo2(100);
bar2(1000);
return value;
}
}

public void testIssue339() throws Exception {
CtClass cc0 = loader.get("javassist.bytecode.StackMapTest$C8");
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C9");

testIssue339b(cc, cc0, "foo", true);
testIssue339b(cc, cc0, "bar", true);
testIssue339b(cc, cc0, "foo2", false);
testIssue339b(cc, cc0, "bar2", false);

cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(2322, invoke(t1, "run"));
}

public void testIssue339b(CtClass cc, CtClass cc0, String name, boolean exclusive) throws Exception {
Bytecode newCode = new Bytecode(cc.getClassFile().constPool);
newCode.addAload(0); // Loads 'this'
newCode.addIload(1); // Loads method param 1 (int)
newCode.addInvokespecial(cc0.getName(), name, "(I)V");
CodeAttribute ca = cc.getDeclaredMethod(name).getMethodInfo().getCodeAttribute();
CodeIterator ci = ca.iterator();
if (exclusive)
ci.insertEx(newCode.get());
else
ci.insert(newCode.get());
}
}

0 comments on commit 1c4e31b

Please sign in to comment.