This is an automated email from the ASF dual-hosted git repository.
markt-asf 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 ea4622a182 Fully fix null handling in MessageBytes.equals(MessageBytes)
ea4622a182 is described below
commit ea4622a1827f66c99fa57cb021388d352c5f1966
Author: Mark Thomas <[email protected]>
AuthorDate: Thu May 21 09:42:13 2026 +0100
Fully fix null handling in MessageBytes.equals(MessageBytes)
---
java/org/apache/tomcat/util/buf/MessageBytes.java | 24 ++++++++++++++--------
.../apache/tomcat/util/buf/TestMessageBytes.java | 10 +++++++++
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/java/org/apache/tomcat/util/buf/MessageBytes.java
b/java/org/apache/tomcat/util/buf/MessageBytes.java
index 5bc5be6b7a..9e59dc397a 100644
--- a/java/org/apache/tomcat/util/buf/MessageBytes.java
+++ b/java/org/apache/tomcat/util/buf/MessageBytes.java
@@ -432,19 +432,25 @@ public final class MessageBytes implements Cloneable,
Serializable {
* @return true if equal
*/
public boolean equals(MessageBytes mb) {
+ // MessageBytes can be one of four types so there are 4 * 4 = 16
possible combinations.
+
+ // If either instance is a String, use equals(String)
if (type == T_STR) {
return mb.equals(strValue);
}
-
- if (mb.type != T_CHARS && mb.type != T_BYTES) {
- // it's a string or int/date string value
- return equals(mb.toString());
+ if (mb.type == T_STR) {
+ return equals(mb.strValue);
}
- // mb is either CHARS or BYTES.
- // this is either CHARS or BYTES
- // Deal with the 4 cases ( in fact 3, one is symmetric)
+ // If either instance is null, use isNull
+ if (type == T_NULL) {
+ return mb.isNull();
+ }
+ if (mb.type == T_NULL) {
+ return isNull();
+ }
+ // At this point both instances are either T_BYTES or T_CHARS
if (mb.type == T_CHARS && type == T_CHARS) {
return charC.equals(mb.charC);
}
@@ -457,7 +463,9 @@ public final class MessageBytes implements Cloneable,
Serializable {
if (mb.type == T_BYTES && type == T_CHARS) {
return mb.byteC.equals(charC);
}
- return (mb.type == T_NULL && type == T_NULL);
+
+ // Impossible to reach this point
+ return false;
}
diff --git a/test/org/apache/tomcat/util/buf/TestMessageBytes.java
b/test/org/apache/tomcat/util/buf/TestMessageBytes.java
index f9af7fd19d..a1063a599c 100644
--- a/test/org/apache/tomcat/util/buf/TestMessageBytes.java
+++ b/test/org/apache/tomcat/util/buf/TestMessageBytes.java
@@ -16,6 +16,7 @@
*/
package org.apache.tomcat.util.buf;
+import org.junit.Assert;
import org.junit.Test;
public class TestMessageBytes {
@@ -66,4 +67,13 @@ public class TestMessageBytes {
mb.recycle();
mb.toChars();
}
+
+
+ @Test
+ public void testEqualsNullNull() {
+ MessageBytes mb1 = MessageBytes.newInstance();
+ MessageBytes mb2 = MessageBytes.newInstance();
+
+ Assert.assertTrue(mb1.equals(mb2));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]