atharv1205 opened a new pull request, #455:
URL: https://github.com/apache/commons-codec/pull/455

   ## Problem
   
   In strict mode, `QuotedPrintableCodec` returns `null` for any non-null input 
shorter than 3 bytes, while non-strict mode encodes the same input correctly:
   
   ```java
   new QuotedPrintableCodec(true).encode("");    // null, expected ""
   new QuotedPrintableCodec(true).encode("ab");  // null, expected "ab"
   new QuotedPrintableCodec(true).encode(" ");   // null, expected "=20"
   new QuotedPrintableCodec(true).encode("a ");  // null, expected "a=20"
   new QuotedPrintableCodec(false).encode("ab"); // "ab"
   new QuotedPrintableCodec(true).encode("abc"); // "abc"
   ```
   
   The same happens through `encode(byte[])` and the static 
`encodeQuotedPrintable(BitSet, byte[], true)`. Callers that pass short values 
(for example single characters or empty header values) get `null` back and fail 
with a `NullPointerException` later.
   
   ## Root cause
   
   CODEC-313 (#221) fixed an `ArrayIndexOutOfBoundsException` found by 
OSS-Fuzz. The strict path treats the last three octets separately, so the index 
`bytesLength - 3` was negative for short input. The fix added an early `if 
(bytesLength < MIN_BYTES) return null;` in `encodeQuotedPrintable(BitSet, 
byte[], boolean)`. That removed the crash but replaced it with a `null` result 
for valid input.
   
   ## Why `testTooShortByteArray` was changed
   
   `testTooShortByteArray` was added by CODEC-313 and asserted `encode("AA")` 
is `null`. It recorded the workaround, not intended behavior:
   
   - The Javadoc of the encode methods never documents a `null` result for 
non-null input. `null` is only returned for `null` input.
   - Non-strict mode encodes these inputs fine, and nothing in RFC 1521 makes 
short input invalid.
   - Neither CODEC-313 nor #221 discussed what the output for short input 
should be.
   
   The test is replaced by `testShortInputStrictEncoding`, which asserts the 
correct encoding.
   
   ## Fix
   
   For strict input shorter than 3 bytes, each byte is encoded with 
`encodeByte(b, !printable.get(b) || (isLastByte && isWhitespace(b)), buffer)`, 
which applies rule #1, rule #2 and rule #3 (trailing whitespace is encoded). 
Such input can never reach the line length limit, so no soft line breaks are 
needed. Behavior for input of 3 or more bytes and for `null` input is unchanged.
   
   ## Tests
   
   `testShortInputStrictEncoding` (parameterized) checks, for `""`, `"a"`, 
`"ab"`, `" "`, `"a "`, `" a"`, `"\t"`, `"="` and `"é"` (UTF-8):
   
   - the strict `encode(String)` result (`""`, `"a"`, `"ab"`, `"=20"`, 
`"a=20"`, `" a"`, `"=09"`, `"=3D"`, `"=C3=A9"`),
   - that decoding gives back the original,
   - that `encode(byte[])` and `encodeQuotedPrintable(BitSet, byte[], true)` 
produce the same bytes,
   - that non-strict results are unchanged.
   
   All 9 cases fail without the change (result `null`) and pass with it. The 
default `mvn` build passes.
   
   ---
   
   Before you push a pull request, review this list:
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [x] Read the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use 
Artificial Intelligence (AI).
   - [x] I used AI to create any part of, or all of, this pull request. Which 
AI tool was used to create this pull request, and to what extent did it 
contribute? Claude Code assisted with investigation, tests and the fix; I 
reviewed and verified every line.
   - [x] Run a successful build using the default 
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command 
line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail 
if the changes to the runtime are not applied. This may not always be possible, 
but it is a best practice.
   - [x] Write a pull request description that is detailed enough to understand 
what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line 
and body. Note that a maintainer may squash commits during the merge process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to