fix: устранение расхождений кода со спецификациями и синхронизация доков #109
@@ -263,7 +263,15 @@ final class CfbReader {
|
|||||||
return fat;
|
return fat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Цепочка секторов по FAT; цикл/выход за FAT → {@link ConversionException}. */
|
/**
|
||||||
|
* Цепочка секторов по FAT; цикл/выход за FAT → {@link ConversionException}.
|
||||||
|
* Капа min(fat.length, sectorTotal): sectorOffset=(sector+1)·sectorSize
|
||||||
|
* обязан остаться положительным int — сектор «за файлом» дал бы оффлоу
|
||||||
|
* и обход проверок base+n>cfb.length; капка fat.length — ещё и на
|
||||||
|
* индексацию fat[] при FAT, покрывающем меньше файла (битый DIFAT).
|
||||||
|
* Регрессия — CfbReaderTest#streamChainBeyondFile_rejected,
|
||||||
|
* #rootChainBeyondFile_rejected.
|
||||||
|
*/
|
||||||
private static int[] chain(int[] fat, long start, int sectorTotal, String what) {
|
private static int[] chain(int[] fat, long start, int sectorTotal, String what) {
|
||||||
List<Integer> sectors = new ArrayList<>();
|
List<Integer> sectors = new ArrayList<>();
|
||||||
long sect = start;
|
long sect = start;
|
||||||
@@ -272,10 +280,7 @@ final class CfbReader {
|
|||||||
if (sectors.size() > limit) {
|
if (sectors.size() > limit) {
|
||||||
throw new ConversionException("Циклическая " + what + " в .doc");
|
throw new ConversionException("Циклическая " + what + " в .doc");
|
||||||
}
|
}
|
||||||
if (sect >= fat.length || sect >= sectorTotal) {
|
if (sect >= limit - 1) {
|
||||||
// sectorTotal: sectorOffset=(sector+1)*sectorSize обязан
|
|
||||||
// остаться положительным int — сектор «за файлом» дал бы
|
|
||||||
// оффлоу и обход проверок base+n>cfb.length
|
|
||||||
throw new ConversionException("Сектор за концом FAT в .doc: " + what);
|
throw new ConversionException("Сектор за концом FAT в .doc: " + what);
|
||||||
}
|
}
|
||||||
sectors.add((int) sect);
|
sectors.add((int) sect);
|
||||||
@@ -356,7 +361,7 @@ final class CfbReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void requireInFile(int offset, int len, int fileLen, String what) {
|
private static void requireInFile(int offset, int len, int fileLen, String what) {
|
||||||
if (offset < HEADER_SIZE || offset + len > fileLen) {
|
if (offset < HEADER_SIZE || (long) offset + len > fileLen) {
|
||||||
throw new ConversionException("Ссылка за конец файла в .doc: " + what);
|
throw new ConversionException("Ссылка за конец файла в .doc: " + what);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -298,6 +298,15 @@ class CfbReaderTest {
|
|||||||
assertSame(ConversionException.class, ex.getClass());
|
assertSame(ConversionException.class, ex.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void difatSectorFarBeyondFile_rejected() {
|
||||||
|
byte[] file = build(new Spec("WordDocument", "abc".getBytes()));
|
||||||
|
put32(file, 0x4C, 0x400000); // (0x400000+1)·512 > Integer.MAX_VALUE
|
||||||
|
ConversionException ex = assertThrows(ConversionException.class,
|
||||||
|
() -> CfbReader.readStreams(file));
|
||||||
|
assertTrue(ex.getMessage().contains("за концом файла"), ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void difatTooManyFatSectors_rejected() {
|
void difatTooManyFatSectors_rejected() {
|
||||||
byte[] file = build();
|
byte[] file = build();
|
||||||
|
|||||||
@@ -40,7 +40,10 @@ class PieceTableTest {
|
|||||||
@Test
|
@Test
|
||||||
void negativeStartCp_rejected() throws Exception {
|
void negativeStartCp_rejected() throws Exception {
|
||||||
byte[] table = clxOnePiece(10, 0, false);
|
byte[] table = clxOnePiece(10, 0, false);
|
||||||
table[5] = (byte) 0xFF; // aCP[0] = 0xFFFFFFFF → (int) −1
|
table[5] = (byte) 0xFF; // aCP[0] (u32 LE @5) = 0xFFFFFFFF → (int) −1
|
||||||
|
table[6] = (byte) 0xFF;
|
||||||
|
table[7] = (byte) 0xFF;
|
||||||
|
table[8] = (byte) 0xFF;
|
||||||
ConversionException e = assertThrows(ConversionException.class,
|
ConversionException e = assertThrows(ConversionException.class,
|
||||||
() -> PieceTable.read(table, 0, table.length, new byte[64], 64));
|
() -> PieceTable.read(table, 0, table.length, new byte[64], 64));
|
||||||
assertSame(ConversionException.class, e.getClass());
|
assertSame(ConversionException.class, e.getClass());
|
||||||
|
|||||||
Reference in New Issue
Block a user