Skip to content

Commit eb947cd

Browse files
authored
gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject (#116599)
1 parent f8147d0 commit eb947cd

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

Objects/longobject.c

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,9 @@ long_to_decimal_string_internal(PyObject *aa,
19651965
digit *pout, *pin, rem, tenpow;
19661966
int negative;
19671967
int d;
1968-
int kind;
1968+
1969+
// writer or bytes_writer can be used, but not both at the same time.
1970+
assert(writer == NULL || bytes_writer == NULL);
19691971

19701972
a = (PyLongObject *)aa;
19711973
if (a == NULL || !PyLong_Check(a)) {
@@ -2078,7 +2080,6 @@ long_to_decimal_string_internal(PyObject *aa,
20782080
Py_DECREF(scratch);
20792081
return -1;
20802082
}
2081-
kind = writer->kind;
20822083
}
20832084
else if (bytes_writer) {
20842085
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
@@ -2093,7 +2094,6 @@ long_to_decimal_string_internal(PyObject *aa,
20932094
Py_DECREF(scratch);
20942095
return -1;
20952096
}
2096-
kind = PyUnicode_KIND(str);
20972097
}
20982098

20992099
#define WRITE_DIGITS(p) \
@@ -2141,19 +2141,23 @@ long_to_decimal_string_internal(PyObject *aa,
21412141
WRITE_DIGITS(p);
21422142
assert(p == *bytes_str);
21432143
}
2144-
else if (kind == PyUnicode_1BYTE_KIND) {
2145-
Py_UCS1 *p;
2146-
WRITE_UNICODE_DIGITS(Py_UCS1);
2147-
}
2148-
else if (kind == PyUnicode_2BYTE_KIND) {
2149-
Py_UCS2 *p;
2150-
WRITE_UNICODE_DIGITS(Py_UCS2);
2151-
}
21522144
else {
2153-
Py_UCS4 *p;
2154-
assert (kind == PyUnicode_4BYTE_KIND);
2155-
WRITE_UNICODE_DIGITS(Py_UCS4);
2145+
int kind = writer ? writer->kind : PyUnicode_KIND(str);
2146+
if (kind == PyUnicode_1BYTE_KIND) {
2147+
Py_UCS1 *p;
2148+
WRITE_UNICODE_DIGITS(Py_UCS1);
2149+
}
2150+
else if (kind == PyUnicode_2BYTE_KIND) {
2151+
Py_UCS2 *p;
2152+
WRITE_UNICODE_DIGITS(Py_UCS2);
2153+
}
2154+
else {
2155+
assert (kind == PyUnicode_4BYTE_KIND);
2156+
Py_UCS4 *p;
2157+
WRITE_UNICODE_DIGITS(Py_UCS4);
2158+
}
21562159
}
2160+
21572161
#undef WRITE_DIGITS
21582162
#undef WRITE_UNICODE_DIGITS
21592163

@@ -2194,11 +2198,12 @@ long_format_binary(PyObject *aa, int base, int alternate,
21942198
PyObject *v = NULL;
21952199
Py_ssize_t sz;
21962200
Py_ssize_t size_a;
2197-
int kind;
21982201
int negative;
21992202
int bits;
22002203

22012204
assert(base == 2 || base == 8 || base == 16);
2205+
// writer or bytes_writer can be used, but not both at the same time.
2206+
assert(writer == NULL || bytes_writer == NULL);
22022207
if (a == NULL || !PyLong_Check(a)) {
22032208
PyErr_BadInternalCall();
22042209
return -1;
@@ -2246,7 +2251,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
22462251
if (writer) {
22472252
if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
22482253
return -1;
2249-
kind = writer->kind;
22502254
}
22512255
else if (bytes_writer) {
22522256
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
@@ -2257,7 +2261,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
22572261
v = PyUnicode_New(sz, 'x');
22582262
if (v == NULL)
22592263
return -1;
2260-
kind = PyUnicode_KIND(v);
22612264
}
22622265

22632266
#define WRITE_DIGITS(p) \
@@ -2318,19 +2321,23 @@ long_format_binary(PyObject *aa, int base, int alternate,
23182321
WRITE_DIGITS(p);
23192322
assert(p == *bytes_str);
23202323
}
2321-
else if (kind == PyUnicode_1BYTE_KIND) {
2322-
Py_UCS1 *p;
2323-
WRITE_UNICODE_DIGITS(Py_UCS1);
2324-
}
2325-
else if (kind == PyUnicode_2BYTE_KIND) {
2326-
Py_UCS2 *p;
2327-
WRITE_UNICODE_DIGITS(Py_UCS2);
2328-
}
23292324
else {
2330-
Py_UCS4 *p;
2331-
assert (kind == PyUnicode_4BYTE_KIND);
2332-
WRITE_UNICODE_DIGITS(Py_UCS4);
2325+
int kind = writer ? writer->kind : PyUnicode_KIND(v);
2326+
if (kind == PyUnicode_1BYTE_KIND) {
2327+
Py_UCS1 *p;
2328+
WRITE_UNICODE_DIGITS(Py_UCS1);
2329+
}
2330+
else if (kind == PyUnicode_2BYTE_KIND) {
2331+
Py_UCS2 *p;
2332+
WRITE_UNICODE_DIGITS(Py_UCS2);
2333+
}
2334+
else {
2335+
assert (kind == PyUnicode_4BYTE_KIND);
2336+
Py_UCS4 *p;
2337+
WRITE_UNICODE_DIGITS(Py_UCS4);
2338+
}
23332339
}
2340+
23342341
#undef WRITE_DIGITS
23352342
#undef WRITE_UNICODE_DIGITS
23362343

0 commit comments

Comments
 (0)