From 6fae5bbc185301057913a800f14202be8d630cf2 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 7 Oct 2023 22:12:25 +0000 Subject: [PATCH] Bug 66425: Avoid Exceptions found via oss-fuzz We try to avoid throwing NullPointerExceptions or endless allocations, but it was possible to trigger one here with a specially crafted input-file Should fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=62697 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912793 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/SSTDeserializer.java | 4 +++- .../poifs/filesystem/DocumentInputStream.java | 3 ++- .../poi/hssf/dev/BaseTestIteratingXLS.java | 1 + .../poi/hssf/record/TestSSTDeserializer.java | 4 +++- ...nimized-POIHSSFFuzzer-4819588401201152.xls | Bin 0 -> 3182 bytes test-data/spreadsheet/stress.xls | Bin 56320 -> 56832 bytes 6 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-4819588401201152.xls diff --git a/poi/src/main/java/org/apache/poi/hssf/record/SSTDeserializer.java b/poi/src/main/java/org/apache/poi/hssf/record/SSTDeserializer.java index d840078c73e..16786e6dae2 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/SSTDeserializer.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/SSTDeserializer.java @@ -48,7 +48,9 @@ public void manufactureStrings(int stringCount, RecordInputStream in) { UnicodeString str; if (in.available() == 0 && (!in.hasNextRecord() || in.getNextSid() != ContinueRecord.sid)) { LOG.atError().log("Ran out of data before creating all the strings! String at index {}", box(i)); - str = new UnicodeString(""); + + // not much sense in trying to continue reading in this case, file seems to be broken + return; } else { str = new UnicodeString(in); } diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java b/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java index a4e76fab89e..92021f2f261 100644 --- a/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java +++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/DocumentInputStream.java @@ -169,7 +169,8 @@ public int read(byte[] b, int off, int len) throws IOException { throw new IllegalArgumentException("buffer must not be null"); } if (off < 0 || len < 0 || b.length < off + len) { - throw new IndexOutOfBoundsException("can't read past buffer boundaries"); + throw new IndexOutOfBoundsException("can't read past buffer boundaries with off: " + off + + ", len: " + len + ", b.length: " + b.length); } if (len == 0) { return 0; diff --git a/poi/src/test/java/org/apache/poi/hssf/dev/BaseTestIteratingXLS.java b/poi/src/test/java/org/apache/poi/hssf/dev/BaseTestIteratingXLS.java index 26627b5a9b1..6e18940a721 100644 --- a/poi/src/test/java/org/apache/poi/hssf/dev/BaseTestIteratingXLS.java +++ b/poi/src/test/java/org/apache/poi/hssf/dev/BaseTestIteratingXLS.java @@ -88,6 +88,7 @@ protected Map> getExcludes() { excludes.put("64130.xls", OldExcelFormatException.class); // fuzzed binaries excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-6322470200934400.xls", RuntimeException.class); + excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-4819588401201152.xls", RuntimeException.class); return excludes; } diff --git a/poi/src/test/java/org/apache/poi/hssf/record/TestSSTDeserializer.java b/poi/src/test/java/org/apache/poi/hssf/record/TestSSTDeserializer.java index c627bbfc558..3dcdf87a272 100644 --- a/poi/src/test/java/org/apache/poi/hssf/record/TestSSTDeserializer.java +++ b/poi/src/test/java/org/apache/poi/hssf/record/TestSSTDeserializer.java @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more package org.apache.poi.hssf.record; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; import java.io.InputStream; @@ -137,6 +138,7 @@ void test65543() throws IOException { deserializer.manufactureStrings(2, in); assertEquals("At a dinner party or", strings.get(0) + ""); - assertEquals("", strings.get(1) + ""); + assertThrows(IndexOutOfBoundsException.class, + () -> strings.get(1)); } } diff --git a/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-4819588401201152.xls b/test-data/spreadsheet/clusterfuzz-testcase-minimized-POIHSSFFuzzer-4819588401201152.xls new file mode 100644 index 0000000000000000000000000000000000000000..524001f77e33ea48f7b36a49a3ed0037aabc5f44 GIT binary patch literal 3182 zcmc&$OK%fb6h3$CIAa2h6Ot6d%PDzh9GsVVN!UD+O%O;a6%qn93Al}$#DUnGY>)%A zRYhB=V#SXBgl^cdXxMa7Rcd$L1|$#?3nEqGp(aefb7p*NhZ!YCm3qeCozLHWopT?@ z{^R$$onHSb>lixLE?0N#aazy!j;7N7>$3Ty*D0BQjo0jdM) zf$cy8&fGE%cd<5(VT7fp89q0f$fi9pM=mD(n?@|n| z^@1M&;y@qJPs$v?n`npzQ6_+c0LSGThv{R_ee}xZzbo(ZANF{^*~e%OPabvjxNF)gkfOHH zTl$QSrZ#G~srr$4tS_E86gz$bmxfkdH;f?F6+-qMyz2`=d2cUN+jnjRsi9D1k1JlC zFLB2oNa!K@cq*OF$G=5(Pxof!uOq%2#>&Lp%epDv%-1e zXJi$V7FUs5%q5jVnUedDDsW^N6Pd$&h2*k|e1b!0&))lr2a;hy`hb35;1%+cNw+W` z=cUVLBOvXh;5@|E#mOsdB2|SV^kCa>$ZH%Ir?{Mnz` zG+7UA&O^KEpE(|e;Yl=jC#5+|Kd_k@KWx< zEeizYd@5W&FyjgSHE^C_<1@!kfL`EY*3e;Sd~N;BJ=BBnaQ$xfd1(C}+JJ{P6nb~O zq0)A>N-k5PRC9ef=%F3*&<>YrlJF}moKt(GY#c{B>Y-&b`CM;wWNtp1yNW;A(cD6= md_KMlyc7JZjMP|OS?RZYdNyb2PVz09pGw>I8#|iL+W!EmMJh`G literal 0 HcmV?d00001 diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls index 2352603880adcfa88e6188ee15a73d67353179c1..d06a4e4d5b74ce0d1ad463948d8cab670be9244e 100644 GIT binary patch delta 3487 zcmZved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