Skip to content

Commit e0335ac

Browse files
committed
Fix different signedness comparisons.
1 parent 2a5331c commit e0335ac

File tree

8 files changed

+148
-136
lines changed

8 files changed

+148
-136
lines changed

src/pylzma/pylzma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pylzma_ppmd_decompress(PyObject *self, PyObject *args)
469469
}
470470
if (i != outsize) {
471471
res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA);
472-
} else if (s.processed + (s.cur - s.begin) != length || !Ppmd7z_RangeDec_IsFinishedOK(&rc)) {
472+
} else if (s.processed + (s.cur - s.begin) != (UInt64)length || !Ppmd7z_RangeDec_IsFinishedOK(&rc)) {
473473
res = SZ_ERROR_DATA;
474474
}
475475
}

src/pylzma/pylzma_compressobj.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
* modify it under the terms of the GNU Lesser General Public
1010
* License as published by the Free Software Foundation; either
1111
* version 2.1 of the License, or (at your option) any later version.
12-
*
12+
*
1313
* This library is distributed in the hope that it will be useful,
1414
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1616
* Lesser General Public License for more details.
17-
*
17+
*
1818
* You should have received a copy of the GNU Lesser General Public
1919
* License along with this library; if not, write to the Free Software
2020
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21-
*
21+
*
2222
* $Id$
2323
*
2424
*/
@@ -47,17 +47,17 @@ pylzma_comp_compress(CCompressionObject *self, PyObject *args)
4747
PyObject *result = NULL;
4848
char *data;
4949
PARSE_LENGTH_TYPE length;
50-
PY_LONG_LONG bufsize=BLOCK_SIZE;
50+
Py_ssize_t bufsize=BLOCK_SIZE;
5151
int res;
5252
size_t before;
53-
54-
if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &bufsize))
53+
54+
if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &bufsize))
5555
return NULL;
56-
56+
5757
if (!MemoryInOutStreamAppend(&self->inStream, (Byte *) data, length)) {
5858
return PyErr_NoMemory();
5959
}
60-
60+
6161
Py_BEGIN_ALLOW_THREADS
6262
while (1) {
6363
before = self->inStream.avail;
@@ -71,17 +71,17 @@ pylzma_comp_compress(CCompressionObject *self, PyObject *args)
7171
PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res);
7272
return NULL;
7373
}
74-
75-
length = min(self->outStream.size, bufsize);
74+
75+
length = min(self->outStream.size, (size_t)bufsize);
7676
result = PyString_FromStringAndSize((const char *)self->outStream.data, length);
7777
if (result != NULL) {
7878
MemoryOutStreamDiscard(&self->outStream, length);
7979
}
80-
80+
8181
return result;
8282
}
8383

84-
static char
84+
static char
8585
doc_comp_flush[] = \
8686
"flush() -- Finishes the compression and returns any remaining compressed data.";
8787

@@ -90,7 +90,7 @@ pylzma_comp_flush(CCompressionObject *self, PyObject *args)
9090
{
9191
PyObject *result;
9292
int res;
93-
93+
9494
Py_BEGIN_ALLOW_THREADS
9595
while (1) {
9696
res = LzmaEnc_CodeOneBlock(self->encoder, False, 0, 0);
@@ -103,13 +103,13 @@ pylzma_comp_flush(CCompressionObject *self, PyObject *args)
103103
PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res);
104104
return NULL;
105105
}
106-
106+
107107
LzmaEnc_Finish(self->encoder);
108108
result = PyString_FromStringAndSize((const char *) self->outStream.data, self->outStream.size);
109109
if (result != NULL) {
110110
MemoryOutStreamDiscard(&self->outStream, self->outStream.size);
111111
}
112-
112+
113113
return result;
114114
}
115115

