Browse Source

fix a bunch of findbugs reported problems in the zip, tar and bzip2 classes. PR 46661

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@741089 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
817fd31998
11 changed files with 99 additions and 13 deletions
  1. +2
    -4
      src/main/org/apache/tools/bzip2/CBZip2OutputStream.java
  2. +7
    -2
      src/main/org/apache/tools/tar/TarInputStream.java
  3. +1
    -1
      src/main/org/apache/tools/tar/TarOutputStream.java
  4. +10
    -0
      src/main/org/apache/tools/zip/AsiExtraField.java
  5. +13
    -4
      src/main/org/apache/tools/zip/UnrecognizedExtraField.java
  6. +18
    -2
      src/main/org/apache/tools/zip/ZipFile.java
  7. +9
    -0
      src/main/org/apache/tools/zip/ZipLong.java
  8. +9
    -0
      src/main/org/apache/tools/zip/ZipShort.java
  9. +16
    -0
      src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java
  10. +7
    -0
      src/tests/junit/org/apache/tools/zip/ZipLongTest.java
  11. +7
    -0
      src/tests/junit/org/apache/tools/zip/ZipShortTest.java

+ 2
- 4
src/main/org/apache/tools/bzip2/CBZip2OutputStream.java View File

@@ -613,7 +613,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants {
} }


if (ge > gs && nPart != nGroups && nPart != 1 if (ge > gs && nPart != nGroups && nPart != 1
&& ((nGroups - nPart) % 2 == 1)) {
&& ((nGroups - nPart) % 2 != 0)) {
aFreq -= mtfFreq[ge]; aFreq -= mtfFreq[ge];
ge--; ge--;
} }
@@ -983,9 +983,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants {
b = t; b = t;
} }
if (b > c) { if (b > c) {
t = b;
b = c; b = c;
c = t;
} }
if (a > b) { if (a > b) {
b = a; b = a;
@@ -1030,7 +1028,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants {


med = med3(block[zptr[lo] + d + 1], med = med3(block[zptr[lo] + d + 1],
block[zptr[hi ] + d + 1], block[zptr[hi ] + d + 1],
block[zptr[(lo + hi) >> 1] + d + 1]);
block[zptr[(lo + hi) >>> 1] + d + 1]);


unLo = ltLo = lo; unLo = ltLo = lo;
unHi = gtHi = hi; unHi = gtHi = hi;


+ 7
- 2
src/main/org/apache/tools/tar/TarInputStream.java View File

@@ -218,8 +218,13 @@ public class TarInputStream extends FilterInputStream {
+ numToSkip + " bytes"); + numToSkip + " bytes");
} }


if (numToSkip > 0) {
skip(numToSkip);
while (numToSkip > 0) {
long skipped = skip(numToSkip);
if (skipped <= 0) {
throw new RuntimeException("failed to skip current tar"
+ " entry");
}
numToSkip -= skipped;
} }


readBuf = null; readBuf = null;


+ 1
- 1
src/main/org/apache/tools/tar/TarOutputStream.java View File

@@ -310,7 +310,7 @@ public class TarOutputStream extends FilterOutputStream {


wOffset += numToWrite; wOffset += numToWrite;
assemLen += numToWrite; assemLen += numToWrite;
numToWrite -= numToWrite;
numToWrite = 0;
} }
} }




+ 10
- 0
src/main/org/apache/tools/zip/AsiExtraField.java View File

@@ -334,4 +334,14 @@ public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
return type | (mode & PERM_MASK); return type | (mode & PERM_MASK);
} }


public Object clone() {
try {
AsiExtraField cloned = (AsiExtraField) super.clone();
cloned.crc = new CRC32();
return cloned;
} catch (CloneNotSupportedException cnfe) {
// impossible
throw new RuntimeException(cnfe);
}
}
} }

+ 13
- 4
src/main/org/apache/tools/zip/UnrecognizedExtraField.java View File

@@ -65,7 +65,7 @@ public class UnrecognizedExtraField implements ZipExtraField {
* @param data the field data to use * @param data the field data to use
*/ */
public void setLocalFileDataData(byte[] data) { public void setLocalFileDataData(byte[] data) {
localData = data;
localData = copy(data);
} }


/** /**
@@ -81,7 +81,7 @@ public class UnrecognizedExtraField implements ZipExtraField {
* @return the local data * @return the local data
*/ */
public byte[] getLocalFileDataData() { public byte[] getLocalFileDataData() {
return localData;
return copy(localData);
} }


/** /**
@@ -97,7 +97,7 @@ public class UnrecognizedExtraField implements ZipExtraField {
* @param data the data to use * @param data the data to use
*/ */
public void setCentralDirectoryData(byte[] data) { public void setCentralDirectoryData(byte[] data) {
centralData = data;
centralData = copy(data);
} }


/** /**
@@ -118,7 +118,7 @@ public class UnrecognizedExtraField implements ZipExtraField {
*/ */
public byte[] getCentralDirectoryData() { public byte[] getCentralDirectoryData() {
if (centralData != null) { if (centralData != null) {
return centralData;
return copy(centralData);
} }
return getLocalFileDataData(); return getLocalFileDataData();
} }
@@ -134,4 +134,13 @@ public class UnrecognizedExtraField implements ZipExtraField {
System.arraycopy(data, offset, tmp, 0, length); System.arraycopy(data, offset, tmp, 0, length);
setLocalFileDataData(tmp); setLocalFileDataData(tmp);
} }

