From 1f49adf01ed06e320a401fd9129175d27bd1753e Mon Sep 17 00:00:00 2001 From: Jonathan Underwood Date: Sat, 13 May 2017 12:54:08 +0100 Subject: [PATCH 1/3] Remove check for zero size in block compress (#27) There are valid use cases for passing a zero length string to compress: https://github.com/python-lz4/python-lz4/issues/27 --- lz4/block/_block.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lz4/block/_block.c b/lz4/block/_block.c index ac13ecd9..4a9f93ba 100644 --- a/lz4/block/_block.c +++ b/lz4/block/_block.c @@ -121,11 +121,6 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs) return NULL; } - if (source_size <= 0) { - PyErr_Format(PyExc_ValueError, "Input source data size invalid: %d bytes", source_size); - return NULL; - } - if (!strncmp (mode, "default", sizeof ("default"))) { comp = DEFAULT; From 94c8473596cf4ff18403791566776dbb7d68faeb Mon Sep 17 00:00:00 2001 From: Jonathan Underwood Date: Sat, 13 May 2017 13:01:19 +0100 Subject: [PATCH 2/3] Allow dest size of 0 in block decompress (#27) --- lz4/block/_block.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lz4/block/_block.c b/lz4/block/_block.c index 4a9f93ba..a31b61fe 100644 --- a/lz4/block/_block.c +++ b/lz4/block/_block.c @@ -253,7 +253,7 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs) source_size -= hdr_size; } - if (dest_size <= 0 || dest_size > PY_SSIZE_T_MAX) + if (dest_size < 0 || dest_size > PY_SSIZE_T_MAX) { PyErr_Format (PyExc_ValueError, "Invalid size in header: 0x%zu", dest_size); From 96f2a8988618977d1f0e6ac58a128fdd0bbf298a Mon Sep 17 00:00:00 2001 From: Jonathan Underwood Date: Sat, 13 May 2017 13:01:54 +0100 Subject: [PATCH 3/3] Add test for zero size input for block compress/decompress (#27) --- tests/test_block.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_block.py b/tests/test_block.py index 9be2c781..29f458a2 100644 --- a/tests/test_block.py +++ b/tests/test_block.py @@ -8,6 +8,10 @@ class TestLZ4Block(unittest.TestCase): + def test_empty_string(self): + DATA = b'' + self.assertEqual(DATA, lz4.block.decompress(lz4.block.compress(DATA))) + def test_random(self): DATA = os.urandom(128 * 1024) # Read 128kb self.assertEqual(DATA, lz4.block.decompress(lz4.block.compress(DATA)))