mirror of
https://github.com/python/cpython.git
synced 2025-11-08 09:32:01 +00:00
[3.14] gh-127971: fix off-by-one read beyond the end of a string during search (GH-132574) (#136628)
gh-127971: fix off-by-one read beyond the end of a string during search (GH-132574)
(cherry picked from commit 85ec3b3b50)
Co-authored-by: Duane Griffin <duaneg@dghda.com>
This commit is contained in:
parent
ed1e0cdc58
commit
348e22cf06
3 changed files with 14 additions and 4 deletions
|
|
@ -767,6 +767,15 @@ def test_replace(self):
|
||||||
self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
|
self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
|
||||||
self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
|
self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
|
||||||
|
|
||||||
|
def test_replacement_on_buffer_boundary(self):
|
||||||
|
# gh-127971: Check we don't read past the end of the buffer when a
|
||||||
|
# potential match misses on the last character.
|
||||||
|
any_3_nonblank_codepoints = '!!!'
|
||||||
|
seven_codepoints = any_3_nonblank_codepoints + ' ' + any_3_nonblank_codepoints
|
||||||
|
a = (' ' * 243) + seven_codepoints + (' ' * 7)
|
||||||
|
b = ' ' * 6 + chr(256)
|
||||||
|
a.replace(seven_codepoints, b)
|
||||||
|
|
||||||
def test_replace_uses_two_way_maxcount(self):
|
def test_replace_uses_two_way_maxcount(self):
|
||||||
# Test that maxcount works in _two_way_count in fastsearch.h
|
# Test that maxcount works in _two_way_count in fastsearch.h
|
||||||
A, B = "A"*1000, "B"*1000
|
A, B = "A"*1000, "B"*1000
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Fix off-by-one read beyond the end of a string in string search.
|
||||||
|
|
@ -595,7 +595,7 @@ STRINGLIB(default_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* miss: check if next character is part of pattern */
|
/* miss: check if next character is part of pattern */
|
||||||
if (!STRINGLIB_BLOOM(mask, ss[i+1])) {
|
if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
|
||||||
i = i + m;
|
i = i + m;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -604,7 +604,7 @@ STRINGLIB(default_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* skip: check if next character is part of pattern */
|
/* skip: check if next character is part of pattern */
|
||||||
if (!STRINGLIB_BLOOM(mask, ss[i+1])) {
|
if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
|
||||||
i = i + m;
|
i = i + m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -668,7 +668,7 @@ STRINGLIB(adaptive_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* miss: check if next character is part of pattern */
|
/* miss: check if next character is part of pattern */
|
||||||
if (!STRINGLIB_BLOOM(mask, ss[i+1])) {
|
if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
|
||||||
i = i + m;
|
i = i + m;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -677,7 +677,7 @@ STRINGLIB(adaptive_find)(const STRINGLIB_CHAR* s, Py_ssize_t n,
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* skip: check if next character is part of pattern */
|
/* skip: check if next character is part of pattern */
|
||||||
if (!STRINGLIB_BLOOM(mask, ss[i+1])) {
|
if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) {
|
||||||
i = i + m;
|
i = i + m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue