Skip to content

Commit 3da7418

Browse files
authored
Merge pull request #89 from fancycode/sign-compare
Fix different signedness comparisons.
2 parents 2a5331c + 617e57e commit 3da7418

14 files changed

+234
-223
lines changed

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,18 @@ def build_extension(self, ext):
129129
if isinstance(self.compiler, MSVCCompiler) or getattr(self.compiler, 'compiler_type', '') == 'msvc':
130130
# set flags only available when using MSVC
131131
ext.extra_link_args.append('/MANIFEST')
132+
# conversion from '__int64' to 'Py_ssize_t', possible loss of data
133+
ext.extra_compile_args.append('/we4244')
134+
# conversion from 'size_t' to 'int', possible loss of data
135+
ext.extra_compile_args.append('/we4267')
132136
if COMPILE_DEBUG:
133137
ext.extra_compile_args.append('/Zi')
134138
ext.extra_compile_args.append('/MTd')
135139
ext.extra_link_args.append('/DEBUG')
136140
else:
137141
ext.extra_compile_args.append('/MT')
142+
else:
143+
ext.extra_compile_args.append('-Werror=sign-compare')
138144

139145
_build_ext.build_extension(self, ext)
140146

src/compat/LzmaCompatDecode.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s)
140140
{
141141
UInt32 bound;
142142
UInt32 pos;
143-
143+
144144
/* restore decoder state */
145145
lzma_stream _s = *s;
146146

@@ -240,7 +240,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s)
240240
while (numProbs--)
241241
p[numProbs] = kBitModelTotal >> 1;
242242

243-
243+
244244
//for (i = 0, newDictionarySize = 0; i < 4; i++)
245245
for (i = 0, _s.temp3 = 0; i < 4; i++)
246246
{
@@ -306,7 +306,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s)
306306
}
307307
}
308308
while (symbol < 0x100);
309-
previousByte = symbol;
309+
previousByte = (Byte)symbol;
310310
}
311311
isPreviousMatch = 0;
312312
}
@@ -319,7 +319,7 @@ int LZMACALL lzmaCompatDecode(lzma_stream *s)
319319
RC_GET_BIT(LZMA_C_LITD, prob, symbol)
320320
}
321321
while (symbol < 0x100);
322-
previousByte = symbol;
322+
previousByte = (Byte)symbol;
323323
}
324324
NEED_OUT(LZMA_C_OUTPUT_1);
325325
PUT_BYTE(previousByte);

