This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit ed7b72a5ad6ac280f72f5c538397cc5580c1f225 Author: Gary Gregory <[email protected]> AuthorDate: Mon Aug 10 18:27:42 2026 -0400 Internal TAR methods now throws ArchiveException instead of IllegalArgumentException. --- .../compress/archivers/tar/TarArchiveEntry.java | 5 ++-- .../commons/compress/archivers/tar/TarUtils.java | 32 ++++++++++++---------- .../compress/archivers/tar/TarUtilsTest.java | 24 ++++++++-------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java index ecbb79ee0..8a8114551 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java @@ -2069,7 +2069,8 @@ public void writeEntryHeader(final byte[] outbuf, final ZipEncoding encoding, fi TarUtils.formatCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN); } - private int writeEntryHeaderField(final long value, final byte[] outbuf, final int offset, final int length, final boolean starMode) { + private int writeEntryHeaderField(final long value, final byte[] outbuf, final int offset, final int length, final boolean starMode) + throws ArchiveException { if (!starMode && (value < 0 || value >= 1L << 3 * (length - 1))) { // value doesn't fit into field when written as octal // number, will be written to PAX header or causes an @@ -2079,7 +2080,7 @@ private int writeEntryHeaderField(final long value, final byte[] outbuf, final i return TarUtils.formatLongOctalOrBinaryBytes(value, outbuf, offset, length); } - private int writeEntryHeaderOptionalTimeField(final FileTime time, int offset, final byte[] outbuf, final int fieldLength) { + private int writeEntryHeaderOptionalTimeField(final FileTime time, int offset, final byte[] outbuf, final int fieldLength) throws ArchiveException { if (time != null) { offset = writeEntryHeaderField(FileTimes.toUnixTime(time), outbuf, offset, fieldLength, true); } else { diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java index b98cefb5d..6d329c00e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java @@ -150,12 +150,13 @@ private static String exceptionMessage(final byte[] buffer, final int offset, fi return "Invalid byte " + currentByte + " at offset " + (current - offset) + " in '" + string + "' len=" + length; } - private static void formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) { + private static void formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) + throws ArchiveException { final BigInteger val = BigInteger.valueOf(value); final byte[] b = val.toByteArray(); final int len = b.length; if (len > length - 1) { - throw new IllegalArgumentException("Value " + value + " is too large for " + length + " byte field."); + throw new ArchiveException("Value " + value + " is too large for " + length + " byte field."); } final int off = offset + length - len; System.arraycopy(b, 0, buf, off, len); @@ -173,9 +174,9 @@ private static void formatBigIntegerBinary(final long value, final byte[] buf, f * @param offset The starting offset into the buffer. * @param length The size of the buffer. * @return The updated value of offset, i.e. offset+length. - * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer. + * @throws ArchiveException if the value (and trailer) will not fit in the buffer. */ - public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length) { + public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length) throws ArchiveException { int idx = length - 2; // for NUL and space formatUnsignedOctalString(value, buf, offset, idx); buf[offset + idx++] = 0; // Trailing null @@ -183,12 +184,13 @@ public static int formatCheckSumOctalBytes(final long value, final byte[] buf, f return offset + length; } - private static void formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) { + private static void formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) + throws ArchiveException { final int bits = (length - 1) * 8; final long max = 1L << bits; long val = Math.abs(value); // Long.MIN_VALUE stays Long.MIN_VALUE if (val < 0 || val >= max) { - throw new IllegalArgumentException("Value " + value + " is too large for " + length + " byte field."); + throw new ArchiveException("Value " + value + " is too large for " + length + " byte field."); } if (negative) { val ^= max - 1; @@ -211,9 +213,9 @@ private static void formatLongBinary(final long value, final byte[] buf, final i * @param offset The starting offset into the buffer. * @param length The length of the buffer. * @return The updated offset. - * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer. + * @throws ArchiveException if the value (and trailer) will not fit in the buffer. */ - public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length) { + public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length) throws ArchiveException { final int idx = length - 1; // For space formatUnsignedOctalString(value, buf, offset, idx); buf[offset + idx] = (byte) SP; // Trailing space @@ -230,10 +232,10 @@ public static int formatLongOctalBytes(final long value, final byte[] buf, final * @param offset The starting offset into the buffer. * @param length The length of the buffer. * @return The updated offset. - * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer. + * @throws ArchiveException if the value (and trailer) will not fit in the buffer. * @since 1.4 */ - public static int formatLongOctalOrBinaryBytes(final long value, final byte[] buf, final int offset, final int length) { + public static int formatLongOctalOrBinaryBytes(final long value, final byte[] buf, final int offset, final int length) throws ArchiveException { // Check whether we are dealing with UID/GID or SIZE field final long maxAsOctalChar = length == TarConstants.UIDLEN ? TarConstants.MAXID : TarConstants.MAXSIZE; final boolean negative = value < 0; @@ -308,9 +310,9 @@ public static int formatNameBytes(final String name, final byte[] buf, final int * @param offset The starting offset into the buffer. * @param length The size of the output buffer. * @return The updated offset, i.e. offset+length. - * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer. + * @throws ArchiveException if the value (and trailer) will not fit in the buffer. */ - public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length) { + public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length) throws ArchiveException { int idx = length - 2; // For space and trailing null formatUnsignedOctalString(value, buf, offset, idx); buf[offset + idx++] = (byte) SP; // Trailing space @@ -325,9 +327,9 @@ public static int formatOctalBytes(final long value, final byte[] buf, final int * @param buffer destination buffer. * @param offset starting offset in buffer. * @param length length of buffer to fill. - * @throws IllegalArgumentException if the value will not fit in the buffer. + * @throws ArchiveException if the value will not fit in the buffer. */ - public static void formatUnsignedOctalString(final long value, final byte[] buffer, final int offset, final int length) { + public static void formatUnsignedOctalString(final long value, final byte[] buffer, final int offset, final int length) throws ArchiveException { int remaining = length; remaining--; if (value == 0) { @@ -341,7 +343,7 @@ public static void formatUnsignedOctalString(final long value, final byte[] buff // CheckStyle:MagicNumber ON } if (val != 0) { - throw new IllegalArgumentException(value + "=" + Long.toOctalString(value) + " will not fit in octal number buffer of length " + length); + throw new ArchiveException(value + "=" + Long.toOctalString(value) + " will not fit in octal number buffer of length " + length); } } for (; remaining >= 0; --remaining) { // leading zeros diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java index 2ef512fa4..351d0586b 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java @@ -184,18 +184,18 @@ private void checkName(final String string) { assertEquals(string, TarUtils.parseName(buff, 0, len)); } - private void checkRoundTripOctal(final long value) { + private void checkRoundTripOctal(final long value) throws ArchiveException { checkRoundTripOctal(value, TarConstants.SIZELEN); } - private void checkRoundTripOctal(final long value, final int bufsize) { + private void checkRoundTripOctal(final long value, final int bufsize) throws ArchiveException { final byte[] buffer = new byte[bufsize]; TarUtils.formatLongOctalBytes(value, buffer, 0, buffer.length); final long parseValue = TarUtils.parseOctal(buffer, 0, buffer.length); assertEquals(value, parseValue); } - private void checkRoundTripOctalOrBinary(final long value, final int bufsize) { + private void checkRoundTripOctalOrBinary(final long value, final int bufsize) throws ArchiveException { final byte[] buffer = new byte[bufsize]; TarUtils.formatLongOctalOrBinaryBytes(value, buffer, 0, buffer.length); final long parseValue = TarUtils.parseOctalOrBinary(buffer, 0, buffer.length); @@ -223,18 +223,18 @@ void testName() { } @Test - void testNegative() { + void testNegative() throws ArchiveException { final byte[] buffer = new byte[22]; TarUtils.formatUnsignedOctalString(-1, buffer, 0, buffer.length); assertEquals("1777777777777777777777", new String(buffer, UTF_8)); } @Test - void testOverflow() { + void testOverflow() throws ArchiveException { final byte[] buffer = new byte[8 - 1]; // a lot of the numbers have 8-byte buffers (nul term) TarUtils.formatUnsignedOctalString(07777777L, buffer, 0, buffer.length); assertEquals("7777777", new String(buffer, UTF_8)); - assertThrows(IllegalArgumentException.class, () -> TarUtils.formatUnsignedOctalString(017777777L, buffer, 0, buffer.length), + assertThrows(ArchiveException.class, () -> TarUtils.formatUnsignedOctalString(017777777L, buffer, 0, buffer.length), "Should have cause IllegalArgumentException"); } @@ -640,7 +640,7 @@ void testRoundTripNames() { } @Test - void testRoundTripOctal() { + void testRoundTripOctal() throws ArchiveException { checkRoundTripOctal(0); checkRoundTripOctal(1); // checkRoundTripOctal(-1); // TODO What should this do? @@ -652,7 +652,7 @@ void testRoundTripOctal() { checkRoundTripOctal(TarConstants.MAXID, 8); } - private void testRoundTripOctalOrBinary(final int length) { + private void testRoundTripOctalOrBinary(final int length) throws ArchiveException { checkRoundTripOctalOrBinary(0, length); checkRoundTripOctalOrBinary(1, length); checkRoundTripOctalOrBinary(TarConstants.MAXSIZE, length); // will need binary format @@ -662,20 +662,20 @@ private void testRoundTripOctalOrBinary(final int length) { } @Test - void testRoundTripOctalOrBinary12() { + void testRoundTripOctalOrBinary12() throws ArchiveException { testRoundTripOctalOrBinary(12); checkRoundTripOctalOrBinary(Long.MAX_VALUE, 12); checkRoundTripOctalOrBinary(Long.MIN_VALUE + 1, 12); } @Test - void testRoundTripOctalOrBinary8() { + void testRoundTripOctalOrBinary8() throws ArchiveException { testRoundTripOctalOrBinary(8); } @Test void testRoundTripOctalOrBinary8_ValueTooBigForBinary() { - final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> checkRoundTripOctalOrBinary(Long.MAX_VALUE, 8), + final ArchiveException e = assertThrows(ArchiveException.class, () -> checkRoundTripOctalOrBinary(Long.MAX_VALUE, 8), "Should throw exception - value is too long to fit buffer of this len"); assertEquals("Value 9223372036854775807 is too large for 8 byte field.", e.getMessage()); } @@ -690,7 +690,7 @@ void testSecondEntryWinsWhenPaxHeaderContainsDuplicateKey() throws Exception { // Check correct trailing bytes are generated @Test - void testTrailers() { + void testTrailers() throws ArchiveException { final byte[] buffer = new byte[12]; TarUtils.formatLongOctalBytes(123, buffer, 0, buffer.length); assertEquals(' ', buffer[buffer.length - 1]);
