Skip to content

Commit c65de5c

Browse files
authored
CODEC-313: Fix possible ArrayIndexOutOfBoundsException thrown by QuotedPrintableCodec.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>
1 parent a56fcfe commit c65de5c

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin
8484

8585
private static final byte LF = 10;
8686

87+
/**
88+
* Minimum length required for the byte arrays used by encodeQuotedPrintable method
89+
*/
90+
private static final int MIN_BYTES = 3;
91+
8792
/**
8893
* Safe line length for quoted printable encoded text.
8994
*/
@@ -208,6 +213,10 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, final byte[]
208213
final int bytesLength = bytes.length;
209214

210215
if (strict) {
216+
if (bytesLength < MIN_BYTES) {
217+
return null;
218+
}
219+
211220
int pos = 1;
212221
// encode up to buffer.length - 3, the last three octets will be treated
213222
// separately for simplification of note #3

src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public void testSoftLineBreakEncode() throws Exception {
227227
assertEquals(qpdata, qpcodec.encode(decoded));
228228
}
229229

230+
@Test
231+
public void testTooShortByteArray() throws Exception{
232+
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true);
233+
assertNull(qpcodec.encode("AA"), "Result should be null.");
234+
}
235+
230236
@Test
231237
public void testTrailingSpecial() throws Exception {
232238
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(true);

0 commit comments

Comments
 (0)