From be90711b32cb79822222d31f258ff4e0f45a616c Mon Sep 17 00:00:00 2001
Date: Thu, 21 Sep 2023 15:24:07 +0800
Subject: add 8242842-Avoid-reallocating-name-when-checking-for-tr

---
 .../share/classes/java/util/zip/ZipFile.java  | 25 ++++++++++++-------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/jdk/src/share/classes/java/util/zip/ZipFile.java b/jdk/src/share/classes/java/util/zip/ZipFile.java
index 9e854e84d..b6a6c2a48 100644
--- a/jdk/src/share/classes/java/util/zip/ZipFile.java
+++ b/jdk/src/share/classes/java/util/zip/ZipFile.java
@@ -1049,7 +1049,7 @@ class ZipFile implements ZipConstants, Closeable {
             return h;
         }
 
-        private static final int hash_append(int hash, byte b) {
+        private static final int hashAppend(int hash, byte b) {
             return hash * 31 + b;
         }
 
@@ -1267,11 +1267,13 @@ class ZipFile implements ZipConstants, Closeable {
             }
             int hsh = hashN(name, 0, name.length);
             int idx = table[(hsh & 0x7fffffff) % tablelen];
+            boolean appendSlash = false;
             /*
              * This while loop is an optimization where a double lookup
-             * for name and name+/ is being performed. The name char
-             * array has enough room at the end to try again with a
-             * slash appended if the first table lookup does not succeed.
+             * for name and name+/ is being performed. The name byte
+             * array will be updated with an added slash only if the first
+             * table lookup fails and there is a matching hash value for
+             * name+/.
              */
             while(true) {
                 /*
@@ -1282,6 +1284,11 @@ class ZipFile implements ZipConstants, Closeable {
                     if (getEntryHash(idx) == hsh) {
                         // The CEN name must match the specfied one
                         int pos = getEntryPos(idx);
+                        if (appendSlash) {
+                            name = Arrays.copyOf(name, name.length + 1);
+                            name[name.length - 1] = '/';
+                            appendSlash = false;
+                        }
                         if (name.length == CENNAM(cen, pos)) {
                             boolean matched = true;
                             int nameoff = pos + CENHDR;
@@ -1302,11 +1309,11 @@ class ZipFile implements ZipConstants, Closeable {
                 if (!addSlash  || name[name.length - 1] == '/') {
                      return -1;
                 }
-                /* Add slash and try once more */
-                name = Arrays.copyOf(name, name.length + 1);
-                name[name.length - 1] = '/';
-                hsh = hash_append(hsh, (byte)'/');
-                //idx = table[hsh % tablelen];
+                // Add a slash to the hash code
+                hsh = hashAppend(hsh, (byte)'/');
+                // If we find a match on the new hash code, we need to append a
+                // slash when comparing
+                appendSlash = true;
                 idx = table[(hsh & 0x7fffffff) % tablelen];
                 addSlash = false;
             }
-- 
2.22.0

