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

markt-asf 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 c10ead6e01 Fix an edge case in MessageBytes.setLong() for 
Long.MIN_VALUE
c10ead6e01 is described below

commit c10ead6e016b55f66cec2d63c43103f9f70b3b6e
Author: Mark Thomas <[email protected]>
AuthorDate: Thu May 21 10:28:34 2026 +0100

    Fix an edge case in MessageBytes.setLong() for Long.MIN_VALUE
---
 java/org/apache/tomcat/util/buf/MessageBytes.java     | 11 ++++-------
 test/org/apache/tomcat/util/buf/TestMessageBytes.java | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/tomcat/util/buf/MessageBytes.java 
b/java/org/apache/tomcat/util/buf/MessageBytes.java
index d03774a062..31dd69a90e 100644
--- a/java/org/apache/tomcat/util/buf/MessageBytes.java
+++ b/java/org/apache/tomcat/util/buf/MessageBytes.java
@@ -649,17 +649,14 @@ public final class MessageBytes implements Cloneable, 
Serializable {
             buf[end++] = (byte) '0';
         }
         if (l < 0) {
-            if (l == Long.MIN_VALUE) {
-                current = Long.MAX_VALUE;
-            } else {
-                current = -l;
-            }
             buf[end++] = (byte) '-';
+        } else {
+            current = -l;
         }
-        while (current > 0) {
+        while (current < 0) {
             int digit = (int) (current % 10);
             current = current / 10;
-            buf[end++] = HexUtils.getHex(digit);
+            buf[end++] = HexUtils.getHex(-digit);
         }
         byteC.setStart(0);
         byteC.setEnd(end);
diff --git a/test/org/apache/tomcat/util/buf/TestMessageBytes.java 
b/test/org/apache/tomcat/util/buf/TestMessageBytes.java
index a1063a599c..3bfac645a5 100644
--- a/test/org/apache/tomcat/util/buf/TestMessageBytes.java
+++ b/test/org/apache/tomcat/util/buf/TestMessageBytes.java
@@ -76,4 +76,23 @@ public class TestMessageBytes {
 
         Assert.assertTrue(mb1.equals(mb2));
     }
+
+
+    @Test
+    public void testSetLong() {
+        doTestSetLong(0);
+        doTestSetLong(-1);
+        doTestSetLong(1);
+        doTestSetLong(-12345);
+        doTestSetLong(12345);
+        doTestSetLong(Long.MIN_VALUE);
+        doTestSetLong(Long.MAX_VALUE);
+    }
+
+
+    private void doTestSetLong(long l) {
+        MessageBytes mb = MessageBytes.newInstance();
+        mb.setLong(l);
+        Assert.assertEquals(mb.toStringType(), Long.toString(l));
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to