Skip to content

Commit 72c5af2

Browse files
committed
bpo-30431: Raise ValueError in case of null in input prompt
If the input prompt to the builtin input function has any null character, then raise ValueError.
1 parent 7c2f82d commit 72c5af2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Lib/test/test_readline.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def test_init(self):
127127
input()
128128
print("History length:", readline.get_current_history_length())
129129
"""
130+
auto_history_script_with_null = """\
131+
import readline
132+
readline.set_auto_history({})
133+
input("Hello \\x00 World: ")
134+
print("History length:", readline.get_current_history_length())
135+
"""
136+
130137

131138
def test_auto_history_enabled(self):
132139
output = run_pty(self.auto_history_script.format(True))
@@ -136,6 +143,10 @@ def test_auto_history_disabled(self):
136143
output = run_pty(self.auto_history_script.format(False))
137144
self.assertIn(b"History length: 0\r\n", output)
138145

146+
def test_prompt_contains_null(self):
147+
output = run_pty(self.auto_history_script_with_null.format(False))
148+
self.assertIn(b"ValueError: input: prompt string cannot contain null characters", output)
149+
139150
def test_nonascii(self):
140151
try:
141152
readline.add_history("\xEB\xEF")

Python/bltinmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,11 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19741974
goto _readline_errors;
19751975
assert(PyBytes_Check(po));
19761976
promptstr = PyBytes_AS_STRING(po);
1977+
if ((Py_ssize_t)strlen(promptstr) != PyBytes_GET_SIZE(po)) {
1978+
PyErr_SetString(PyExc_ValueError,
1979+
"input: prompt string cannot contain null characters");
1980+
goto _readline_errors;
1981+
}
19771982
}
19781983
else {
19791984
po = NULL;

0 commit comments

Comments
 (0)