@@ -140,7 +140,7 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs)
140140
Byte header[LZMA_PROPS_SIZE];
141141
size_t headerSize = LZMA_PROPS_SIZE;
142142
int result=-1;
143-
143+
144144
// possible keywords for this function
145145
static char *kwlist[] = {"dictionary", "fastBytes", "literalContextBits",
146146
"literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL};
@@ -154,11 +154,11 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs)
154154
char *matchfinder = NULL; // matchfinder algorithm
155155
int algorithm = 2;
156156
int res;
157-
157+
158158
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiiiis", kwlist, &dictionary, &fastBytes,
159159
&literalContextBits, &literalPosBits, &posBits, &algorithm, &eos, &multithreading, &matchfinder))
160160
return -1;
161-
161+
162162
CHECK_RANGE(dictionary, 0, 28, "dictionary must be between 0 and 28");
163163
CHECK_RANGE(fastBytes, 5, 255, "fastBytes must be between 5 and 255");
164164
CHECK_RANGE(literalContextBits, 0, 8, "literalContextBits must be between 0 and 8");
@@ -172,15 +172,15 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs)
172172
PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored");
173173
#endif
174174
}
175-
175+
176176
self->encoder = LzmaEnc_Create(&allocator);
177177
if (self->encoder == NULL) {
178178
PyErr_NoMemory();
179179
return -1;
180180
}
181-
181+
182182
LzmaEncProps_Init(&props);
183-
183+
184184
props.dictSize = 1 << dictionary;
185185
props.lc = literalContextBits;
186186
props.lp = literalPosBits;
@@ -207,10 +207,10 @@ pylzma_comp_init(CCompressionObject *self, PyObject *args, PyObject *kwargs)
207207
PyErr_SetString(PyExc_TypeError, "could not generate stream header");
208208
goto exit;
209209
}
210-
210+
211211
LzmaEnc_Prepare(self->encoder, &self->inStream.s, &self->outStream.s, &allocator, &allocator);
212212
result = 0;
213-
213+
214214
exit:
215215
return result;
216216
}

src/pylzma/pylzma_decompress.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
* modify it under the terms of the GNU Lesser General Public
1010
* License as published by the Free Software Foundation; either
1111
* version 2.1 of the License, or (at your option) any later version.
12-
*
12+
*
1313
* This library is distributed in the hope that it will be useful,
1414
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1616
* Lesser General Public License for more details.
17-
*
17+
*
1818
* You should have received a copy of the GNU Lesser General Public
1919
* License along with this library; if not, write to the Free Software
2020
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21-
*
21+
*
2222
* $Id$
2323
*
2424
*/
@@ -45,7 +45,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
4545
Byte *tmp;
4646
PARSE_LENGTH_TYPE length;
4747
int bufsize=BLOCK_SIZE;
48-
PY_LONG_LONG totallength=-1;
48+
Py_ssize_t totallength=-1;
4949
int lzma2 = 0;
5050
PARSE_LENGTH_TYPE avail;
5151
PyObject *result=NULL;
@@ -60,8 +60,8 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
6060
int propertiesLength;
6161
// possible keywords for this function
6262
static char *kwlist[] = {"data", "bufsize", "maxlength", "lzma2", NULL};
63-
64-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iLi", kwlist, &data, &length, &bufsize, &totallength, &lzma2))
63+
64+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|ini", kwlist, &data, &length, &bufsize, &totallength, &lzma2))
6565
return NULL;
6666

