Skip to content

Commit

Permalink
8286594: (zipfs) Mention paths with dot elements in ZipException and …
Browse files Browse the repository at this point in the history
…cleanups

Reviewed-by: lancea
  • Loading branch information
RealCLanger committed May 14, 2022
1 parent 29c4b8e commit 80cf9f3
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
Expand Up @@ -1582,9 +1582,9 @@ private byte[] initCEN() throws IOException {
throw new ZipException("invalid CEN header (bad header size)");
}
IndexNode inode = new IndexNode(cen, pos, nlen);
if (hasDotOrDotDot(inode.name)) {
if (inode.pathHasDotOrDotDot()) {
throw new ZipException("ZIP file can't be opened as a file system " +
"because an entry has a '.' or '..' element in its name");
"because entry \"" + inode.nameAsString() + "\" has a '.' or '..' element in its name");
}
inodes.put(inode, inode);
if (zc.isUTF8() || (flag & FLAG_USE_UTF8) != 0) {
Expand All @@ -1602,44 +1602,6 @@ private byte[] initCEN() throws IOException {
return cen;
}

/**
* Check Inode.name to see if it includes a "." or ".." in the name array
* @param path the path as stored in Inode.name to verify
* @return true if the path contains a "." or ".." entry; false otherwise
*/
private boolean hasDotOrDotDot(byte[] path) {
// Inode.name always includes "/" in path[0]
assert path[0] == '/';
if (path.length == 1) {
return false;
}
int index = 1;
while (index < path.length) {
int starting = index;
while (index < path.length && path[index] != '/') {
index++;
}
// Check the path snippet for a "." or ".."
if (isDotOrDotDotPath(path, starting, index)) {
return true;
}
index++;
}
return false;
}

/**
* Check the path to see if it includes a "." or ".."
* @param path the path to check
* @return true if the path contains a "." or ".." entry; false otherwise
*/
private boolean isDotOrDotDotPath(byte[] path, int start, int index) {
int pathLen = index - start;
if ((pathLen == 1 && path[start] == '.'))
return true;
return (pathLen == 2 && path[start] == '.') && path[start + 1] == '.';
}

private final void checkUTF8(byte[] a) throws ZipException {
try {
int end = a.length;
Expand Down Expand Up @@ -2660,6 +2622,37 @@ boolean isDir() {
return isdir;
}

/**
* Check name if it contains a "." or ".." path element
* @return true if the path contains a "." or ".." entry; false otherwise
*/
private boolean pathHasDotOrDotDot() {
// name always includes "/" in path[0]
assert name[0] == '/';
if (name.length == 1) {
return false;
}
int index = 1;
while (index < name.length) {
int start = index;
while (index < name.length && name[index] != '/') {
index++;
}
if (name[start] == '.') {
int len = index - start;
if (len == 1 || (name[start + 1] == '.' && len == 2)) {
return true;
}
}
index++;
}
return false;
}

protected String nameAsString() {
return new String(name);
}

@Override
public boolean equals(Object other) {
if (!(other instanceof IndexNode)) {
Expand All @@ -2678,7 +2671,7 @@ public int hashCode() {

@Override
public String toString() {
return new String(name) + (isdir ? " (dir)" : " ") + ", index: " + pos;
return nameAsString() + (isdir ? " (dir)" : " ") + ", index: " + pos;
}
}

Expand Down Expand Up @@ -3214,7 +3207,7 @@ private void readLocEXTT(ZipFileSystem zipfs) throws IOException {
public String toString() {
StringBuilder sb = new StringBuilder(1024);
Formatter fm = new Formatter(sb);
fm.format(" name : %s%n", new String(name));
fm.format(" name : %s%n", nameAsString());
fm.format(" creationTime : %tc%n", creationTime().toMillis());
fm.format(" lastAccessTime : %tc%n", lastAccessTime().toMillis());
fm.format(" lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
Expand Down

7 comments on commit 80cf9f3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk18u

@openjdk
Copy link

@openjdk openjdk bot commented on 80cf9f3 May 28, 2022

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-80cf9f34 in my personal fork of openjdk/jdk18u. To create a pull request with this backport targeting openjdk/jdk18u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 80cf9f34 from the openjdk/jdk repository.

The commit being backported was authored by Christoph Langer on 14 May 2022 and was reviewed by Lance Andersen.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk18u:

$ git fetch https://github.com/openjdk-bots/jdk18u RealCLanger-backport-80cf9f34:RealCLanger-backport-80cf9f34
$ git checkout RealCLanger-backport-80cf9f34
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk18u RealCLanger-backport-80cf9f34

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 80cf9f3 May 28, 2022

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-80cf9f34 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 80cf9f34 from the openjdk/jdk repository.

The commit being backported was authored by Christoph Langer on 14 May 2022 and was reviewed by Lance Andersen.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev RealCLanger-backport-80cf9f34:RealCLanger-backport-80cf9f34
$ git checkout RealCLanger-backport-80cf9f34
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev RealCLanger-backport-80cf9f34

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 80cf9f3 May 28, 2022

Choose a reason for hiding this comment

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

@RealCLanger Could not automatically backport 80cf9f34 to openjdk/jdk11u-dev due to conflicts in the following files:

  • src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

To manually resolve these conflicts run the following commands in your personal fork of openjdk/jdk11u-dev:

$ git checkout -b RealCLanger-backport-80cf9f34
$ git fetch --no-tags https://git.openjdk.java.net/jdk 80cf9f3464c599fb7860432bf4ed506a3b298d8e
$ git cherry-pick --no-commit 80cf9f3464c599fb7860432bf4ed506a3b298d8e
$ # Resolve conflicts
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 80cf9f3464c599fb7860432bf4ed506a3b298d8e'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk11u-dev with the title Backport 80cf9f3464c599fb7860432bf4ed506a3b298d8e.

Please sign in to comment.