diff --git a/WHATSNEW b/WHATSNEW index fb1579a51..57de52a01 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -54,6 +54,10 @@ Changes that could break older environments: however the configuration-from-String behavior remains equivalent, rendering a FileResource. + * CBZip2InputStream will now throw an IOException if + passed in a null or empty InputStream to read from. + Bugzilla Report 32200 + Fixed bugs: ----------- @@ -112,6 +116,10 @@ Fixed bugs: * creates remoteToDir if it doesn't exist. Bugzilla Report 42781 + * CBZip2OutputStream threw an exception if it was closed prior to + writing anything. + Bugzilla Report 32200 + Other changes: -------------- diff --git a/src/main/org/apache/tools/bzip2/CBZip2InputStream.java b/src/main/org/apache/tools/bzip2/CBZip2InputStream.java index ebb5f607f..14cfd0337 100644 --- a/src/main/org/apache/tools/bzip2/CBZip2InputStream.java +++ b/src/main/org/apache/tools/bzip2/CBZip2InputStream.java @@ -23,8 +23,9 @@ */ package org.apache.tools.bzip2; -import java.io.InputStream; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; /** * An input stream that decompresses from the BZip2 format (without the file @@ -223,6 +224,12 @@ public class CBZip2InputStream extends InputStream implements BZip2Constants { } private void init() throws IOException { + if (null == in) { + throw new IOException("No InputStream"); + } + if (in.available() == 0) { + throw new IOException("Empty InputStream"); + } int magic2 = this.in.read(); if (magic2 != 'h') { throw new IOException("Stream is not BZip2 formatted: expected 'h'" diff --git a/src/main/org/apache/tools/bzip2/CBZip2OutputStream.java b/src/main/org/apache/tools/bzip2/CBZip2OutputStream.java index c99c91509..98c0a2300 100644 --- a/src/main/org/apache/tools/bzip2/CBZip2OutputStream.java +++ b/src/main/org/apache/tools/bzip2/CBZip2OutputStream.java @@ -503,7 +503,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { code[i] = vec; vec++; } - }; + } vec <<= 1; } } @@ -703,7 +703,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { bc = cost[t]; bt = t; } - }; + } totc += bc; fave[bt]++; selector[nSelectors] = (char) bt; @@ -1047,7 +1047,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { ltLo++; unLo++; continue; - }; + } if (n > 0) { break; } @@ -1066,7 +1066,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { gtHi--; unHi--; continue; - }; + } if (n < 0) { break; } @@ -1131,6 +1131,15 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { */ // if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" ); + + // set last to zero in case it's never been set before + // see bug#32200, initBlock is the real culprit + // setting last to -1, but not sure if this -1 is important + // in normal scheme + if (last < 0) { + last = 0; + } + for (i = 0; i < NUM_OVERSHOOT_BYTES; i++) { block[last + i + 2] = block[(i % (last + 1)) + 1]; } @@ -1360,7 +1369,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { origPtr = i; break; } - }; + } if (origPtr == -1) { panic(); @@ -1478,11 +1487,11 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { if (i1 > last) { i1 -= last; i1--; - }; + } if (i2 > last) { i2 -= last; i2--; - }; + } k -= 4; workDone++; @@ -1565,7 +1574,7 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { tmp2 = tmp; tmp = yy[j]; yy[j] = tmp2; - }; + } yy[0] = tmp; if (j == 0) { @@ -1585,12 +1594,12 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { wr++; mtfFreq[RUNB]++; break; - }; + } if (zPend < 2) { break; } zPend = (zPend - 2) / 2; - }; + } zPend = 0; } szptr[wr] = (short) (j + 1); @@ -1627,6 +1636,4 @@ public class CBZip2OutputStream extends OutputStream implements BZip2Constants { nMTF = wr; } -} - - +} \ No newline at end of file diff --git a/src/tests/junit/org/apache/tools/bzip2/CBZip2StreamTest.java b/src/tests/junit/org/apache/tools/bzip2/CBZip2StreamTest.java new file mode 100644 index 000000000..5e26c2205 --- /dev/null +++ b/src/tests/junit/org/apache/tools/bzip2/CBZip2StreamTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.bzip2; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import junit.framework.TestCase; + +public class CBZip2StreamTest extends TestCase { + + public void testNullPointer() throws IOException { + try { + CBZip2InputStream cb = new CBZip2InputStream(new ByteArrayInputStream(new byte[0])); + fail("expected an exception"); + } catch (IOException e) { + // expected + } + } + + public void testDivisionByZero() throws IOException { + CBZip2OutputStream cb = new CBZip2OutputStream(new ByteArrayOutputStream()); + cb.close(); + // expected no exception + } +}