Skip to content

Commit 8d1a144

Browse files
committed
resolved detection and reporting of crashes
1 parent abc1da5 commit 8d1a144

2 files changed

Lines changed: 33 additions & 30 deletions

File tree

src/test/java/org/xerial/snappy/fuzz/SnappyCombinedFuzzer.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,26 @@ private static void testFramed(FuzzedDataProvider data) {
130130
byte[] compressed = compressedBuf.toByteArray();
131131

132132
for (int bufferSize : new int[]{1, 64, 256, 1024, 4096}) {
133-
SnappyFramedInputStream framedIn = new SnappyFramedInputStream(
134-
new ByteArrayInputStream(compressed), true);
135-
ByteArrayOutputStream out = new ByteArrayOutputStream();
136-
byte[] buf = new byte[bufferSize];
137-
int readBytes;
138-
while ((readBytes = framedIn.read(buf)) != -1) {
139-
out.write(buf, 0, readBytes);
133+
try (SnappyFramedInputStream framedIn = new SnappyFramedInputStream(
134+
new ByteArrayInputStream(compressed), true)) {
135+
ByteArrayOutputStream out = new ByteArrayOutputStream();
136+
byte[] buf = new byte[bufferSize];
137+
int readBytes;
138+
while ((readBytes = framedIn.read(buf)) != -1) {
139+
out.write(buf, 0, readBytes);
140+
}
141+
out.flush();
140142
}
141-
out.flush();
142143
}
143144
} catch (IOException e) {
145+
throw new RuntimeException(e);
144146
}
145147

146-
try {
147-
byte[] invalidData = data.consumeBytes(100);
148-
SnappyFramedInputStream invalidIn = new SnappyFramedInputStream(
149-
new ByteArrayInputStream(invalidData));
148+
try (SnappyFramedInputStream invalidIn = new SnappyFramedInputStream(
149+
new ByteArrayInputStream(data.consumeBytes(100)))) {
150150
while (invalidIn.read() != -1) {}
151151
} catch (IOException e) {
152+
throw new RuntimeException(e);
152153
}
153154
}
154155

@@ -207,22 +208,22 @@ private static void testBlockStream(FuzzedDataProvider data) {
207208
out.close();
208209
byte[] compressed = compressedBuf.toByteArray();
209210

210-
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
211-
ByteArrayOutputStream result = new ByteArrayOutputStream();
212-
byte[] buf = new byte[1024];
213-
int read;
214-
while ((read = in.read(buf)) != -1) {
215-
result.write(buf, 0, read);
211+
try (SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed))) {
212+
ByteArrayOutputStream result = new ByteArrayOutputStream();
213+
byte[] buf = new byte[1024];
214+
int read;
215+
while ((read = in.read(buf)) != -1) {
216+
result.write(buf, 0, read);
217+
}
216218
}
217-
in.close();
218219
} catch (Exception e) {
220+
throw new RuntimeException(e);
219221
}
220222

221-
try {
222-
byte[] invalid = data.consumeBytes(100);
223-
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(invalid));
223+
try (SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(data.consumeBytes(100)))) {
224224
while (in.read() != -1) {}
225225
} catch (Exception e) {
226+
throw new RuntimeException(e);
226227
}
227228
}
228229

src/test/java/org/xerial/snappy/fuzz/SnappyStreamFuzzer.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,20 @@ public static void fuzzerTestOneInput(FuzzedDataProvider data) {
4040
snappyOut.write(original);
4141
snappyOut.close();
4242
byte[] compressed = compressedBuf.toByteArray();
43-
SnappyInputStream snappyIn = new SnappyInputStream(new ByteArrayInputStream(compressed));
44-
ByteArrayOutputStream out = new ByteArrayOutputStream();
45-
byte[] buf = new byte[4096];
46-
for (int readBytes = 0; (readBytes = snappyIn.read(buf)) != -1; ) {
47-
out.write(buf, 0, readBytes);
43+
44+
try (SnappyInputStream snappyIn = new SnappyInputStream(new ByteArrayInputStream(compressed))) {
45+
ByteArrayOutputStream out = new ByteArrayOutputStream();
46+
byte[] buf = new byte[4096];
47+
for (int readBytes = 0; (readBytes = snappyIn.read(buf)) != -1; ) {
48+
out.write(buf, 0, readBytes);
49+
}
50+
out.flush();
51+
uncompressed = out.toByteArray();
4852
}
49-
out.flush();
50-
uncompressed = out.toByteArray();
5153
}
5254
catch (IOException e)
5355
{
54-
return;
56+
throw new RuntimeException(e);
5557
}
5658

5759
if(Arrays.equals(original,uncompressed) == false)

0 commit comments

Comments
 (0)