private static byte[] copy(byte[] from) {
if (from != null) {
byte[] to = new byte[from.length];
System.arraycopy(from, 0, to, 0, to.length);
return to;
}
return null;
}
} }

+ 18
- 2
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -345,7 +345,15 @@ public class ZipFile {


nameMap.put(ze.getName(), ze); nameMap.put(ze.getName(), ze);


archive.skipBytes(extraLen);
int lenToSkip = extraLen;
while (lenToSkip > 0) {
int skipped = archive.skipBytes(lenToSkip);
if (skipped <= 0) {
throw new RuntimeException("failed to skip extra data in"
+ " central directory");
}
lenToSkip -= skipped;
}


byte[] comment = new byte[commentLen]; byte[] comment = new byte[commentLen];
archive.readFully(comment); archive.readFully(comment);
@@ -461,7 +469,15 @@ public class ZipFile {
int fileNameLen = ZipShort.getValue(b); int fileNameLen = ZipShort.getValue(b);
archive.readFully(b); archive.readFully(b);
int extraFieldLen = ZipShort.getValue(b); int extraFieldLen = ZipShort.getValue(b);
archive.skipBytes(fileNameLen);
int lenToSkip = fileNameLen;
while (lenToSkip > 0) {
int skipped = archive.skipBytes(lenToSkip);
if (skipped <= 0) {
throw new RuntimeException("failed to skip file name in"
+ " local file header");
}
lenToSkip -= skipped;
}
byte[] localExtraData = new byte[extraFieldLen]; byte[] localExtraData = new byte[extraFieldLen];
archive.readFully(localExtraData); archive.readFully(localExtraData);
ze.setExtra(localExtraData); ze.setExtra(localExtraData);


+ 9
- 0
src/main/org/apache/tools/zip/ZipLong.java View File

@@ -147,4 +147,13 @@ public final class ZipLong implements Cloneable {
public int hashCode() { public int hashCode() {
return (int) value; return (int) value;
} }

public Object clone() {
try {
return (ZipLong) super.clone();
} catch (CloneNotSupportedException cnfe) {
// impossible
throw new RuntimeException(cnfe);
}
}
} }

+ 9
- 0
src/main/org/apache/tools/zip/ZipShort.java View File

@@ -133,4 +133,13 @@ public final class ZipShort implements Cloneable {
public int hashCode() { public int hashCode() {
return value; return value;
} }

public Object clone() {
try {
return (ZipShort) super.clone();
} catch (CloneNotSupportedException cnfe) {
// impossible
throw new RuntimeException(cnfe);
}
}
} }

+ 16
- 0
src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java View File

@@ -138,4 +138,20 @@ public class AsiExtraFieldTest extends TestCase implements UnixStat {
e.getMessage()); e.getMessage());
} }
} }

public void testClone() {
AsiExtraField s1 = new AsiExtraField();
s1.setUserId(42);
s1.setGroupId(12);
s1.setLinkedFile("foo");
s1.setMode(0644);
s1.setDirectory(true);
AsiExtraField s2 = (AsiExtraField) s1.clone();
assertNotSame(s1, s2);
assertEquals(s1.getUserId(), s2.getUserId());
assertEquals(s1.getGroupId(), s2.getGroupId());
assertEquals(s1.getLinkedFile(), s2.getLinkedFile());
assertEquals(s1.getMode(), s2.getMode());
assertEquals(s1.isDirectory(), s2.isDirectory());
}
} }

+ 7
- 0
src/tests/junit/org/apache/tools/zip/ZipLongTest.java View File

@@ -79,4 +79,11 @@ public class ZipLongTest extends TestCase {
assertEquals(0x00000000FFFFFFFFl, zl.getValue()); assertEquals(0x00000000FFFFFFFFl, zl.getValue());
} }


public void testClone() {
ZipLong s1 = new ZipLong(42);
ZipLong s2 = (ZipLong) s1.clone();
assertNotSame(s1, s2);
assertEquals(s1, s2);
assertEquals(s1.getValue(), s2.getValue());
}
} }

+ 7
- 0
src/tests/junit/org/apache/tools/zip/ZipShortTest.java View File

@@ -77,4 +77,11 @@ public class ZipShortTest extends TestCase {
assertEquals(0x0000FFFF, zs.getValue()); assertEquals(0x0000FFFF, zs.getValue());
} }


public void testClone() {
ZipShort s1 = new ZipShort(42);
ZipShort s2 = (ZipShort) s1.clone();
assertNotSame(s1, s2);
assertEquals(s1, s2);
assertEquals(s1.getValue(), s2.getValue());
}
} }

Loading…
Cancel
Save