Skip to content

Commit 0bd148e

Browse files
committed
Fix bug of incrementing counter in PdfTokenizer#decodeStringContent in case of ending line on octal number
DEVSIX-7476
1 parent 07551d7 commit 0bd148e

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

io/src/main/java/com/itextpdf/io/source/PdfTokenizer.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.io.source;
2424

25+
import com.itextpdf.commons.utils.MessageFormatUtil;
2526
import com.itextpdf.io.exceptions.IOException;
2627
import com.itextpdf.io.exceptions.IoExceptionMessageConstant;
2728
import com.itextpdf.io.logs.IoLogMessageConstant;
28-
import com.itextpdf.commons.utils.MessageFormatUtil;
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
3129

3230
import java.io.Closeable;
3331
import java.util.Arrays;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3434

3535
public class PdfTokenizer implements Closeable {
3636

@@ -525,7 +525,8 @@ protected static byte[] decodeStringContent(byte[] content, int from, int to, bo
525525

526526
// <6954657874ae...>
527527
if (hexWriting) {
528-
for (int i = from; i <= to; ) {
528+
int i = from;
529+
while (i <= to) {
529530
int v1 = ByteBuffer.getHex(content[i++]);
530531
if (i > to) {
531532
buffer.append(v1 << 4);
@@ -537,8 +538,8 @@ protected static byte[] decodeStringContent(byte[] content, int from, int to, bo
537538
}
538539
} else {
539540
// ((iText\( some version)...)
540-
541-
for (int i = from; i <= to; ) {
541+
int i = from;
542+
while (i <= to) {
542543
int ch = content[i++];
543544
if (ch == '\\') {
544545
boolean lineBreak = false;
@@ -577,19 +578,17 @@ protected static byte[] decodeStringContent(byte[] content, int from, int to, bo
577578
break;
578579
}
579580
int octal = ch - '0';
580-
ch = content[i++];
581-
if (ch < '0' || ch > '7') {
582-
i--;
581+
if (i > to) {
583582
ch = octal;
584583
break;
585584
}
586-
octal = (octal << 3) + ch - '0';
587585
ch = content[i++];
588-
if (ch < '0' || ch > '7') {
589-
i--;
586+
octal = (octal << 3) + ch - '0';
587+
if (ch < '0' || ch > '7' || i > to) {
590588
ch = octal;
591589
break;
592590
}
591+
ch = content[i++];
593592
octal = (octal << 3) + ch - '0';
594593
ch = octal & 0xff;
595594
break;

io/src/test/java/com/itextpdf/io/source/PdfTokenizerTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.io.source;
2424

25-
import com.itextpdf.io.source.PdfTokenizer.TokenType;
2625
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.io.source.PdfTokenizer.TokenType;
2727
import com.itextpdf.test.ExtendedITextTest;
2828
import com.itextpdf.test.annotations.type.UnitTest;
2929

@@ -439,6 +439,30 @@ public void primitivesTest() throws Exception {
439439
Assert.assertEquals("-116.23", new String(tok.getByteContent()));
440440
}
441441

442+
@Test
443+
public void octalNumberLong1Test() {
444+
// 49 equal to string "1", octal 1 equals to 1 in decimal
445+
byte[] bytes = new byte[] {92, 49};
446+
byte[] result = PdfTokenizer.decodeStringContent(bytes, false);
447+
Assert.assertArrayEquals(new byte[] {1}, result);
448+
}
449+
450+
@Test
451+
public void octalNumberLong2Test() {
452+
// 49 50 equal to string "12", octal 12 equals to 10 in decimal
453+
byte[] bytes = new byte[] {92, 49, 50};
454+
byte[] result = PdfTokenizer.decodeStringContent(bytes, false);
455+
Assert.assertArrayEquals(new byte[] {10}, result);
456+
}
457+
458+
@Test
459+
public void octalNumberLong3Test() {
460+
// 49 50 51 equal to string "123", octal 123 equals to 83 in decimal
461+
byte[] bytes = new byte[] {92, 49, 50, 51};
462+
byte[] result = PdfTokenizer.decodeStringContent(bytes, false);
463+
Assert.assertArrayEquals(new byte[] {83}, result);
464+
}
465+
442466
private void checkTokenTypes(String data, TokenType... expectedTypes) throws Exception {
443467
RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
444468
PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(factory

0 commit comments

Comments
 (0)