Skip to content

Commit

Permalink
c-ext: change casts between Py_ssize_t and size_t
Browse files Browse the repository at this point in the history
In all these cases, we are comparing a Py_ssize_t (from Python
obviously) with a size_t in zstd. The Py_ssize_t type is signed.
But in these cases, the value refers to the length of a Py_buffer.
The length of a Py_buffer can never be negative. And the size
of Py_ssize_t must be the same as size_t. So it follows that casting
a Py_buffer to size_t should be safe.

This avoids the use of ssize_t in our code. And this should hopefully
unblock building the extension with PyPy on Windows.

Fixes #61.
  • Loading branch information
indygreg committed Nov 3, 2018
1 parent 14c4bbc commit d2228c4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ Bug Fixes

* ``zstd_cffi.py`` added to ``setup.py`` (#60).

Changes
-------

* Change some integer casts to avoid ``ssize_t`` (#61).

0.10.1 (released 2018-10-08)
============================

Expand Down
2 changes: 1 addition & 1 deletion c-ext/compressionwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static PyObject* ZstdCompressionWriter_write(ZstdCompressionWriter* self, PyObje
input.size = source.len;
input.pos = 0;

while ((ssize_t)input.pos < source.len) {
while (input.pos < (size_t)source.len) {
Py_BEGIN_ALLOW_THREADS
zresult = ZSTD_compress_generic(self->compressor->cctx, &output, &input, ZSTD_e_continue);
Py_END_ALLOW_THREADS
Expand Down
2 changes: 1 addition & 1 deletion c-ext/compressobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static PyObject* ZstdCompressionObj_compress(ZstdCompressionObj* self, PyObject*
input.size = source.len;
input.pos = 0;

while ((ssize_t)input.pos < source.len) {
while (input.pos < (size_t)source.len) {
Py_BEGIN_ALLOW_THREADS
zresult = ZSTD_compress_generic(self->compressor->cctx, &self->output,
&input, ZSTD_e_continue);
Expand Down
2 changes: 1 addition & 1 deletion c-ext/decompressionwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static PyObject* ZstdDecompressionWriter_write(ZstdDecompressionWriter* self, Py
input.size = source.len;
input.pos = 0;

while ((ssize_t)input.pos < source.len) {
while (input.pos < (size_t)source.len) {
Py_BEGIN_ALLOW_THREADS
zresult = ZSTD_decompress_generic(self->decompressor->dctx, &output, &input);
Py_END_ALLOW_THREADS
Expand Down

0 comments on commit d2228c4

Please sign in to comment.