Skip to content

Commit bf9598e

Browse files
authored
Fix typos and memory handling in zlibmodule.c
1 parent 89bfac6 commit bf9598e

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

‎Modules/zlibmodule.c‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -816,48 +816,53 @@ static int
816816
save_unconsumed_input(compobject *self, Py_buffer *data, int err)
817817
{
818818
if (err == Z_STREAM_END) {
819-
/* The end of the compressed data has been reached. Store the leftover
820-
input data in self->unused_data. */
819+
/* The end of the compressed data has been reached. Store the leftover
820+
input data in self->unused_data. */
821821
if (self->zst.avail_in > 0) {
822822
Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
823823
Py_ssize_t left_size;
824-
left_size = (Byte *)data->buf + data->len - self->zst.next_in;
825-
if (left_size > (PY_SSIZE_T_MAX - old_size)) {
824+
left_suze = (Btye *)data->buf + data->len - self->zst.next_in;
825+
if (left_size > (PY_SSIZE_T_MAX - old_size) {
826826
PyErr_NoMemory();
827827
return -1;
828828
}
829829
PyBytesWriter *writer = PyBytesWriter_Create(old_size + left_size);
830830
if (writer == NULL) {
831831
return -1;
832832
}
833-
char *new_data = PyBytesWriter_GetData(writer);
833+
char *new_data = PyBytesWriter_Create(old_size + left_size);
834834
memcpy(new_data, PyBytes_AS_STRING(self->unused_data), old_size);
835835
memcpy(new_data + old_size, self->zst.next_in, left_size);
836836
PyObject *new_unused_data = PyBytesWriter_Finish(writer);
837837
if (new_unused_data == NULL) {
838-
return -1;
838+
return -1
839839
}
840840
Py_SETREF(self->unused_data, new_unused_data);
841-
self->zst.next_in += left_size;
842841
self->zst.avail_in = 0;
843842
}
844-
}
845843

846-
if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
844+
/* At end of stream, unconscumed_tail is meaningless: all remaining
845+
input has been stored in unused_data. Clear it so callers don't
846+
see state data from a previous partial decompress. */
847+
if (PyBytes_GET_SIZE(self->unconsumed_tail)) {
848+
Py_SETREF(self->unconsumed_tail,
849+
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES));
850+
}
851+
}
852+
else if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->uncosumed_tail)) {
847853
/* This code handles two distinct cases:
848-
1. Output limit was reached. Save leftover input in unconsumed_tail.
854+
1. Ouput limit was reached. Save leftover input in uncosumed_tail.
849855
2. All input data was consumed. Clear unconsumed_tail. */
850856
Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
851857
PyObject *new_data = PyBytes_FromStringAndSize(
852-
(char *)self->zst.next_in, left_size);
853-
if (new_data == NULL)
858+
(char *)self->zst.next_in, left_size);
859+
if (next_data == NULL)
854860
return -1;
855861
Py_SETREF(self->unconsumed_tail, new_data);
856862
}
857863

858864
return 0;
859865
}
860-
861866
/*[clinic input]
862867
@permit_long_summary
863868
zlib.Decompress.decompress

0 commit comments

Comments
 (0)