Skip to content

Commit

Permalink
8321156: Improve the handling of invalid UTF-8 byte sequences for Zip…
Browse files Browse the repository at this point in the history
…InputStream::getNextEntry and ZipFile::getComment

Reviewed-by: jpai, alanb
  • Loading branch information
Lance Andersen committed Feb 26, 2024
1 parent c042f08 commit 20c71ce
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 113 deletions.
14 changes: 11 additions & 3 deletions src/java.base/share/classes/java/util/zip/ZipFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -308,7 +308,9 @@ public ZipFile(File file, Charset charset) throws IOException
}

/**
* Returns the zip file comment, or null if none.
* Returns the zip file comment. If a comment does not exist or an error is
* encountered decoding the comment using the charset specified
* when opening the Zip file, then {@code null} is returned.
*
* @return the comment string for the zip file, or null if none
*
Expand All @@ -322,7 +324,13 @@ public String getComment() {
if (res.zsrc.comment == null) {
return null;
}
return res.zsrc.zc.toString(res.zsrc.comment);
// If there is a problem decoding the byte array which represents
// the Zip file comment, return null;
try {
return res.zsrc.zc.toString(res.zsrc.comment);
} catch (IllegalArgumentException iae) {
return null;
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/java.base/share/classes/java/util/zip/ZipInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,16 @@ private ZipEntry readLOC() throws IOException {
}
readFully(b, 0, len);
// Force to use UTF-8 if the USE_UTF8 bit is ON
ZipEntry e = createZipEntry(((flag & USE_UTF8) != 0)
? ZipCoder.toStringUTF8(b, len)
: zc.toString(b, len));
String entryName;
try {
entryName = ((flag & USE_UTF8) != 0) ?
ZipCoder.toStringUTF8(b, len)
: zc.toString(b, len);
} catch (Exception ex) {
throw (ZipException) new ZipException(
"invalid LOC header (bad entry name)").initCause(ex);
}
ZipEntry e = createZipEntry(entryName);
// now get the remaining fields for the entry
if ((flag & 1) == 1) {
throw new ZipException("encrypted ZIP entry not supported");
Expand Down
Loading

1 comment on commit 20c71ce

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.