You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2021/09/27 20:54:28 UTC

[tomcat] 02/02: Fix BZ 65586 - Correct bloom filter lookups for directories with final /

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 4d719497e5de8226efc55debcb0e24a176fd9bfc
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Sep 27 21:38:49 2021 +0100

    Fix BZ 65586 - Correct bloom filter lookups for directories with final /
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=65586
---
 .../apache/catalina/webresources/JarContents.java  | 17 +++++++-
 .../TestAbstractArchiveResourceSet.java            | 49 ++++++++++++++++++++++
 .../webresources/TesterWebResourceRoot.java        |  3 +-
 test/org/apache/tomcat/unittest/TesterContext.java |  9 +++-
 webapps/docs/changelog.xml                         |  6 +++
 5 files changed, 79 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/webresources/JarContents.java b/java/org/apache/catalina/webresources/JarContents.java
index 6346d52..71cc50f 100644
--- a/java/org/apache/catalina/webresources/JarContents.java
+++ b/java/org/apache/catalina/webresources/JarContents.java
@@ -77,6 +77,16 @@ public final class JarContents {
 
             bits1.set(pathHash1 % TABLE_SIZE);
             bits2.set(pathHash2 % TABLE_SIZE);
+
+            // While directory entry names always end in "/", application code
+            // may look them up without the trailing "/". Add this second form.
+            if (entry.isDirectory()) {
+                pathHash1 = hashcode(name, startPos, name.length() - 1, HASH_PRIME_1);
+                pathHash2 = hashcode(name, startPos, name.length() - 1, HASH_PRIME_2);
+
+                bits1.set(pathHash1 % TABLE_SIZE);
+                bits2.set(pathHash2 % TABLE_SIZE);
+            }
         }
     }
 
@@ -91,9 +101,12 @@ public final class JarContents {
      * @return hashcode of the range.
      */
     private int hashcode(String content, int startPos, int hashPrime) {
+        return hashcode(content, startPos, content.length(), hashPrime);
+    }
+
+    private int hashcode(String content, int startPos, int endPos, int hashPrime) {
         int h = hashPrime/2;
-        int contentLength = content.length();
-        for (int i = startPos; i < contentLength; i++) {
+        for (int i = startPos; i < endPos; i++) {
             h = hashPrime * h + content.charAt(i);
         }
 
diff --git a/test/org/apache/catalina/webresources/TestAbstractArchiveResourceSet.java b/test/org/apache/catalina/webresources/TestAbstractArchiveResourceSet.java
new file mode 100644
index 0000000..7c5b2f2
--- /dev/null
+++ b/test/org/apache/catalina/webresources/TestAbstractArchiveResourceSet.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.File;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.WebResource;
+import org.apache.catalina.WebResourceRoot;
+
+public class TestAbstractArchiveResourceSet {
+
+    /*
+     * https://bz.apache.org/bugzilla/show_bug.cgi?id=65586
+     */
+    @Test
+    public void testBloomFilterWithDirectory() {
+        WebResourceRoot root = new TesterWebResourceRoot();
+
+        root.getContext().setUseBloomFilterForArchives(true);
+
+        File file = new File("webapps/examples/WEB-INF/lib/taglibs-standard-impl-1.2.5-migrated-0.0.1.jar");
+
+        JarResourceSet jarResourceSet = new JarResourceSet(root, "/WEB-INF/classes", file.getAbsolutePath(), "/");
+        jarResourceSet.getArchiveEntries(false);
+
+        WebResource r1 = jarResourceSet.getResource("/WEB-INF/classes/org/");
+        Assert.assertTrue(r1.isDirectory());
+
+        WebResource r2 = jarResourceSet.getResource("/WEB-INF/classes/org");
+        Assert.assertTrue(r2.isDirectory());
+    }
+}
diff --git a/test/org/apache/catalina/webresources/TesterWebResourceRoot.java b/test/org/apache/catalina/webresources/TesterWebResourceRoot.java
index 6217f26..a56186e 100644
--- a/test/org/apache/catalina/webresources/TesterWebResourceRoot.java
+++ b/test/org/apache/catalina/webresources/TesterWebResourceRoot.java
@@ -80,9 +80,10 @@ public class TesterWebResourceRoot extends StandardRoot {
         return null;
     }
 
+    Context context = new TesterContext();
     @Override
     public Context getContext() {
-        return new TesterContext();
+        return context;
     }
 
     @Override
diff --git a/test/org/apache/tomcat/unittest/TesterContext.java b/test/org/apache/tomcat/unittest/TesterContext.java
index 7164bcc..b0c624b 100644
--- a/test/org/apache/tomcat/unittest/TesterContext.java
+++ b/test/org/apache/tomcat/unittest/TesterContext.java
@@ -1306,10 +1306,15 @@ public class TesterContext implements Context {
     @Override
     public void setParallelAnnotationScanning(boolean parallelAnnotationScanning) {}
 
+    boolean useBloomFilterForArchives = false;
     @Override
-    public boolean getUseBloomFilterForArchives() { return false; }
+    public boolean getUseBloomFilterForArchives() {
+        return useBloomFilterForArchives;
+    }
 
     @Override
-    public void setUseBloomFilterForArchives(boolean useBloomFilterForArchives) {}
+    public void setUseBloomFilterForArchives(boolean useBloomFilterForArchives) {
+        this.useBloomFilterForArchives = useBloomFilterForArchives;
+    }
 
 }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 8af1114..d09fd6f 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -127,6 +127,12 @@
         Implement the new connection ID and request ID API for Servlet 6.0.
         (markt)
       </add>
+      <fix>
+        <bug>65586</bug>: Fix the bloom filter used to improve performance of
+        archive file look ups in the web resources implementation so it works
+        correctly for directory lookups whether or not the provided directory
+        name includes the trailing <code>/</code>. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org