This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 1b087286 QuotedPrintableCodec strict mode returns null for inputs
shorter than 3 bytes (#455)
1b087286 is described below
commit 1b087286f8b755a8c9776670d424f693b2e21e4e
Author: Atharva Dudhe <[email protected]>
AuthorDate: Sat Nov 14 17:49:20 2026 +0530
QuotedPrintableCodec strict mode returns null for inputs shorter than 3
bytes (#455)
Strict encoding returned null for any non-null input shorter than 3
bytes because of the early return added for CODEC-313. Encode such
input byte by byte instead, applying rules #1, #2 and #3 (trailing
whitespace). Input of 3 or more bytes and null input are unchanged.
Replace testTooShortByteArray, which asserted the null result, with
testShortInputStrictEncoding.
Used an AI tool for assistance.
---
.../commons/codec/net/QuotedPrintableCodec.java | 9 +++++--
.../codec/net/QuotedPrintableCodecTest.java | 31 +++++++++++++++++++---
2 files changed, 34 insertions(+), 6 deletions(-)
diff --git
a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
index a44153a3..f0eb2d7f 100644
--- a/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
+++ b/src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
@@ -78,7 +78,7 @@ public class QuotedPrintableCodec implements BinaryEncoder,
BinaryDecoder, Strin
private static final byte LF = 10;
/**
- * Minimum length required for the byte arrays used by
encodeQuotedPrintable method.
+ * Minimum input length for the strict encoding path that treats the last
three octets separately. Shorter input is encoded byte by byte.
*/
private static final int MIN_BYTES = 3;
@@ -208,7 +208,12 @@ public class QuotedPrintableCodec implements
BinaryEncoder, BinaryDecoder, Strin
final int bytesLength = bytes.length;
if (strict) {
if (bytesLength < MIN_BYTES) {
- return null;
+ // too short for a soft line break, only rules #1, #2 and #3
(trailing whitespace) apply
+ for (int i = 0; i < bytesLength; i++) {
+ final int b = getUnsignedOctet(i, bytes);
+ encodeByte(b, !printable.get(b) || i == bytesLength - 1 &&
isWhitespace(b), buffer);
+ }
+ return buffer.toByteArray();
}
int pos = 1;
// encode up to buffer.length - 3, the last three octets will be
treated
diff --git
a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
index 4fa113fc..599aa895 100644
--- a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
@@ -58,6 +58,21 @@ class QuotedPrintableCodecTest {
// @formatter:on
}
+ private static Stream<Arguments> shortInputCases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("", "", ""),
+ Arguments.of("a", "a", "a"),
+ Arguments.of("ab", "ab", "ab"),
+ Arguments.of(" ", "=20", " "),
+ Arguments.of("a ", "a=20", "a "),
+ Arguments.of(" a", " a", " a"),
+ Arguments.of("\t", "=09", "\t"),
+ Arguments.of("=", "=3D", "=3D"),
+ Arguments.of("\u00e9", "=C3=A9", "=C3=A9"));
+ // @formatter:on
+ }
+
private String constructString(final int[] unicodeChars) {
final StringBuilder buffer = new StringBuilder();
if (unicodeChars != null) {
@@ -270,10 +285,18 @@ class QuotedPrintableCodecTest {
assertEquals(qpdata, qpcodec.encode(decoded));
}
- @Test
- void testTooShortByteArray() throws Exception {
- final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true);
- assertNull(qpcodec.encode("AA"), "Result should be null.");
+ @ParameterizedTest
+ @MethodSource("shortInputCases")
+ void testShortInputStrictEncoding(final String plain, final String
strictExpected, final String lenientExpected) throws Exception {
+ final QuotedPrintableCodec strictCodec = new
QuotedPrintableCodec(true);
+ final QuotedPrintableCodec lenientCodec = new
QuotedPrintableCodec(false);
+ final byte[] plainBytes = plain.getBytes(StandardCharsets.UTF_8);
+ final byte[] strictBytes =
strictExpected.getBytes(StandardCharsets.US_ASCII);
+ assertEquals(strictExpected, strictCodec.encode(plain));
+ assertEquals(plain, strictCodec.decode(strictCodec.encode(plain)));
+ assertArrayEquals(strictBytes, strictCodec.encode(plainBytes));
+ assertArrayEquals(strictBytes,
QuotedPrintableCodec.encodeQuotedPrintable(null, plainBytes, true));
+ assertEquals(lenientExpected, lenientCodec.encode(plain));
}
@Test