summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2024-03-12 10:50:06 (GMT)
committerGitHub <noreply@github.com>2024-03-12 10:50:06 (GMT)
commiteb947cdc1374842a32fa82249ba3c688abf252dc (patch)
tree17f0d50ad357891037ec4d4bf6b16273838daec9
parentf8147d01da44da2434496d868c86c2785f7244cd (diff)
downloadcpython-eb947cdc1374842a32fa82249ba3c688abf252dc.zip
cpython-eb947cdc1374842a32fa82249ba3c688abf252dc.tar.gz
cpython-eb947cdc1374842a32fa82249ba3c688abf252dc.tar.bz2
gh-110819: Fix ‘kind’ may be used uninitialized warning in `longobject` (#116599)
-rw-r--r--Objects/longobject.c63
1 files changed, 35 insertions, 28 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 2d1c6ad..cc2fe11 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -1965,7 +1965,9 @@ long_to_decimal_string_internal(PyObject *aa,
digit *pout, *pin, rem, tenpow;
int negative;
int d;
- int kind;
+
+ // writer or bytes_writer can be used, but not both at the same time.
+ assert(writer == NULL || bytes_writer == NULL);
a = (PyLongObject *)aa;
if (a == NULL || !PyLong_Check(a)) {
@@ -2078,7 +2080,6 @@ long_to_decimal_string_internal(PyObject *aa,
Py_DECREF(scratch);
return -1;
}
- kind = writer->kind;
}
else if (bytes_writer) {
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
@@ -2093,7 +2094,6 @@ long_to_decimal_string_internal(PyObject *aa,
Py_DECREF(scratch);
return -1;
}
- kind = PyUnicode_KIND(str);
}
#define WRITE_DIGITS(p) \
@@ -2141,19 +2141,23 @@ long_to_decimal_string_internal(PyObject *aa,
WRITE_DIGITS(p);
assert(p == *bytes_str);
}
- else if (kind == PyUnicode_1BYTE_KIND) {
- Py_UCS1 *p;
- WRITE_UNICODE_DIGITS(Py_UCS1);
- }
- else if (kind == PyUnicode_2BYTE_KIND) {
- Py_UCS2 *p;
- WRITE_UNICODE_DIGITS(Py_UCS2);
- }
else {
- Py_UCS4 *p;
- assert (kind == PyUnicode_4BYTE_KIND);
- WRITE_UNICODE_DIGITS(Py_UCS4);
+ int kind = writer ? writer->kind : PyUnicode_KIND(str);
+ if (kind == PyUnicode_1BYTE_KIND) {
+ Py_UCS1 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS1);
+ }
+ else if (kind == PyUnicode_2BYTE_KIND) {
+ Py_UCS2 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS2);
+ }
+ else {
+ assert (kind == PyUnicode_4BYTE_KIND);
+ Py_UCS4 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS4);
+ }
}
+
#undef WRITE_DIGITS
#undef WRITE_UNICODE_DIGITS
@@ -2194,11 +2198,12 @@ long_format_binary(PyObject *aa, int base, int alternate,
PyObject *v = NULL;
Py_ssize_t sz;
Py_ssize_t size_a;
- int kind;
int negative;
int bits;
assert(base == 2 || base == 8 || base == 16);
+ // writer or bytes_writer can be used, but not both at the same time.
+ assert(writer == NULL || bytes_writer == NULL);
if (a == NULL || !PyLong_Check(a)) {
PyErr_BadInternalCall();
return -1;
@@ -2246,7 +2251,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
if (writer) {
if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
return -1;
- kind = writer->kind;
}
else if (bytes_writer) {
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
@@ -2257,7 +2261,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
v = PyUnicode_New(sz, 'x');
if (v == NULL)
return -1;
- kind = PyUnicode_KIND(v);
}
#define WRITE_DIGITS(p) \
@@ -2318,19 +2321,23 @@ long_format_binary(PyObject *aa, int base, int alternate,
WRITE_DIGITS(p);
assert(p == *bytes_str);
}
- else if (kind == PyUnicode_1BYTE_KIND) {
- Py_UCS1 *p;
- WRITE_UNICODE_DIGITS(Py_UCS1);
- }
- else if (kind == PyUnicode_2BYTE_KIND) {
- Py_UCS2 *p;
- WRITE_UNICODE_DIGITS(Py_UCS2);
- }
else {
- Py_UCS4 *p;
- assert (kind == PyUnicode_4BYTE_KIND);
- WRITE_UNICODE_DIGITS(Py_UCS4);
+ int kind = writer ? writer->kind : PyUnicode_KIND(v);
+ if (kind == PyUnicode_1BYTE_KIND) {
+ Py_UCS1 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS1);
+ }
+ else if (kind == PyUnicode_2BYTE_KIND) {
+ Py_UCS2 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS2);
+ }
+ else {
+ assert (kind == PyUnicode_4BYTE_KIND);
+ Py_UCS4 *p;
+ WRITE_UNICODE_DIGITS(Py_UCS4);
+ }
}
+
#undef WRITE_DIGITS
#undef WRITE_UNICODE_DIGITS