Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ def test_run_module_bug1764407(self):
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 a buffer overflow when a single NULL character is read
# from standard input in interactive mode. The test ensures that
# feeding a null byte to the interactive prompt does not crash
# the interpreter.
proc = spawn_python('-i')
proc.communicate(b'\x00', timeout=10)
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a buffer overflow when a single NULL character is read from the standard input.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a buffer overflow, "just" a read out of bounds.

Suggested change
Fix a buffer overflow when a single NULL character is read from the standard input.
Fix an out of bounds read when a single NUL character is read from the standard input.

Patch by Shamil Abdulaev.
2 changes: 1 addition & 1 deletion Parser/myreadline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to stop the loop if n==0.

Suggested change
} while (n == 0 || p[n-1] != '\n');
} while (n != 0 && p[n-1] != '\n');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then input like b'\x00spam\nham\nspam\n' will stop early. Is this intended?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure which behavior is the correct behavior honestly.

I suppose that you should call readline again to read spam\n.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds worth it to add a test at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then behavior will be different if string starts with NUL byte or has it in the middle. I think we should document this difference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this conversation here: #140910 (comment).
Current fix seems to be slightly better for me.


pr = (char *)PyMem_RawRealloc(p, n+1);
if (pr == NULL) {
Expand Down
Loading