Skip to content

Commit 295f214

Browse files
miss-islingtonashm-devpicnixzvstinner
authored
[3.14] gh-140594: Fix an out of bounds read when feeding NUL byte to PyOS_StdioReadline() (GH-140910) (#145852)
gh-140594: Fix an out of bounds read when feeding NUL byte to PyOS_StdioReadline() (GH-140910) (cherry picked from commit 86a0756) Co-authored-by: Shamil <ashm.tech@proton.me> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 59be951 commit 295f214

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_cmd_line.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ def test_run_module_bug1764407(self):
200200
self.assertTrue(data.find(b'1 loop') != -1)
201201
self.assertTrue(data.find(b'__main__.Timer') != -1)
202202

203+
@support.cpython_only
204+
def test_null_byte_in_interactive_mode(self):
205+
# gh-140594: Fix an out of bounds read when a single NUL character
206+
# is read from the standard input in interactive mode.
207+
proc = spawn_python('-i')
208+
proc.communicate(b'\x00', timeout=support.SHORT_TIMEOUT)
209+
self.assertEqual(proc.returncode, 0)
210+
203211
def test_relativedir_bug46421(self):
204212
# Test `python -m unittest` with a relative directory beginning with ./
205213
# Note: We have to switch to the project's top module's directory, as per
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an out of bounds read when a single NUL character is read from the standard input.
2+
Patch by Shamil Abdulaev.

Parser/myreadline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
344344
break;
345345
}
346346
n += strlen(p + n);
347-
} while (p[n-1] != '\n');
347+
} while (n == 0 || p[n-1] != '\n');
348348

349349
pr = (char *)PyMem_RawRealloc(p, n+1);
350350
if (pr == NULL) {

0 commit comments

Comments
 (0)