Skip to content

Commit e13019b

Browse files
Fix: UNINIT.LOCAL_VAR
Problem: Argument Clinic rendered the adjacent "length" variable of an optional length-bearing string parameter ("s#", "y#", "z#", "u#", "Z#") without an initializer. The parser only writes *<name>_length when the argument is actually passed, so an omitted argument left the variable undefined and the impl function received an arbitrary buffer size (Modules/clinic/_testclinic.c.h:1531, c_length). Solution: Initialize the length variable from the parameter's default value in Tools/clinic/libclinic/converter.py: a new CConverter.length_c_default() hook, UTF-8 byte length for str defaults, 0 for None/NULL; regenerate the affected clinic files and add regression tests for calls with omitted arguments. Signed-off-by: a.burlakov (a.burlakov@fobos-nt.ru)
1 parent 91ab655 commit e13019b

6 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎Lib/test/clinic.test.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,11 +2043,11 @@ test_str_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
20432043
const char *d = "cef";
20442044
const char *e = "gh";
20452045
const char *f = "ij";
2046-
Py_ssize_t f_length;
2046+
Py_ssize_t f_length = 2;
20472047
const char *g = "kl";
2048-
Py_ssize_t g_length;
2048+
Py_ssize_t g_length = 2;
20492049
const char *h = "mn";
2050-
Py_ssize_t h_length;
2050+
Py_ssize_t h_length = 2;
20512051

20522052
if (!PyArg_ParseArray(args, nargs, "|sssyzy#s#z#:test_str_converter",
20532053
&a, &b, &c, &d, &e, &f, &f_length, &g, &g_length, &h, &h_length)) {
@@ -2065,7 +2065,7 @@ test_str_converter_impl(PyObject *module, const char *a, const char *b,
20652065
const char *f, Py_ssize_t f_length, const char *g,
20662066
Py_ssize_t g_length, const char *h,
20672067
Py_ssize_t h_length)
2068-
/*[clinic end generated code: output=58dc0fad26cd4c5d input=8afe9da8185cd38c]*/
2068+
/*[clinic end generated code: output=e251efc38d275ad6 input=8afe9da8185cd38c]*/
20692069

20702070

20712071
/*[clinic input]

‎Lib/test/test_clinic.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,17 @@ def test_param_default_parameters_out_of_order(self):
17111711
"""
17121712
self.expect_failure(block, err, lineno=3)
17131713

1714+
def test_param_optional_str_length_without_c_default(self):
1715+
err = (
1716+
"str_converter: cannot compute the default value for 'a_length'"
1717+
)
1718+
block = """
1719+
module os
1720+
os.access
1721+
a: str(accept={str, robuffer}, zeroes=True, c_default="some_global") = unknown
1722+
"""
1723+
self.expect_failure(block, err, lineno=2)
1724+
17141725
def disabled_test_converter_arguments(self):
17151726
function = self.parse_function("""
17161727
module os
@@ -4909,6 +4920,8 @@ def test_str_converter(self):
49094920
ac_tester.str_converter('a', 'b', 'c')
49104921
with self.assertRaises(ValueError):
49114922
ac_tester.str_converter('a', b'b\0b', 'c')
4923+
self.assertEqual(ac_tester.str_converter(), ('a', 'b', 'c'))
4924+
self.assertEqual(ac_tester.str_converter('a', b'b'), ('a', 'b', 'c'))
49124925
self.assertEqual(ac_tester.str_converter('a', b'b', 'c'), ('a', 'b', 'c'))
49134926
self.assertEqual(ac_tester.str_converter('a', b'b', b'c'), ('a', 'b', 'c'))
49144927
self.assertEqual(ac_tester.str_converter('a', b'b', 'c\0c'), ('a', 'b', 'c\0c'))

‎Modules/clinic/_testclinic.c.h‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/clinic/_testclinic_depr.c.h‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Tools/clinic/libclinic/converter.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ def converter_init(self) -> None:
286286
def c_default_init(self) -> None:
287287
return
288288

289+
def length_c_default(self) -> str | None:
290+
"""
291+
The C expression the associated "length" variable is initialized
292+
with, or None if the converter cannot compute it.
293+
"""
294+
return None
295+
289296
# An alternative name of a preceding parameter: they share
290297
# the same C variable.
291298
alias_of: Parameter | None = None
@@ -445,7 +452,17 @@ def declaration(self, *, in_parser: bool = False) -> str:
445452
declaration.append(";")
446453
if self.length:
447454
declaration.append('\n')
448-
declaration.append(f"Py_ssize_t {self.length_name};")
455+
declaration.append(f"Py_ssize_t {self.length_name}")
456+
# The parser only writes the length when the argument is
457+
# passed, so an optional parameter needs its length
458+
# initialized from the default value.
459+
if self.is_optional():
460+
length_c_default = self.length_c_default()
461+
if length_c_default is None:
462+
fail(f"{self.__class__.__name__}: cannot compute "
463+
f"the default value for {self.length_name!r}")
464+
declaration.append(f" = {length_c_default}")
465+
declaration.append(";")
449466
return "".join(declaration)
450467

451468
def initialize(self) -> str:

‎Tools/clinic/libclinic/converters.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,18 @@ def c_default_init(self) -> None:
828828
if self.default is None:
829829
self.c_default = "NULL"
830830

831+
def length_c_default(self) -> str | None:
832+
# The byte length of the default value; the parser only writes
833+
# it when the argument is passed.
834+
default = self.default
835+
if default is NULL or default is None:
836+
return "0"
837+
elif isinstance(default, str):
838+
return str(len(default.encode('utf-8')))
839+
elif isinstance(default, bytes):
840+
return str(len(default))
841+
return None
842+
831843
def post_parsing(self) -> str:
832844
if self.encoding:
833845
name = self.name
@@ -1065,6 +1077,10 @@ def converter_init(
10651077
fail(f"Py_UNICODE_converter: illegal 'accept' argument {accept!r}")
10661078
self.c_default = "NULL"
10671079

1080+
def length_c_default(self) -> str | None:
1081+
# The C default value is always NULL.
1082+
return "0"
1083+
10681084
def cleanup(self) -> str:
10691085
if self.length:
10701086
return ""

0 commit comments

Comments
 (0)