Skip to content

Commit 12174a5

Browse files
committed
Issue #22156: Fix "comparison between signed and unsigned integers" compiler
warnings in the Objects/ subdirectory. PyType_FromSpecWithBases() and PyType_FromSpec() now reject explicitly negative slot identifiers.
1 parent 98ea54c commit 12174a5

5 files changed

Lines changed: 13 additions & 12 deletions

File tree

Objects/exceptions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ _PyErr_TrySetFromCause(const char *format, ...)
27142714
same_basic_size = (
27152715
caught_type_size == base_exc_size ||
27162716
(PyType_SUPPORTS_WEAKREFS(caught_type) &&
2717-
(caught_type_size == base_exc_size + sizeof(PyObject *))
2717+
(caught_type_size == base_exc_size + (Py_ssize_t)sizeof(PyObject *))
27182718
)
27192719
);
27202720
if (caught_type->tp_init != (initproc)BaseException_init ||

Objects/longobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5094,13 +5094,13 @@ _PyLong_Init(void)
50945094
* to the original refcnt + 1 */
50955095
Py_REFCNT(op) = refcnt + 1;
50965096
assert(Py_SIZE(op) == size);
5097-
assert(v->ob_digit[0] == abs(ival));
5097+
assert(v->ob_digit[0] == (digit)abs(ival));
50985098
}
50995099
else {
51005100
(void)PyObject_INIT(v, &PyLong_Type);
51015101
}
51025102
Py_SIZE(v) = size;
5103-
v->ob_digit[0] = abs(ival);
5103+
v->ob_digit[0] = (digit)abs(ival);
51045104
}
51055105
#endif
51065106
/* initialize int_info */

Objects/setobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ frozenset_hash(PyObject *self)
771771
/* Make the final result spread-out in a different pattern
772772
than the algorithm for tuples or other python objects. */
773773
hash = hash * 69069U + 907133923UL;
774-
if (hash == -1)
774+
if (hash == (Py_uhash_t)-1)
775775
hash = 590923713UL;
776776
so->hash = hash;
777777
return hash;

Objects/typeobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,8 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
26222622
type->tp_itemsize = spec->itemsize;
26232623

26242624
for (slot = spec->slots; slot->slot; slot++) {
2625-
if (slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) {
2625+
if (slot->slot < 0
2626+
|| (size_t)slot->slot >= Py_ARRAY_LENGTH(slotoffsets)) {
26262627
PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
26272628
goto fail;
26282629
}
@@ -2682,11 +2683,11 @@ PyType_FromSpec(PyType_Spec *spec)
26822683
void *
26832684
PyType_GetSlot(PyTypeObject *type, int slot)
26842685
{
2685-
if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
2686+
if (!PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE) || slot < 0) {
26862687
PyErr_BadInternalCall();
26872688
return NULL;
26882689
}
2689-
if (slot >= Py_ARRAY_LENGTH(slotoffsets)) {
2690+
if ((size_t)slot >= Py_ARRAY_LENGTH(slotoffsets)) {
26902691
/* Extension module requesting slot from a future version */
26912692
return NULL;
26922693
}

Objects/unicodeobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,7 +3519,7 @@ PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
35193519
if (locale_error_handler(errors, &surrogateescape) < 0)
35203520
return NULL;
35213521

3522-
if (str[len] != '\0' || len != strlen(str)) {
3522+
if (str[len] != '\0' || (size_t)len != strlen(str)) {
35233523
PyErr_SetString(PyExc_TypeError, "embedded null character");
35243524
return NULL;
35253525
}
@@ -3696,7 +3696,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
36963696
}
36973697
size = PyBytes_GET_SIZE(output);
36983698
data = PyBytes_AS_STRING(output);
3699-
if (size != strlen(data)) {
3699+
if ((size_t)size != strlen(data)) {
37003700
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
37013701
Py_DECREF(output);
37023702
return 0;
@@ -8874,7 +8874,7 @@ PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
88748874

88758875
maxchar = 127;
88768876
for (i = 0; i < length; i++) {
8877-
Py_UNICODE ch = s[i];
8877+
Py_UCS4 ch = s[i];
88788878
if (ch > 127) {
88798879
int decimal = Py_UNICODE_TODECIMAL(ch);
88808880
if (decimal >= 0)
@@ -8891,7 +8891,7 @@ PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
88918891
data = PyUnicode_DATA(decimal);
88928892
/* Iterate over code points */
88938893
for (i = 0; i < length; i++) {
8894-
Py_UNICODE ch = s[i];
8894+
Py_UCS4 ch = s[i];
88958895
if (ch > 127) {
88968896
int decimal = Py_UNICODE_TODECIMAL(ch);
88978897
if (decimal >= 0)
@@ -10833,7 +10833,7 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
1083310833
void *data = PyUnicode_DATA(uni);
1083410834
/* Compare Unicode string and source character set string */
1083510835
for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
10836-
if (chr != str[i])
10836+
if (chr != (unsigned char)str[i])
1083710837
return (chr < (unsigned char)(str[i])) ? -1 : 1;
1083810838
/* This check keeps Python strings that end in '\0' from comparing equal
1083910839
to C strings identical up to that point. */

0 commit comments

Comments
 (0)