gh-140594: Fix an out of bounds read when feeding NUL byte to PyOS_StdioReadline() (#140910)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Shamil
2026-03-12 13:46:36 +03:00
committed by GitHub
parent 72456309e9
commit 86a0756234
3 changed files with 11 additions and 1 deletions
+8
View File
@@ -200,6 +200,14 @@ class CmdLineTest(unittest.TestCase):
self.assertTrue(data.find(b'1 loop') != -1)
self.assertTrue(data.find(b'__main__.Timer') != -1)
@support.cpython_only
def test_null_byte_in_interactive_mode(self):
# gh-140594: Fix an out of bounds read when a single NUL character
# is read from the standard input in interactive mode.
proc = spawn_python('-i')
proc.communicate(b'\x00', timeout=support.SHORT_TIMEOUT)
self.assertEqual(proc.returncode, 0)
def test_relativedir_bug46421(self):
# Test `python -m unittest` with a relative directory beginning with ./
# Note: We have to switch to the project's top module's directory, as per
@@ -0,0 +1,2 @@
Fix an out of bounds read when a single NUL character is read from the standard input.
Patch by Shamil Abdulaev.
+1 -1
View File
@@ -344,7 +344,7 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
break;
}
n += strlen(p + n);
} while (p[n-1] != '\n');
} while (n == 0 || p[n-1] != '\n');
pr = (char *)PyMem_RawRealloc(p, n+1);
if (pr == NULL) {