Skip to content

Commit

Permalink
CODEC-313: Fix possible ArrayIndexOutOfBoundsException thrown by Quot…
Browse files Browse the repository at this point in the history
…edPrintableCodec.encodeQuotedPrintable() method (#221)

* CODEC-313: Fix possible ArrayIndexOutOfBoundsException

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

* CODEC-313: Add unit test

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

---------

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
  • Loading branch information
arthurscchan committed Nov 25, 2023
1 parent a56fcfe commit c65de5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin

private static final byte LF = 10;

/**
* Minimum length required for the byte arrays used by encodeQuotedPrintable method
*/
private static final int MIN_BYTES = 3;

/**
* Safe line length for quoted printable encoded text.
*/
Expand Down Expand Up @@ -208,6 +213,10 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[]
final int bytesLength = bytes.length;

if (strict) {
if (bytesLength < MIN_BYTES) {
return null;
}

int pos = 1;
// encode up to buffer.length - 3, the last three octets will be treated
// separately for simplification of note #3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ public void testSoftLineBreakEncode() throws Exception {
assertEquals(qpdata, qpcodec.encode(decoded));
}

@Test
public void testTooShortByteArray() throws Exception{
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true);
assertNull(qpcodec.encode("AA"), "Result should be null.");
}

@Test
public void testTrailingSpecial() throws Exception {
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true);
Expand Down

0 comments on commit c65de5c

Please sign in to comment.