6767
propertiesLength = lzma2 ? 1 : LZMA_PROPS_SIZE;
@@ -72,10 +72,10 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
7272
if (result == NULL) {
7373
return NULL;
7474
}
75-
75+
7676
tmp = (Byte *) PyBytes_AS_STRING(result);
7777
srcLen = length - propertiesLength;
78-
destLen = totallength;
78+
destLen = (size_t)totallength;
7979
Py_BEGIN_ALLOW_THREADS
8080
if (lzma2) {
8181
res = Lzma2Decode(tmp, &destLen, (Byte *) (data + propertiesLength), &srcLen, data[0], LZMA_FINISH_ANY, &status, &allocator);
@@ -92,7 +92,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
9292
}
9393
return result;
9494
}
95-
95+
9696
CreateMemoryOutStream(&outStream);
9797
tmp = (Byte *) malloc(bufsize);
9898
if (tmp == NULL) {
@@ -126,7 +126,7 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
126126
for (;;) {
127127
srcLen = avail;
128128
destLen = bufsize;
129-
129+
130130
if (lzma2) {
131131
res = Lzma2Dec_DecodeToBuf(&state.lzma2, tmp, &destLen, data, &srcLen, LZMA_FINISH_ANY, &status);
132132
} else {
@@ -140,18 +140,18 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
140140
if (res != SZ_OK || status == LZMA_STATUS_FINISHED_WITH_MARK || status == LZMA_STATUS_NEEDS_MORE_INPUT) {
141141
break;
142142
}
143-
143+
144144
}
145145
Py_END_ALLOW_THREADS
146-
146+
147147
if (status == LZMA_STATUS_NEEDS_MORE_INPUT) {
148148
PyErr_SetString(PyExc_ValueError, "data error during decompression");
149149
} else if (res != SZ_OK) {
150150
PyErr_Format(PyExc_TypeError, "Error while decompressing: %d", res);
151151
} else {
152152
result = PyBytes_FromStringAndSize((const char *) outStream.data, outStream.size);
153153
}
154-
154+
155155
exit:
156156
if (outStream.data != NULL) {
157157
free(outStream.data);
@@ -162,6 +162,6 @@ pylzma_decompress(PyObject *self, PyObject *args, PyObject *kwargs)
162162
LzmaDec_Free(&state.lzma, &allocator);
163163
}
164164
free(tmp);
165-
165+
166166
return result;
167167
}

src/pylzma/pylzma_decompress_compat.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
* modify it under the terms of the GNU Lesser General Public
1010
* License as published by the Free Software Foundation; either
1111
* version 2.1 of the License, or (at your option) any later version.
12-
*
12+
*
1313
* This library is distributed in the hope that it will be useful,
1414
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1515
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1616
* Lesser General Public License for more details.
17-
*
17+
*
1818
* You should have received a copy of the GNU Lesser General Public
1919
* License along with this library; if not, write to the Free Software
2020
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21-
*
21+
*
2222
* $Id$
2323
*
2424
*/
@@ -33,7 +33,7 @@ void free_lzma_stream(lzma_stream *stream)
3333
if (stream->dynamicData)
3434
lzmafree(stream->dynamicData);
3535
stream->dynamicData = NULL;
36-
36+
3737
if (stream->dictionary)
3838
lzmafree(stream->dictionary);
3939
stream->dictionary = NULL;
@@ -47,35 +47,40 @@ PyObject *pylzma_decompress_compat(PyObject *self, PyObject *args)
4747
{
4848
char *data;
4949
PARSE_LENGTH_TYPE length;
50-
PY_LONG_LONG blocksize=BLOCK_SIZE;
50+
Py_ssize_t blocksize=BLOCK_SIZE;
5151
PyObject *result = NULL;
5252
lzma_stream stream;
5353
int res;
5454
char *output;
55-
56-
if (!PyArg_ParseTuple(args, "s#|L", &data, &length, &blocksize))
55+
56+
if (!PyArg_ParseTuple(args, "s#|n", &data, &length, &blocksize))
57+
return NULL;
58+
59+
if (blocksize <= 0) {
60+
PyErr_SetString(PyExc_ValueError, "bufsize must be greater than zero");
5761
return NULL;
58-
62+
}
63+
5964
memset(&stream, 0, sizeof(stream));
6065
if (!(output = (char *)malloc(blocksize)))
6166
{
6267
PyErr_NoMemory();
6368
goto exit;
6469
}
65-
70+
6671
lzmaCompatInit(&stream);
6772
stream.next_in = (Byte *)data;
6873
stream.avail_in = length;
6974
stream.next_out = (Byte *)output;
7075
stream.avail_out = blocksize;
71-
76+
7277
// decompress data
7378
while (1)
7479
{
7580
Py_BEGIN_ALLOW_THREADS
7681
res = lzmaCompatDecode(&stream);
7782
Py_END_ALLOW_THREADS
78-
83+
7984
if (res == LZMA_STREAM_END) {
8085
break;
8186
} else if (res == LZMA_NOT_ENOUGH_MEM) {
@@ -98,19 +103,19 @@ PyObject *pylzma_decompress_compat(PyObject *self, PyObject *args)
98103
PyErr_Format(PyExc_ValueError, "unknown return code from lzmaDecode: %d", res);
99104
goto exit;
100105
}
101-
106+
102107
// if we exit here, decompression finished without returning LZMA_STREAM_END
103108
// XXX: why is this sometimes?
104109
if (stream.avail_in == 0)
105110
break;
106111
}
107112

108113
result = PyBytes_FromStringAndSize(output, stream.totalOut);
109-
114+
110115
exit:
111116
free_lzma_stream(&stream);
112117
if (output != NULL)
113118
free(output);
114-
119+
115120
return result;
116121
}

0 commit comments

Comments
 (0)