Skip to content

Commit f3a2b62

Browse files
authored
[3.11] gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject (GH-116599) (#116650)
(cherry picked from commit eb947cd)
1 parent b17d347 commit f3a2b62

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
@@ -1717,7 +1717,9 @@ long_to_decimal_string_internal(PyObject *aa,
17171717
digit *pout, *pin, rem, tenpow;
17181718
int negative;
17191719
int d;
1720-
enum PyUnicode_Kind kind;
1720+
1721+
// writer or bytes_writer can be used, but not both at the same time.
1722+
assert(writer == NULL || bytes_writer == NULL);
17211723

17221724
a = (PyLongObject *)aa;
17231725
if (a == NULL || !PyLong_Check(a)) {
@@ -1819,7 +1821,6 @@ long_to_decimal_string_internal(PyObject *aa,
18191821
Py_DECREF(scratch);
18201822
return -1;
18211823
}
1822-
kind = writer->kind;
18231824
}
18241825
else if (bytes_writer) {
18251826
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
@@ -1834,7 +1835,6 @@ long_to_decimal_string_internal(PyObject *aa,
18341835
Py_DECREF(scratch);
18351836
return -1;
18361837
}
1837-
kind = PyUnicode_KIND(str);
18381838
}
18391839

18401840
#define WRITE_DIGITS(p) \
@@ -1882,19 +1882,23 @@ long_to_decimal_string_internal(PyObject *aa,
18821882
WRITE_DIGITS(p);
18831883
assert(p == *bytes_str);
18841884
}
1885-
else if (kind == PyUnicode_1BYTE_KIND) {
1886-
Py_UCS1 *p;
1887-
WRITE_UNICODE_DIGITS(Py_UCS1);
1888-
}
1889-
else if (kind == PyUnicode_2BYTE_KIND) {
1890-
Py_UCS2 *p;
1891-
WRITE_UNICODE_DIGITS(Py_UCS2);
1892-
}
18931885
else {
1894-
Py_UCS4 *p;
1895-
assert (kind == PyUnicode_4BYTE_KIND);
1896-
WRITE_UNICODE_DIGITS(Py_UCS4);
1886+
enum PyUnicode_Kind kind = writer ? writer->kind : PyUnicode_KIND(str);
1887+
if (kind == PyUnicode_1BYTE_KIND) {
1888+
Py_UCS1 *p;
1889+
WRITE_UNICODE_DIGITS(Py_UCS1);
1890+
}
1891+
else if (kind == PyUnicode_2BYTE_KIND) {
1892+
Py_UCS2 *p;
1893+
WRITE_UNICODE_DIGITS(Py_UCS2);
1894+
}
1895+
else {
1896+
assert (kind == PyUnicode_4BYTE_KIND);
1897+
Py_UCS4 *p;
1898+
WRITE_UNICODE_DIGITS(Py_UCS4);
1899+
}
18971900
}
1901+
18981902
#undef WRITE_DIGITS
18991903
#undef WRITE_UNICODE_DIGITS
19001904

@@ -1935,11 +1939,12 @@ long_format_binary(PyObject *aa, int base, int alternate,
19351939
PyObject *v = NULL;
19361940
Py_ssize_t sz;
19371941
Py_ssize_t size_a;
1938-
enum PyUnicode_Kind kind;
19391942
int negative;
19401943
int bits;
19411944

19421945
assert(base == 2 || base == 8 || base == 16);
1946+
// writer or bytes_writer can be used, but not both at the same time.
1947+
assert(writer == NULL || bytes_writer == NULL);
19431948
if (a == NULL || !PyLong_Check(a)) {
19441949
PyErr_BadInternalCall();
19451950
return -1;
@@ -1987,7 +1992,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
19871992
if (writer) {
19881993
if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
19891994
return -1;
1990-
kind = writer->kind;
19911995
}
19921996
else if (bytes_writer) {
19931997
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
@@ -1998,7 +2002,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
19982002
v = PyUnicode_New(sz, 'x');
19992003
if (v == NULL)
20002004
return -1;
2001-
kind = PyUnicode_KIND(v);
20022005
}
20032006

20042007
#define WRITE_DIGITS(p) \
@@ -2059,19 +2062,23 @@ long_format_binary(PyObject *aa, int base, int alternate,
20592062
WRITE_DIGITS(p);
20602063
assert(p == *bytes_str);
20612064
}
2062-
else if (kind == PyUnicode_1BYTE_KIND) {
2063-
Py_UCS1 *p;
2064-
WRITE_UNICODE_DIGITS(Py_UCS1);
2065-
}
2066-
else if (kind == PyUnicode_2BYTE_KIND) {
2067-
Py_UCS2 *p;
2068-
WRITE_UNICODE_DIGITS(Py_UCS2);
2069-
}
20702065
else {
2071-
Py_UCS4 *p;
2072-
assert (kind == PyUnicode_4BYTE_KIND);
2073-
WRITE_UNICODE_DIGITS(Py_UCS4);
2066+
enum PyUnicode_Kind kind = writer ? writer->kind : PyUnicode_KIND(v);
2067+
if (kind == PyUnicode_1BYTE_KIND) {
2068+
Py_UCS1 *p;
2069+
WRITE_UNICODE_DIGITS(Py_UCS1);
2070+
}
2071+
else if (kind == PyUnicode_2BYTE_KIND) {
2072+
Py_UCS2 *p;
2073+
WRITE_UNICODE_DIGITS(Py_UCS2);
2074+
}
2075+
else {
2076+
assert (kind == PyUnicode_4BYTE_KIND);
2077+
Py_UCS4 *p;
2078+
WRITE_UNICODE_DIGITS(Py_UCS4);
2079+
}
20742080
}
2081+
20752082
#undef WRITE_DIGITS
20762083
#undef WRITE_UNICODE_DIGITS
20772084

0 commit comments

Comments
 (0)