From eeddfd1298b3d337b89345349713a5cb7f3d6cd3 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 13 Jan 2015 18:25:15 +0400 Subject: [PATCH] Fix bugs that were found by FindBugs. Fix https://github.com/konsoletyper/teavm/issues/46 https://github.com/konsoletyper/teavm/issues/45 https://github.com/konsoletyper/teavm/issues/44 https://github.com/konsoletyper/teavm/issues/42 --- .../java/io/TBufferedInputStream.java | 2 +- .../classlib/java/io/TFilterOutputStream.java | 2 +- .../classlib/java/lang/TStringBuffer.java | 2 +- .../classlib/java/lang/TStringBuilder.java | 2 +- .../java/text/TDateFormatSymbols.java | 2 +- .../org/teavm/classlib/java/util/TBitSet.java | 16 +++++++----- .../org/teavm/classlib/java/util/TRandom.java | 2 +- .../teavm/classlib/java/util/BitSetTest.java | 26 ++++++++++++++----- .../src/main/java/org/teavm/cache/AstIO.java | 6 +++-- .../java/org/teavm/debugging/Debugger.java | 4 +-- .../model/util/InterferenceGraphBuilder.java | 2 +- .../org/teavm/dom/ajax/XMLHttpRequest.java | 2 +- 12 files changed, 43 insertions(+), 25 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java index eac3fa937..8d701a40f 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TBufferedInputStream.java @@ -64,7 +64,7 @@ public class TBufferedInputStream extends TFilterInputStream { if (result > 0) { markpos = -1; pos = 0; - count = result == -1 ? 0 : result; + count = result; } return result; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterOutputStream.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterOutputStream.java index bcafb5a26..0e2c49dbe 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterOutputStream.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/io/TFilterOutputStream.java @@ -38,7 +38,7 @@ public class TFilterOutputStream extends TOutputStream { } catch (TIOException e) { // do nothing } - close(); + out.close(); } @Override diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java index ee9921dad..e428aecf4 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuffer.java @@ -200,7 +200,7 @@ public class TStringBuffer extends TAbstractStringBuilder implements TAppendable @Override public TStringBuffer deleteCharAt(int index) { - deleteCharAt(index); + super.deleteCharAt(index); return this; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java index 92fd8ec96..abe9eeb10 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TStringBuilder.java @@ -200,7 +200,7 @@ public class TStringBuilder extends TAbstractStringBuilder implements TAppendabl @Override public TStringBuilder deleteCharAt(int index) { - deleteCharAt(index); + super.deleteCharAt(index); return this; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java index 3cba349cc..4d384c103 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/text/TDateFormatSymbols.java @@ -109,7 +109,7 @@ public class TDateFormatSymbols implements TSerializable, TCloneable { return false; } for (int j = 0; j < element.length; j++) { - if (element[j] != element[j] && !(element[j].equals(element[j]))) { + if (!(element[j].equals(element[j]))) { return false; } } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java index e96a6a76f..40b349a3b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TBitSet.java @@ -54,19 +54,21 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { int[] ints = new int[(bytes.length + 3) / 4]; int fullInts = bytes.length / 4; for (int i = 0; i < fullInts; ++i) { - ints[i] = bytes[i * 4] | (bytes[i * 4 + 1] << 8) | (bytes[i * 4 + 2] << 16) | (bytes[i * 4 + 3] << 24); + ints[i] = (bytes[i * 4] & 0xFF) | ((bytes[i * 4 + 1] & 0xFF) << 8) | ((bytes[i * 4 + 2] & 0xFF) << 16) | + ((bytes[i * 4 + 3] & 0xFF) << 24); } int lastInt = ints.length - 1; - int lastByte = bytes[lastInt * 4]; + int lastByte = lastInt * 4; switch (bytes.length % 4) { case 3: - ints[lastInt] = bytes[lastByte] | (bytes[lastByte + 1] << 8) | (bytes[lastByte + 2] << 16); + ints[lastInt] = (bytes[lastByte] & 0xFF) | ((bytes[lastByte + 1] & 0xFF) << 8) | + ((bytes[lastByte + 2] & 0xFF) << 16); break; case 2: - ints[lastInt] = bytes[lastByte] | (bytes[lastByte + 1] << 8); + ints[lastInt] = (bytes[lastByte] & 0xFF) | ((bytes[lastByte + 1] & 0xFF) << 8); break; case 1: - ints[lastInt] = bytes[lastByte]; + ints[lastInt] = bytes[lastByte] & 0xFF; break; } return new TBitSet(ints); @@ -105,9 +107,9 @@ public class TBitSet extends TObject implements TCloneable, TSerializable { int fullLongs = length / 64; int i = 0; for (; i < fullLongs; ++i) { - longs[i] = data[i * 2] | (data[i * 2 + 1] << 32); + longs[i] = data[i * 2] | ((long)data[i * 2 + 1] << 32); } - if (((31 + length) / 32) % 2 == 1) { + if ((((31 + length) / 32) & 1) == 1) { longs[i] = data[i * 2]; } return longs; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java index 366e9b310..5d71eed1c 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TRandom.java @@ -39,7 +39,7 @@ public class TRandom extends TObject implements TSerializable { } public void nextBytes(byte[] bytes) { - for (int i = 0; i < bytes.length; ) { + for (int i = 0; i < bytes.length; ++i) { bytes[i] = (byte)next(8); } } diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/BitSetTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/BitSetTest.java index 7089e35b4..2624dadc6 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/BitSetTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/BitSetTest.java @@ -32,11 +32,11 @@ package org.teavm.classlib.java.util; import static org.junit.Assert.*; +import java.util.Arrays; import java.util.BitSet; import org.junit.Test; public class BitSetTest { - BitSet eightbs; public BitSetTest() { @@ -58,6 +58,23 @@ public class BitSetTest { assertEquals("New BitSet had invalid string representation: " + bs, "{}", bs.toString()); } + @Test + public void constructFromBytes() { + for (int i = 4; i < 8; ++i) { + byte[] bytes = new byte[i]; + Arrays.fill(bytes, (byte)0x80); + BitSet bs = BitSet.valueOf(bytes); + assertEquals("Wrong length of BitSet", i * 8, bs.length()); + for (int j = 0; j < bs.length(); ++j) { + if (j % 8 == 7) { + assertTrue("Expected that " + j + "th bit is to be set", bs.get(j)); + } else { + assertFalse("Expected that " + j + "th bit is not to be set", bs.get(j)); + } + } + } + } + @Test public void clonePerformed() { BitSet bs; @@ -76,12 +93,9 @@ public class BitSetTest { bs = (BitSet) eightbs.clone(); bs.set(128); - assertFalse("Different sized BitSet with higher bit set returned true", - eightbs.equals(bs)); + assertFalse("Different sized BitSet with higher bit set returned true", eightbs.equals(bs)); bs.clear(128); - assertTrue( - "Different sized BitSet with higher bits not set returned false", - eightbs.equals(bs)); + assertTrue("Different sized BitSet with higher bits not set returned false", eightbs.equals(bs)); } @Test diff --git a/teavm-core/src/main/java/org/teavm/cache/AstIO.java b/teavm-core/src/main/java/org/teavm/cache/AstIO.java index 153495913..b750fddb4 100644 --- a/teavm-core/src/main/java/org/teavm/cache/AstIO.java +++ b/teavm-core/src/main/java/org/teavm/cache/AstIO.java @@ -53,8 +53,10 @@ public class AstIO { output.writeShort(method.getParameterDebugNames().size()); for (Set debugNames : method.getParameterDebugNames()) { output.writeShort(debugNames != null ? debugNames.size() : 0); - for (String debugName : debugNames) { - output.writeUTF(debugName); + if (debugNames != null) { + for (String debugName : debugNames) { + output.writeUTF(debugName); + } } } output.writeBoolean(method.isOriginalNamePreserved()); diff --git a/teavm-core/src/main/java/org/teavm/debugging/Debugger.java b/teavm-core/src/main/java/org/teavm/debugging/Debugger.java index 39ebd672e..c05332e23 100644 --- a/teavm-core/src/main/java/org/teavm/debugging/Debugger.java +++ b/teavm-core/src/main/java/org/teavm/debugging/Debugger.java @@ -224,7 +224,7 @@ public class Debugger { } public Breakpoint createBreakpoint(SourceLocation location) { - synchronized (breakpoints) { + synchronized (breakpointMap) { Breakpoint breakpoint = new Breakpoint(this, location); breakpoints.put(breakpoint, dummyObject); updateInternalBreakpoints(breakpoint); @@ -366,7 +366,7 @@ public class Debugger { breakpointMap.remove(jsBreakpoint); } breakpoint.jsBreakpoints = new ArrayList<>(); - breakpoints.remove(this); + breakpoints.remove(breakpoint); } private void fireResumed() { diff --git a/teavm-core/src/main/java/org/teavm/model/util/InterferenceGraphBuilder.java b/teavm-core/src/main/java/org/teavm/model/util/InterferenceGraphBuilder.java index 1ef421e12..54c42aec7 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/InterferenceGraphBuilder.java +++ b/teavm-core/src/main/java/org/teavm/model/util/InterferenceGraphBuilder.java @@ -60,7 +60,7 @@ class InterferenceGraphBuilder { } for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { if (tryCatch.getExceptionVariable() != null) { - live.remove(tryCatch.getExceptionVariable()); + live.remove(nodes.get(tryCatch.getExceptionVariable().getIndex())); } } for (int j = block.getInstructions().size() - 1; j >= 0; --j) { diff --git a/teavm-dom/src/main/java/org/teavm/dom/ajax/XMLHttpRequest.java b/teavm-dom/src/main/java/org/teavm/dom/ajax/XMLHttpRequest.java index 026a0129e..88e5adff9 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/ajax/XMLHttpRequest.java +++ b/teavm-dom/src/main/java/org/teavm/dom/ajax/XMLHttpRequest.java @@ -63,7 +63,7 @@ public interface XMLHttpRequest extends JSObject { Document getResponseXML(); @JSProperty - Integer getStatus(); + int getStatus(); @JSProperty String getStatusText();