This is an automated email from the ASF dual-hosted git repository.
rmaucher 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 aca39aba07 Fixes from code review
aca39aba07 is described below
commit aca39aba076a854c0b4160f5e7145d28c7fed3da
Author: remm <[email protected]>
AuthorDate: Mon Jun 1 22:06:21 2026 +0200
Fixes from code review
Handle EOF in B2C and C2B.
Avoid using subtractB and deprecate it.
---
java/org/apache/tomcat/util/buf/B2CConverter.java | 19 +++++++++++++------
java/org/apache/tomcat/util/buf/ByteChunk.java | 15 +++++++++++++--
java/org/apache/tomcat/util/buf/C2BConverter.java | 20 ++++++++++++++++----
java/org/apache/tomcat/util/buf/CharChunk.java | 12 ++++++++----
java/org/apache/tomcat/util/buf/MessageBytes.java | 2 ++
5 files changed, 52 insertions(+), 16 deletions(-)
diff --git a/java/org/apache/tomcat/util/buf/B2CConverter.java
b/java/org/apache/tomcat/util/buf/B2CConverter.java
index 91a1467974..e40f020606 100644
--- a/java/org/apache/tomcat/util/buf/B2CConverter.java
+++ b/java/org/apache/tomcat/util/buf/B2CConverter.java
@@ -153,7 +153,13 @@ public class B2CConverter {
int pos = cb.position();
// Loop until one char is decoded or there is a decoder error
do {
- leftovers.put(bc.subtractB());
+ int b = bc.subtract();
+ if (b < 0) {
+ leftovers.flip();
+ result = decoder.decode(leftovers, cb, endOfInput);
+ break;
+ }
+ leftovers.put((byte) b);
leftovers.flip();
result = decoder.decode(leftovers, cb, endOfInput);
leftovers.position(leftovers.limit());
@@ -222,14 +228,15 @@ public class B2CConverter {
int pos = cb.position();
// Loop until one char is decoded or there is a decoder error
do {
- byte chr;
if (bc.remaining() == 0) {
int n = ic.realReadBytes();
- chr = n < 0 ? -1 : bc.get();
- } else {
- chr = bc.get();
+ if (n < 0) {
+ leftovers.flip();
+ result = decoder.decode(leftovers, cb, endOfInput);
+ break;
+ }
}
- leftovers.put(chr);
+ leftovers.put(bc.get());
leftovers.flip();
result = decoder.decode(leftovers, cb, endOfInput);
leftovers.position(leftovers.limit());
diff --git a/java/org/apache/tomcat/util/buf/ByteChunk.java
b/java/org/apache/tomcat/util/buf/ByteChunk.java
index 0a6fb23ef1..3897a1755e 100644
--- a/java/org/apache/tomcat/util/buf/ByteChunk.java
+++ b/java/org/apache/tomcat/util/buf/ByteChunk.java
@@ -162,8 +162,14 @@ public final class ByteChunk extends AbstractChunk {
@Override
- public Object clone() throws CloneNotSupportedException {
- return super.clone();
+ public ByteChunk clone() throws CloneNotSupportedException {
+ ByteChunk result = (ByteChunk) super.clone();
+ if (buff != null) {
+ result.buff = buff.clone();
+ }
+ result.in = null;
+ result.out = null;
+ return result;
}
@@ -446,7 +452,12 @@ public final class ByteChunk extends AbstractChunk {
* @return the byte value or -1 if end of data
*
* @throws IOException if reading from input channel fails
+ *
+ * @deprecated Use {@link #subtract()} instead. This method cannot
distinguish
+ * between EOF (-1) and the legitimate byte value 0xFF because
+ * it returns {@code byte}.
*/
+ @Deprecated
public byte subtractB() throws IOException {
if (checkEof()) {
return -1;
diff --git a/java/org/apache/tomcat/util/buf/C2BConverter.java
b/java/org/apache/tomcat/util/buf/C2BConverter.java
index 33c6b49902..0143a27935 100644
--- a/java/org/apache/tomcat/util/buf/C2BConverter.java
+++ b/java/org/apache/tomcat/util/buf/C2BConverter.java
@@ -66,7 +66,7 @@ public final class C2BConverter {
encoder.reset();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
- log.warn(sm.getString("c2bConverter.decoderResetFail",
encoder.charset()), t);
+ log.warn(sm.getString("c2bConverter.encoderResetFail",
encoder.charset()), t);
}
leftovers.position(0);
}
@@ -85,6 +85,7 @@ public final class C2BConverter {
*
* @return true if there are leftovers
*/
+ @Deprecated
public boolean isUndeflow() {
return isUnderflow();
}
@@ -120,7 +121,13 @@ public final class C2BConverter {
int pos = bb.position();
// Loop until one char is encoded or there is an encoder error
do {
- leftovers.put((char) cc.subtract());
+ int c = cc.subtract();
+ if (c < 0) {
+ leftovers.flip();
+ result = encoder.encode(leftovers, bb, false);
+ break;
+ }
+ leftovers.put((char) c);
leftovers.flip();
result = encoder.encode(leftovers, bb, false);
leftovers.position(leftovers.limit());
@@ -132,7 +139,7 @@ public final class C2BConverter {
cb.position(cc.getStart());
leftovers.position(0);
}
- // Do the decoding and get the results into the byte chunk and the char
+ // Do the encoding and get the results into the byte chunk and the char
// chunk
result = encoder.encode(cb, bb, false);
if (result.isError() || result.isMalformed()) {
@@ -185,6 +192,11 @@ public final class C2BConverter {
int pos = bb.position();
// Loop until one char is encoded or there is an encoder error
do {
+ if (!cc.hasRemaining()) {
+ leftovers.flip();
+ result = encoder.encode(leftovers, bb, false);
+ break;
+ }
leftovers.put(cc.get());
leftovers.flip();
result = encoder.encode(leftovers, bb, false);
@@ -197,7 +209,7 @@ public final class C2BConverter {
cb.position(cc.position());
leftovers.position(0);
}
- // Do the decoding and get the results into the byte chunk and the char
+ // Do the encoding and get the results into the byte chunk and the char
// chunk
result = encoder.encode(cb, bb, false);
if (result.isError() || result.isMalformed()) {
diff --git a/java/org/apache/tomcat/util/buf/CharChunk.java
b/java/org/apache/tomcat/util/buf/CharChunk.java
index b7084a2fad..c7cbd24a95 100644
--- a/java/org/apache/tomcat/util/buf/CharChunk.java
+++ b/java/org/apache/tomcat/util/buf/CharChunk.java
@@ -90,11 +90,15 @@ public final class CharChunk extends AbstractChunk
implements CharSequence {
}
- // --------------------
-
@Override
- public Object clone() throws CloneNotSupportedException {
- return super.clone();
+ public CharChunk clone() throws CloneNotSupportedException {
+ CharChunk result = (CharChunk) super.clone();
+ if (buff != null) {
+ result.buff = buff.clone();
+ }
+ result.in = null;
+ result.out = null;
+ return result;
}
diff --git a/java/org/apache/tomcat/util/buf/MessageBytes.java
b/java/org/apache/tomcat/util/buf/MessageBytes.java
index 499e46390f..9dbb796e58 100644
--- a/java/org/apache/tomcat/util/buf/MessageBytes.java
+++ b/java/org/apache/tomcat/util/buf/MessageBytes.java
@@ -602,6 +602,8 @@ public final class MessageBytes implements Cloneable,
Serializable {
String sc = src.getString();
this.setString(sc);
break;
+ case T_NULL:
+ recycle();
}
setCharset(src.getCharset());
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]