Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODEC-313: Fix possible ArrayIndexOutOfBoundsException thrown by QuotedPrintableCodec.encodeQuotedPrintable() method #221

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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