From 9b0e738ab047536e199176e35d93c5743dbe53a1 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Thu, 23 Jan 2025 18:14:58 +0000
Subject: [PATCH] Use ValueError on insufficient space in output buffer

Following mmap.write(), and (more roughly) memoryview, ctypes buffers &
numpy.

Also don't automatically error for 0 size output buffer
---
 src/zlib_into.c   | 14 ++------------
 test_zlib_into.py | 10 ++++++++--
 2 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/src/zlib_into.c b/src/zlib_into.c
index f3e9178..39005e4 100644
--- a/src/zlib_into.c
+++ b/src/zlib_into.c
@@ -72,11 +72,6 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
         return NULL;
     }
 
-    if (output.len <= 0) {
-        PyErr_SetString(PyExc_ValueError, "Output buffer may not be 0 size");
-        goto done;
-    }
-
     zst.opaque = NULL;
     zst.zalloc = PyZlib_Malloc;
     zst.zfree = PyZlib_Free;
@@ -113,7 +108,7 @@ compress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
         case Z_OK:
         case Z_BUF_ERROR:
             deflateEnd(&zst);
-            PyErr_SetString(PyExc_BufferError, "Not enough space in output buffer");
+            PyErr_SetString(PyExc_ValueError, "Not enough space in output buffer");
             goto done;
         default:
             deflateEnd(&zst);
@@ -150,11 +145,6 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
         return NULL;
     }
 
-    if (output.len <= 0) {
-        PyErr_SetString(PyExc_ValueError, "Output buffer may not be 0 size");
-        goto done;
-    }
-
     zst.opaque = NULL;
     zst.zalloc = PyZlib_Malloc;
     zst.zfree = PyZlib_Free;
@@ -187,7 +177,7 @@ decompress_into(PyObject *module, PyObject *args, PyObject *kwargs) {
         case Z_OK:
         case Z_BUF_ERROR:
             deflateEnd(&zst);
-            PyErr_SetString(PyExc_BufferError, "Not enough space in output buffer");
+            PyErr_SetString(PyExc_ValueError, "Not enough space in output buffer");
             goto done;
         default:
             deflateEnd(&zst);
diff --git a/test_zlib_into.py b/test_zlib_into.py
index 8f89f2c..fd29d9f 100644
--- a/test_zlib_into.py
+++ b/test_zlib_into.py
@@ -30,7 +30,7 @@ def test_compress_into_err():
     with pytest.raises(TypeError):
         compress_into(data_in, memoryview(buf).toreadonly())  # Output not writable
 
-    with pytest.raises(BufferError):
+    with pytest.raises(ValueError):
         compress_into(data_in, buf[:10])  # Output too small
 
 
@@ -54,9 +54,15 @@ def test_decompress_into():
 
     # Not enough space, by 1 byte
     buf3 = bytearray(len(expanded_data) - 1)
-    with pytest.raises(BufferError):
+    with pytest.raises(ValueError):
         decompress_into(compressed_data, buf3)
 
+    # Corner case: decompress 0 bytes
+    compressed_nothing = zlib.compress(b'')
+    buf_size_0 = bytearray(0)
+    n_bytes_out = decompress_into(compressed_nothing, buf_size_0)
+    assert n_bytes_out == 0
+
 
 def test_decompress_into_err():
     expanded_data = b'abcde' * 5000
-- 
GitLab