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

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


The following commit(s) were added to refs/heads/main by this push:
     new 80ae858419 Avoid caching bogus 0 content length
80ae858419 is described below

commit 80ae858419d816eb82a2377c7db0e34aef52c8c1
Author: remm <r...@apache.org>
AuthorDate: Thu Jan 9 16:29:24 2025 +0100

    Avoid caching bogus 0 content length
    
    When using getResources, the actual webResource is not retrieved. If
    calling getContentLength, it would cache 0 as the content length.
    Also initialize the cached resource a bit more to avoid a very likely
    concurrent eviction if somehow the cache is full.
    Possible fix for 69527.
---
 java/org/apache/catalina/webresources/CachedResource.java | 10 +++++-----
 webapps/docs/changelog.xml                                |  4 ++++
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/java/org/apache/catalina/webresources/CachedResource.java 
b/java/org/apache/catalina/webresources/CachedResource.java
index 31c13ba7a1..834588e547 100644
--- a/java/org/apache/catalina/webresources/CachedResource.java
+++ b/java/org/apache/catalina/webresources/CachedResource.java
@@ -85,6 +85,7 @@ public class CachedResource implements WebResource {
         this.root = root;
         this.webAppPath = path;
         this.ttl = ttl;
+        nextCheck = ttl + System.currentTimeMillis();
         this.objectMaxSizeBytes = objectMaxSizeBytes;
         this.usesClassLoaderResources = usesClassLoaderResources;
     }
@@ -247,6 +248,9 @@ public class CachedResource implements WebResource {
 
     @Override
     public long getContentLength() {
+        if (webResource == null) {
+            return Long.valueOf(0);
+        }
         /*
          * Cache the content length for two reasons.
          *
@@ -263,11 +267,7 @@ public class CachedResource implements WebResource {
         if (cachedContentLength == null) {
             synchronized (cachedContentLengthLock) {
                 if (cachedContentLength == null) {
-                    if (webResource == null) {
-                        cachedContentLength = Long.valueOf(0);
-                    } else {
-                        cachedContentLength = 
Long.valueOf(webResource.getContentLength());
-                    }
+                    cachedContentLength = 
Long.valueOf(webResource.getContentLength());
                 }
             }
         }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index ab16f2677b..c9276112e4 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -150,6 +150,10 @@
         attribute value will also be used by the default and WebDAV Servlets.
         (remm)
       </update>
+      <fix>
+        <bug>69527</bug>: Avoid rare cases where a cached resource could be set
+        with 0 content length, or could be evicted immediately. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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

Reply via email to