This is an automated email from the ASF dual-hosted git repository.
remm pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new f182b68b35 Avoid caching bogus 0 content length
f182b68b35 is described below
commit f182b68b359134805bad89ec58f9bf86979124f1
Author: remm <[email protected]>
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 40fb323224..ff2be74844 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 55ff6d1a91..829cf62af0 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -123,6 +123,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: [email protected]
For additional commands, e-mail: [email protected]