src/pylzma/pylzma.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static PyObject *
6363
pylzma_calculate_key(PyObject *self, PyObject *args, PyObject *kwargs)
6464
{
6565
char *password;
66-
PARSE_LENGTH_TYPE pwlen;
66+
Py_ssize_t pwlen;
6767
int cycles;
6868
PyObject *pysalt=NULL;
6969
char *salt;
@@ -136,7 +136,7 @@ static PyObject *
136136
pylzma_bcj_x86_convert(PyObject *self, PyObject *args)
137137
{
138138
char *data;
139-
PARSE_LENGTH_TYPE length;
139+
Py_ssize_t length;
140140
int encoding=0;
141141
PyObject *result;
142142

@@ -169,7 +169,7 @@ static PyObject * \
169169
pylzma_bcj_##id##_convert(PyObject *self, PyObject *args) \
170170
{ \
171171
char *data; \
172-
PARSE_LENGTH_TYPE length; \
172+
Py_ssize_t length; \
173173
int encoding=0; \
174174
PyObject *result; \
175175
\
@@ -205,8 +205,8 @@ static PyObject *
205205
pylzma_bcj2_decode(PyObject *self, PyObject *args)
206206
{
207207
char *main_data, *call_data, *jump_data, *rc_data;
208-
PARSE_LENGTH_TYPE main_length, call_length, jump_length, rc_length;
209-
PARSE_LENGTH_TYPE dest_len = -1;
208+
Py_ssize_t main_length, call_length, jump_length, rc_length;
209+
Py_ssize_t dest_len = -1;
210210
CBcj2Dec dec;
211211
SRes res;
212212
PyObject *result;
@@ -280,7 +280,7 @@ static PyObject *
280280
pylzma_delta_decode(PyObject *self, PyObject *args)
281281
{
282282
char *data;
283-
PARSE_LENGTH_TYPE length;
283+
Py_ssize_t length;
284284
unsigned int delta;
285285
Byte state[DELTA_STATE_SIZE];
286286
Byte *tmp;
@@ -320,7 +320,7 @@ static PyObject *
320320
pylzma_delta_encode(PyObject *self, PyObject *args)
321321
{
322322
char *data;
323-
PARSE_LENGTH_TYPE length;
323+
Py_ssize_t length;
324324
unsigned int delta;
325325
Byte state[DELTA_STATE_SIZE];
326326
Byte *tmp;
@@ -395,9 +395,9 @@ static PyObject *
395395
pylzma_ppmd_decompress(PyObject *self, PyObject *args)
396396
{
397397
char *data;
398-
PARSE_LENGTH_TYPE length;
398+
Py_ssize_t length;
399399
char *props;
400-
PARSE_LENGTH_TYPE propssize;
400+
Py_ssize_t propssize;
401401
unsigned int outsize;
402402
PyObject *result;
403403
Byte *tmp;
@@ -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.h

Lines changed: 3 additions & 11 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
*/
@@ -96,14 +96,6 @@ PyInterpreterState* _pylzma_interpreterState;
9696
0,
9797
#endif
9898

99-
#if defined(PY_SSIZE_T_CLEAN) && (PY_VERSION_HEX >= 0x02040000)
100-
#define PARSE_LENGTH_TYPE Py_ssize_t
101-
#define PARSE_LENGTH_FORMAT "%zd"
102-
#else
103-
#define PARSE_LENGTH_TYPE int
104-
#define PARSE_LENGTH_FORMAT "%d"
105-
#endif
106-
10799
// Handle tp_print -> tp_vectorcall_offset change in Python 3.8+
108100
#if PY_VERSION_HEX >= 0x03080000
109101
#define PYLZMA_TP_PRINT 0

src/pylzma/pylzma_aes.c

Lines changed: 17 additions & 17 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
*/
@@ -43,16 +43,16 @@ int
4343
aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs)
4444
{
4545
char *key=NULL;
46-
PARSE_LENGTH_TYPE keylength=0;
46+
Py_ssize_t keylength=0;
4747
char *iv=NULL;
48-
PARSE_LENGTH_TYPE ivlength=0;
48+
Py_ssize_t ivlength=0;
4949
int offset;
50-
50+
5151
// possible keywords for this function
5252
static char *kwlist[] = {"key", "iv", NULL};
5353
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#s#", kwlist, &key, &keylength, &iv, &ivlength))
5454
return -1;
55-
55+
5656
memset(&self->aesBuf, 0, sizeof(self->aesBuf));
5757
self->aes = (UInt32 *) self->aesBuf;
5858
// AES code expects aligned memory
@@ -64,15 +64,15 @@ aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs)
6464

6565
if (keylength > 0) {
6666
if (keylength != 16 && keylength != 24 && keylength != 32) {
67-
PyErr_Format(PyExc_TypeError, "key must be 16, 24 or 32 bytes, got " PARSE_LENGTH_FORMAT, keylength);
67+
PyErr_Format(PyExc_TypeError, "key must be 16, 24 or 32 bytes, got %zd", keylength);
6868
return -1;
6969
}
7070

71-
Aes_SetKey_Dec(self->aes + 4, (Byte *) key, keylength);
71+
Aes_SetKey_Dec(self->aes + 4, (Byte *) key, (unsigned)keylength);
7272
}
7373
if (ivlength > 0) {
7474
if (ivlength != AES_BLOCK_SIZE) {
75-
PyErr_Format(PyExc_TypeError, "iv must be %d bytes, got " PARSE_LENGTH_FORMAT, AES_BLOCK_SIZE, ivlength);
75+
PyErr_Format(PyExc_TypeError, "iv must be %d bytes, got %zd", AES_BLOCK_SIZE, ivlength);
7676
return -1;
7777
}
7878

@@ -81,28 +81,28 @@ aesdecrypt_init(CAESDecryptObject *self, PyObject *args, PyObject *kwargs)
8181
return 0;
8282
}
8383

84-
static char
84+
static char
8585
doc_aesdecrypt_decrypt[] = \
8686
"decrypt(data) -- Decrypt given data.";
8787

8888
static PyObject *
8989
aesdecrypt_decrypt(CAESDecryptObject *self, PyObject *args)
9090
{
9191
char *data;
92-
PARSE_LENGTH_TYPE length;
92+
Py_ssize_t length;
9393
PyObject *result;
9494
char *out;
95-
PARSE_LENGTH_TYPE outlength;
95+
Py_ssize_t outlength;
9696
char *tmpdata = NULL;
97-
97+
9898
if (!PyArg_ParseTuple(args, "s#", &data, &length))
9999
return NULL;
100-
100+
101101
if (length % AES_BLOCK_SIZE) {
102-
PyErr_Format(PyExc_TypeError, "data must be a multiple of %d bytes, got " PARSE_LENGTH_FORMAT, AES_BLOCK_SIZE, length);
102+
PyErr_Format(PyExc_TypeError, "data must be a multiple of %d bytes, got %zd", AES_BLOCK_SIZE, length);
103103
return NULL;
104104
}
105-
105+
106106
result = PyBytes_FromStringAndSize(NULL, length);
107107
if (result == NULL) {
108108
return NULL;

src/pylzma/pylzma_compress.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
*/
@@ -44,7 +44,7 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs)
4444
CMemoryInStream inStream;
4545
Byte header[LZMA_PROPS_SIZE];
4646
size_t headerSize = LZMA_PROPS_SIZE;
47-
int res;
47+
int res;
4848
// possible keywords for this function
4949
static char *kwlist[] = {"data", "dictionary", "fastBytes", "literalContextBits",
5050
"literalPosBits", "posBits", "algorithm", "eos", "multithreading", "matchfinder", NULL};
@@ -58,37 +58,37 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs)
5858
char *matchfinder = NULL; // matchfinder algorithm
5959
int algorithm = 2;
6060
char *data;
61-
PARSE_LENGTH_TYPE length;
61+
Py_ssize_t length;
6262

6363
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iiiiiiiis", kwlist, &data, &length, &dictionary, &fastBytes,
6464
&literalContextBits, &literalPosBits, &posBits, &algorithm, &eos, &multithreading, &matchfinder))
6565
return NULL;
66-
66+
6767
outStream.data = NULL;
6868
CHECK_RANGE(dictionary, 0, 27, "dictionary must be between 0 and 27");
6969
CHECK_RANGE(fastBytes, 5, 273, "fastBytes must be between 5 and 273");
7070
CHECK_RANGE(literalContextBits, 0, 8, "literalContextBits must be between 0 and 8");
7171
CHECK_RANGE(literalPosBits, 0, 4, "literalPosBits must be between 0 and 4");
7272
CHECK_RANGE(posBits, 0, 4, "posBits must be between 0 and 4");
7373
CHECK_RANGE(algorithm, 0, 2, "algorithm must be between 0 and 2");
74-
74+
7575
if (matchfinder != NULL) {
7676
#if (PY_VERSION_HEX >= 0x02050000)
7777
PyErr_WarnEx(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored", 1);
7878
#else
7979
PyErr_Warn(PyExc_DeprecationWarning, "matchfinder selection is deprecated and will be ignored");
8080
#endif
8181
}
82-
82+
8383
encoder = LzmaEnc_Create(&allocator);
8484
if (encoder == NULL)
8585
return PyErr_NoMemory();
86-
86+
8787
CreateMemoryInStream(&inStream, (Byte *) data, length);
8888
CreateMemoryOutStream(&outStream);
89-
89+
9090
LzmaEncProps_Init(&props);
91-
91+
9292
props.dictSize = 1 << dictionary;
9393
props.lc = literalContextBits;
9494
props.lp = literalPosBits;
@@ -119,16 +119,16 @@ pylzma_compress(PyObject *self, PyObject *args, PyObject *kwargs)
119119
PyErr_Format(PyExc_TypeError, "Error during compressing: %d", res);
120120
goto exit;
121121
}
122-
122+
123123
result = PyBytes_FromStringAndSize((const char *) outStream.data, outStream.size);
124-
124+
125125
exit:
126126
if (encoder != NULL) {
127127
LzmaEnc_Destroy(encoder, &allocator, &allocator);
128128
}
129129
if (outStream.data != NULL) {
130130
free(outStream.data);
131131
}
132-
132+
133133
return result;
134134
}

0 commit comments

Comments
 (0)