Skip to content

Commit 4ea9d15

Browse files
[3.12] gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject (GH-116599) (#116648)
gh-110819: Fix ‘kind’ may be used uninitialized warning in `longobject` (GH-116599) (cherry picked from commit eb947cd) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent d4e8cbb commit 4ea9d15

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
@@ -1766,7 +1766,9 @@ long_to_decimal_string_internal(PyObject *aa,
17661766
digit *pout, *pin, rem, tenpow;
17671767
int negative;
17681768
int d;
1769-
int kind;
1769+
1770+
// writer or bytes_writer can be used, but not both at the same time.
1771+
assert(writer == NULL || bytes_writer == NULL);
17701772

17711773
a = (PyLongObject *)aa;
17721774
if (a == NULL || !PyLong_Check(a)) {
@@ -1879,7 +1881,6 @@ long_to_decimal_string_internal(PyObject *aa,
18791881
Py_DECREF(scratch);
18801882
return -1;
18811883
}
1882-
kind = writer->kind;
18831884
}
18841885
else if (bytes_writer) {
18851886
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
@@ -1894,7 +1895,6 @@ long_to_decimal_string_internal(PyObject *aa,
18941895
Py_DECREF(scratch);
18951896
return -1;
18961897
}
1897-
kind = PyUnicode_KIND(str);
18981898
}
18991899

19001900
#define WRITE_DIGITS(p) \
@@ -1942,19 +1942,23 @@ long_to_decimal_string_internal(PyObject *aa,
19421942
WRITE_DIGITS(p);
19431943
assert(p == *bytes_str);
19441944
}
1945-
else if (kind == PyUnicode_1BYTE_KIND) {
1946-
Py_UCS1 *p;
1947-
WRITE_UNICODE_DIGITS(Py_UCS1);
1948-
}
1949-
else if (kind == PyUnicode_2BYTE_KIND) {
1950-
Py_UCS2 *p;
1951-
WRITE_UNICODE_DIGITS(Py_UCS2);
1952-
}
19531945
else {
1954-
Py_UCS4 *p;
1955-
assert (kind == PyUnicode_4BYTE_KIND);
1956-
WRITE_UNICODE_DIGITS(Py_UCS4);
1946+
int kind = writer ? writer->kind : PyUnicode_KIND(str);
1947+
if (kind == PyUnicode_1BYTE_KIND) {
1948+
Py_UCS1 *p;
1949+
WRITE_UNICODE_DIGITS(Py_UCS1);
1950+
}
1951+
else if (kind == PyUnicode_2BYTE_KIND) {
1952+
Py_UCS2 *p;
1953+
WRITE_UNICODE_DIGITS(Py_UCS2);
1954+
}
1955+
else {
1956+
assert (kind == PyUnicode_4BYTE_KIND);
1957+
Py_UCS4 *p;
1958+
WRITE_UNICODE_DIGITS(Py_UCS4);
1959+
}
19571960
}
1961+
19581962
#undef WRITE_DIGITS
19591963
#undef WRITE_UNICODE_DIGITS
19601964

@@ -1995,11 +1999,12 @@ long_format_binary(PyObject *aa, int base, int alternate,
19951999
PyObject *v = NULL;
19962000
Py_ssize_t sz;
19972001
Py_ssize_t size_a;
1998-
int kind;
19992002
int negative;
20002003
int bits;
20012004

20022005
assert(base == 2 || base == 8 || base == 16);
2006+
// writer or bytes_writer can be used, but not both at the same time.
2007+
assert(writer == NULL || bytes_writer == NULL);
20032008
if (a == NULL || !PyLong_Check(a)) {
20042009
PyErr_BadInternalCall();
20052010
return -1;
@@ -2047,7 +2052,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
20472052
if (writer) {
20482053
if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
20492054
return -1;
2050-
kind = writer->kind;
20512055
}
20522056
else if (bytes_writer) {
20532057
*bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
@@ -2058,7 +2062,6 @@ long_format_binary(PyObject *aa, int base, int alternate,
20582062
v = PyUnicode_New(sz, 'x');
20592063
if (v == NULL)
20602064
return -1;
2061-
kind = PyUnicode_KIND(v);
20622065
}
20632066

20642067
#define WRITE_DIGITS(p) \
@@ -2119,19 +2122,23 @@ long_format_binary(PyObject *aa, int base, int alternate,
21192122
WRITE_DIGITS(p);
21202123
assert(p == *bytes_str);
21212124
}
2122-
else if (kind == PyUnicode_1BYTE_KIND) {
2123-
Py_UCS1 *p;
2124-
WRITE_UNICODE_DIGITS(Py_UCS1);
2125-
}
2126-
else if (kind == PyUnicode_2BYTE_KIND) {
2127-
Py_UCS2 *p;
2128-
WRITE_UNICODE_DIGITS(Py_UCS2);
2129-
}
21302125
else {
2131-
Py_UCS4 *p;
2132-
assert (kind == PyUnicode_4BYTE_KIND);
2133-
WRITE_UNICODE_DIGITS(Py_UCS4);
2126+
int kind = writer ? writer->kind : PyUnicode_KIND(v);
2127+
if (kind == PyUnicode_1BYTE_KIND) {
2128+
Py_UCS1 *p;
2129+
WRITE_UNICODE_DIGITS(Py_UCS1);
2130+
}
2131+
else if (kind == PyUnicode_2BYTE_KIND) {
2132+
Py_UCS2 *p;
2133+
WRITE_UNICODE_DIGITS(Py_UCS2);
2134+
}
2135+
else {
2136+
assert (kind == PyUnicode_4BYTE_KIND);
2137+
Py_UCS4 *p;
2138+
WRITE_UNICODE_DIGITS(Py_UCS4);
2139+
}
21342140
}
2141+
21352142
#undef WRITE_DIGITS
21362143
#undef WRITE_UNICODE_DIGITS
21372144

0 commit comments

Comments
 (0)