Skip to content

Commit 87676a6

Browse files
committed
DataMatrix: check array bounds
DEVSIX-1845
1 parent 963e936 commit 87676a6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

barcodes/src/main/java/com/itextpdf/barcodes/BarcodeDataMatrix.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ public int setCode(String text) {
339339
* <CODE>DM_ERROR_EXTENSION</CODE> - an error was while parsing an extension.
340340
*/
341341
public int setCode(byte[] text, int textOffset, int textSize) {
342+
if (textOffset < 0) {
343+
throw new IndexOutOfBoundsException("" + textOffset);
344+
}
345+
if (textOffset + textSize > text.length) {
346+
throw new IndexOutOfBoundsException("" + textSize);
347+
}
342348
int extCount, e, k, full;
343349
DmParams dm, last;
344350
byte[] data = new byte[2500];

barcodes/src/test/java/com/itextpdf/barcodes/BarcodeDataMatrixTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,35 @@ public void barcode10Test() {
221221
int result = barcodeDataMatrix.setCode("AbcdFFghijklmnopqrstuWXSQ");
222222
Assert.assertEquals(BarcodeDataMatrix.DM_ERROR_TEXT_TOO_BIG, result);
223223
}
224+
225+
@Test
226+
public void barcode11Test() {
227+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
228+
barcodeDataMatrix.setWidth(18);
229+
barcodeDataMatrix.setHeight(18);
230+
byte[] str = "AbcdFFghijklmnop".getBytes();
231+
int result = barcodeDataMatrix.setCode(str, 0, str.length);
232+
Assert.assertEquals(BarcodeDataMatrix.DM_NO_ERROR, result);
233+
}
234+
235+
236+
@Test
237+
public void barcode12Test() {
238+
junitExpectedException.expect(IndexOutOfBoundsException.class);;
239+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
240+
barcodeDataMatrix.setWidth(18);
241+
barcodeDataMatrix.setHeight(18);
242+
byte[] str = "AbcdFFghijklmnop".getBytes();
243+
barcodeDataMatrix.setCode(str, -1, str.length);
244+
}
245+
246+
@Test
247+
public void barcode13Test() {
248+
junitExpectedException.expect(IndexOutOfBoundsException.class);;
249+
BarcodeDataMatrix barcodeDataMatrix = new BarcodeDataMatrix();
250+
barcodeDataMatrix.setWidth(18);
251+
barcodeDataMatrix.setHeight(18);
252+
byte[] str = "AbcdFFghijklmnop".getBytes();
253+
barcodeDataMatrix.setCode(str, 0, str.length + 1);
254+
}
224255
}

0 commit comments

Comments
 (0)