From 4a5624cd84e300e704e83f3fffb6cb3ebd41870e Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 5 Jul 2016 17:58:50 +0200 Subject: [PATCH] Fix int/ssize_t mismatch in tests for the file shim MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using "s#" in PyArg_ParseTuple caused test failures on big-endian architectures. The choice between int and Py_ssize_t is governed by the PY_SSIZE_T_CLEAN macro, but in a header file we don't control. the macro. Use "s*" with Py_buffer instead. Reported by Dan HorĂ¡k here: https://bugzilla.redhat.com/show_bug.cgi?id=1351971 --- test/test_py3c.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/test/test_py3c.c b/test/test_py3c.c index 0cb3185..63a1619 100644 --- a/test/test_py3c.c +++ b/test/test_py3c.c @@ -288,20 +288,33 @@ static PyObject *file_fread(PyObject *mod, PyObject *o) { FILE *f = (FILE*) PyCapsule_GetPointer(o, "test_py3c._file"); if (!f) return NULL; size = fread(data, 1, 200, f); + if (size > 199) { + return PyErr_Format(PyExc_AssertionError, + "size read is too big (%zu)", size); + } return PyStr_FromStringAndSize(data, size); } static PyObject *file_fwrite(PyObject *mod, PyObject *args) { PyObject *o; - char *data; - Py_ssize_t size = 0; + Py_buffer pybuf; + size_t size_written; FILE *f; - if (!PyArg_ParseTuple(args, "Os#", &o, &data, &size)) return NULL; + if (!PyArg_ParseTuple(args, "Os*", &o, &pybuf)) return NULL; f = (FILE*) PyCapsule_GetPointer(o, "test_py3c._file"); - if (!f) return NULL; - size = fwrite(data, 1, size, f); - if (size < 0) return PyErr_SetFromErrno(PyExc_OSError); + if (!f) { + PyBuffer_Release(&pybuf); + return NULL; + } + size_written = fwrite(pybuf.buf, 1, pybuf.len, f); + if (size_written != (size_t)pybuf.len) { + PyBuffer_Release(&pybuf); + return PyErr_Format(PyExc_AssertionError, + "size written (%zu) != size of string (%zd)", + size_written, pybuf.len); + } fflush(f); + PyBuffer_Release(&pybuf); Py_RETURN_NONE; }