From 38f4bc677b4265c9b0a21b929be067514afd3d5d Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 21 Feb 2014 05:25:56 +0000 Subject: [PATCH] be more lenient when parsing tar headers git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1570454 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++++ src/main/org/apache/tools/tar/TarUtils.java | 21 ++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 3a65c2d38..1add234ec 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -84,6 +84,10 @@ Fixed bugs: extend Javadoc Bugzilla Report 56047 + * TarInputStream will now read archives created by tar + implementations that encode big numbers by not adding a trailing + NUL. + Other changes: -------------- diff --git a/src/main/org/apache/tools/tar/TarUtils.java b/src/main/org/apache/tools/tar/TarUtils.java index e88f6b40d..cc4106349 100644 --- a/src/main/org/apache/tools/tar/TarUtils.java +++ b/src/main/org/apache/tools/tar/TarUtils.java @@ -121,19 +121,18 @@ public class TarUtils { } } - // Must have trailing NUL or space - byte trailer; - trailer = buffer[end-1]; - if (trailer == 0 || trailer == ' '){ + // Trim all trailing NULs and spaces. + // The ustar and POSIX tar specs require a trailing NUL or + // space but some implementations use the extra digit for big + // sizes/uids/gids ... + byte trailer = buffer[end - 1]; + while (start < end && (trailer == 0 || trailer == ' ')) { end--; - } else { - throw new IllegalArgumentException( - exceptionMessage(buffer, offset, length, end-1, trailer)); + trailer = buffer[end - 1]; } - // May have additional NUL or space - trailer = buffer[end-1]; - if (trailer == 0 || trailer == ' '){ - end--; + if (start == end) { + throw new IllegalArgumentException( + exceptionMessage(buffer, offset, length, start, trailer)); } for ( ;start < end; start++) {