Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite13d1d9

Browse files
authored
gh-99581: Fix a buffer overflow in the tokenizer when copying lines that fill the available buffer (#99605)
1 parentabf5b6f commite13d1d9

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

‎Lib/test/test_tokenize.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
fromunittestimportTestCase,mock
1111
fromtest.test_grammarimport (VALID_UNDERSCORE_LITERALS,
1212
INVALID_UNDERSCORE_LITERALS)
13+
fromtest.supportimportos_helper
14+
fromtest.support.script_helperimportrun_test_script,make_script
1315
importos
1416
importtoken
1517

@@ -2631,5 +2633,19 @@ def fib(n):
26312633
self.assertEqual(get_tokens(code),get_tokens(code_no_cont))
26322634

26332635

2636+
classCTokenizerBufferTests(unittest.TestCase):
2637+
deftest_newline_at_the_end_of_buffer(self):
2638+
# See issue 99581: Make sure that if we need to add a new line at the
2639+
# end of the buffer, we have enough space in the buffer, specially when
2640+
# the current line is as long as the buffer space available.
2641+
test_script=f"""\
2642+
#coding: latin-1
2643+
#{"a"*10000}
2644+
#{"a"*10002}"""
2645+
withos_helper.temp_dir()astemp_dir:
2646+
file_name=make_script(temp_dir,'foo',test_script)
2647+
run_test_script(file_name)
2648+
2649+
26342650
if__name__=="__main__":
26352651
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a bug that was causing a buffer overflow if the tokenizer copies a
2+
line missing the newline caracter from a file that is as long as the
3+
available tokenizer buffer. Patch by Pablo galindo

‎Parser/tokenizer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ tok_readline_recode(struct tok_state *tok) {
413413
error_ret(tok);
414414
gotoerror;
415415
}
416-
if (!tok_reserve_buf(tok,buflen+1)) {
416+
// Make room for the null terminator *and* potentially
417+
// an extra newline character that we may need to artificially
418+
// add.
419+
size_tbuffer_size=buflen+2;
420+
if (!tok_reserve_buf(tok,buffer_size)) {
417421
gotoerror;
418422
}
419423
memcpy(tok->inp,buf,buflen);
@@ -1000,6 +1004,7 @@ tok_underflow_file(struct tok_state *tok) {
10001004
return0;
10011005
}
10021006
if (tok->inp[-1]!='\n') {
1007+
assert(tok->inp+1<tok->end);
10031008
/* Last line does not end in \n, fake one */
10041009
*tok->inp++='\n';
10051010
*tok->inp='\0';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp