Skip to content

Commit 152426b

Browse files
committed
Always pass cause exception to ZipParseException
1 parent a8115f3 commit 152426b

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

src/main/java/software/coley/lljzip/format/model/CentralDirectoryFileHeader.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,31 @@ public void read(@Nonnull MemorySegment data, long offset) throws ZipParseExcept
100100
internalFileAttributes = readWord(data, offset, 36);
101101
externalFileAttributes = readQuad(data, offset, 38);
102102
relativeOffsetOfLocalHeader = readMaskedLongQuad(data, offset, 42);
103+
} catch (IndexOutOfBoundsException ex) {
104+
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_OTHER);
103105
} catch (Throwable t) {
104-
throw new ZipParseException(ZipParseException.Type.OTHER);
106+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
105107
}
106108
try {
107-
fileName = StringData.of(readSlice(data, offset, 46, fileNameLength));
109+
fileName = StringData.of(data, offset + 46, fileNameLength);
108110
} catch (IndexOutOfBoundsException ex) {
109111
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_NAME);
110112
} catch (Throwable t) {
111-
throw new ZipParseException(ZipParseException.Type.OTHER);
113+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
112114
}
113115
try {
114116
extraField = MemorySegmentData.of(data, offset + 46 + fileNameLength, extraFieldLength);
115117
} catch (IndexOutOfBoundsException ex) {
116118
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_EXTRA);
117119
} catch (Throwable t) {
118-
throw new ZipParseException(ZipParseException.Type.OTHER);
120+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
119121
}
120122
try {
121123
fileComment = StringData.of(readSlice(data, offset, 46 + fileNameLength + extraFieldLength, fileCommentLength));
122124
} catch (IndexOutOfBoundsException ex) {
123125
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_CEN_COMMENT);
124126
} catch (Throwable t) {
125-
throw new ZipParseException(ZipParseException.Type.OTHER);
127+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
126128
}
127129
}
128130

src/main/java/software/coley/lljzip/format/model/JvmLocalFileHeader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void read(@Nonnull MemorySegment data, long offset) throws ZipParseExcept
9191
} catch (IndexOutOfBoundsException ex) {
9292
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_DATA);
9393
} catch (Throwable t) {
94-
throw new ZipParseException(ZipParseException.Type.OTHER);
94+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
9595
}
9696

9797
// Update sizes where possible

src/main/java/software/coley/lljzip/format/model/LocalFileHeader.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,32 @@ public void read(@Nonnull MemorySegment data, long offset) throws ZipParseExcept
8282
uncompressedSize = readMaskedLongQuad(data, offset, 22);
8383
fileNameLength = readWord(data, offset, 26);
8484
extraFieldLength = readWord(data, offset, 28);
85+
} catch (IndexOutOfBoundsException ex) {
86+
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_OTHER);
8587
} catch (Throwable t) {
86-
throw new ZipParseException(ZipParseException.Type.OTHER);
88+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
8789
}
8890
try {
8991
fileName = StringData.of(readSlice(data, offset, MIN_FIXED_SIZE, fileNameLength));
9092
} catch (IndexOutOfBoundsException ex) {
9193
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_NAME);
9294
} catch (Throwable t) {
93-
throw new ZipParseException(ZipParseException.Type.OTHER);
95+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
9496
}
9597
try {
9698
extraField = MemorySegmentData.of(data, offset + MIN_FIXED_SIZE + fileNameLength, extraFieldLength);
9799
} catch (IndexOutOfBoundsException ex) {
98100
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_EXTRA);
99101
} catch (Throwable t) {
100-
throw new ZipParseException(ZipParseException.Type.OTHER);
102+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
101103
}
102104
long fileDataLength = (compressionMethod == STORED) ? uncompressedSize : compressedSize;
103105
try {
104106
fileData = MemorySegmentData.of(readLongSlice(data, offset, MIN_FIXED_SIZE + fileNameLength + extraFieldLength, fileDataLength));
105107
} catch (IndexOutOfBoundsException ex) {
106108
throw new ZipParseException(ex, ZipParseException.Type.IOOBE_FILE_DATA);
107109
} catch (Throwable t) {
108-
throw new ZipParseException(ZipParseException.Type.OTHER);
110+
throw new ZipParseException(t, ZipParseException.Type.OTHER);
109111
}
110112
}
111113

src/main/java/software/coley/lljzip/format/model/ZipParseException.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
public class ZipParseException extends Exception {
1313
private final Type type;
1414

15-
public ZipParseException(@Nullable IndexOutOfBoundsException cause, @Nonnull Type type) {
15+
public ZipParseException(@Nullable Throwable cause, @Nonnull Type type) {
1616
super(type.getMessage(), cause);
1717
this.type = type;
1818
}
1919

20-
public ZipParseException(@Nonnull Type type) {
21-
this(null, type);
22-
}
23-
2420
/**
2521
* @return Parse failure case.
2622
*/
@@ -37,6 +33,7 @@ public enum Type {
3733
IOOBE_FILE_DATA("Bounds check failed reading file data"),
3834
IOOBE_FILE_EXTRA("Bounds check failed reading file extra"),
3935
IOOBE_CEN_COMMENT("Bounds check failed reading directory comment"),
36+
IOOBE_OTHER("Bounds check failed"),
4037
OTHER("Unknown zip parse error");
4138

4239
private final String message;

0 commit comments

Comments
 (0)