Repository: jd-boyd/python-lzo (language: python)

Issue:
Make py_ssize_t_clean.



Discussion / hints:

Fix patch:
diff --git a/lzomodule.c b/lzomodule.c
index b5fa542..e9ca432 100644
--- a/lzomodule.c
+++ b/lzomodule.c
@@ -31,6 +31,8 @@
 
 #define MODULE_VERSION  "1.12"
 
+#define PY_SSIZE_T_CLEAN
+
 #include <Python.h>
 #include <lzo/lzo1x.h>
 
@@ -83,7 +85,7 @@ compress(PyObject *dummy, PyObject *args)
     lzo_uint in_len;
     lzo_uint out_len;
     lzo_uint new_len;
-    int len;
+    Py_ssize_t len;
     int level = 1;
     int header = 1;
     int err;
@@ -95,6 +97,16 @@ compress(PyObject *dummy, PyObject *args)
     if (len < 0)
         return NULL;
 
+    if (len > LZO_UINT_MAX) {
+      PyErr_SetString(LzoError, "Input size is larger than LZO_UINT_MAX");
+      return NULL;
+    }
+
+    if ((len + len / 16 + 64 + 3) > LZO_UINT_MAX) {
+      PyErr_SetString(LzoError, "Output size is larger than LZO_UINT_MAX");
+      return NULL;
+    }
+
     in_len = len;
     out_len = in_len + in_len / 16 + 64 + 3;
 
@@ -189,7 +201,7 @@ decompress(PyObject *dummy, PyObject *args)
     lzo_uint in_len;
     lzo_uint out_len;
     lzo_uint new_len;
-    int len;
+    Py_ssize_t len;
     int buflen = -1;
     int header = 1;
     int err;
@@ -274,7 +286,7 @@ optimize(PyObject *dummy, PyObject *args)
     lzo_uint in_len;
     lzo_uint out_len;
     lzo_uint new_len;
-    int len;
+    Py_ssize_t len;
     int err;
     int header = 1;
     int buflen = -1;
@@ -356,7 +368,7 @@ static PyObject *
 adler32(PyObject *dummy, PyObject *args)
 {
     char *buf;
-    int len;
+    Py_ssize_t len;
     unsigned long val = 1; /* == lzo_adler32(0, NULL, 0); */
 
     UNUSED(dummy);
@@ -392,7 +404,7 @@ static PyObject *
 crc32(PyObject *dummy, PyObject *args)
 {
     char *buf;
-    int len;
+    Py_ssize_t len;
     unsigned long val = 0; /* == lzo_crc32(0, NULL, 0); */
 
     UNUSED(dummy);
diff --git a/tox.ini b/tox.ini
index 2b6468c..0e79dd1 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
 # content of: tox.ini , put in same dir as setup.py
 [tox]
-envlist = py27,py35
+envlist = py27,py37
 [testenv]
 deps=
     nose


Test patch:
diff --git a/tests/test.py b/tests/test.py
index df0c355..af761d9 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -96,10 +96,13 @@ def test_version():
 
 def test_lzo():
     yield gen, b"aaaaaaaaaaaaaaaaaaaaaaaa"
-    yield gen_raw, b"aaaaaaaaaaaaaaaaaaaaaaaa"
     yield gen, b"abcabcabcabcabcabcabcabc"
-    yield gen_raw, b"abcabcabcabcabcabcabcabc"
     yield gen, b"abcabcabcabcabcabcabcabc", 9
+
+
+def test_lzo_raw():
+    yield gen_raw, b"aaaaaaaaaaaaaaaaaaaaaaaa"
+    yield gen_raw, b"abcabcabcabcabcabcabcabc"
     yield gen_raw, b"abcabcabcabcabcabcabcabc", 9
 
 
@@ -114,3 +117,10 @@ def test_lzo_big():
 
 def test_lzo_raw_big():
     gen_raw(b" " * 131072)
+
+
+if sys.maxsize > 1<<32:
+    # This test raises OverflowError on 32-bit Pythons. Compressing
+    # this much data requires a 64-bit system.
+    def test_lzo_compress_extremely_big():
+        b = lzo.compress(bytes(bytearray((1024**3)*2)))
diff --git a/tests/util.py b/tests/util.py
index 0a2f4ed..c7bd5f0 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -45,7 +45,7 @@ def get_sys_path(p=None):
     if p: p0 = p[0]
     #
     plat = get_platform()
-    plat_specifier = "%s-%s" % (plat, sys.version[:3])
+    plat_specifier = "%s-%d.%d" % (plat, sys.version_info[0], sys.version_info[1])
     ##print plat, plat_specifier
     #
     for prefix in (p0, os.curdir, os.pardir,):