Skip to content

Commit a53ebab

Browse files
committed
Fix zlib.Decompress.flush error handling
1 parent 9ff366f commit a53ebab

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

graalpython/com.oracle.graal.python.cext/zlib/zlib.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,13 +1108,6 @@ int zlib_Decompress_flush(zlib_stream *zst, ssize_t length) {
11081108
error_occurred(zst, INFLATE_END_ERROR);
11091109
return err;
11101110
}
1111-
} else if (err != Z_OK && err != Z_BUF_ERROR) {
1112-
/* We will only get Z_BUF_ERROR if the output buffer was full
1113-
but there wasn't more output when we tried again, so it is
1114-
not an error condition.
1115-
*/
1116-
error_occurred(zst, INFLATE_FLUSH_ERROR);
1117-
return err;
11181111
}
11191112
if (zst->output->size) {
11201113
zst->output_size = zst->zst.next_out - zst->output->buf;

graalpython/com.oracle.graal.python.test/src/tests/test_zlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def test_custom_bufsize(self):
164164
compressed = zlib.compress(data, 1)
165165
self.assertEqual(zlib.decompress(compressed, 15, CustomInt()), data)
166166

167+
def test_decoding_flush_ignore_error(self):
168+
d = zlib.decompressobj()
169+
self.assertRaises(zlib.error, d.decompress, b"asdf")
170+
d.flush()
171+
172+
167173
HAMLET_SCENE = b"""
168174
LAERTES
169175

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressBuiltins.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.oracle.graal.python.runtime.NFIZlibSupport;
7474
import com.oracle.graal.python.runtime.NativeLibrary;
7575
import com.oracle.graal.python.runtime.PythonContext;
76+
import com.oracle.graal.python.runtime.exception.PException;
7677
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7778
import com.oracle.graal.python.util.PythonUtils;
7879
import com.oracle.truffle.api.dsl.Cached;
@@ -253,7 +254,13 @@ PBytes doit(ZLibCompObject.NativeZlibCompObject self, int length,
253254
PBytes doit(ZLibCompObject.JavaZlibCompObject self, int length,
254255
@Cached ZlibNodes.JavaDecompressNode decompressNode,
255256
@Cached BytesNodes.ToBytesNode toBytes) {
256-
byte[] res = decompressNode.execute(self, toBytes.execute(self.getUnconsumedTail()), 0, length, factory());
257+
byte[] res;
258+
try {
259+
res = decompressNode.execute(self, toBytes.execute(self.getUnconsumedTail()), 0, length, factory());
260+
} catch (PException e) {
261+
// CPython ignores errors here
262+
res = PythonUtils.EMPTY_BYTE_ARRAY;
263+
}
257264
self.setUninitialized();
258265
return factory().createBytes(res);
259266
}

0 commit comments

Comments
 (0)