mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.11] gh-105235: Prevent reading outside buffer during mmap.find() (… (#106710)
[3.11] gh-105235: Prevent reading outside buffer during mmap.find() (GH-105252)
* Add a special case for s[-m:] == p in _PyBytes_Find
* Add tests for _PyBytes_Find
* Make sure that start <= end in mmap.find.
(cherry picked from commit ab86426a34)
This commit is contained in:
parent
2186212191
commit
d488970ae6
5 changed files with 161 additions and 3 deletions
|
|
@ -1283,8 +1283,25 @@ _PyBytes_Find(const char *haystack, Py_ssize_t len_haystack,
|
|||
const char *needle, Py_ssize_t len_needle,
|
||||
Py_ssize_t offset)
|
||||
{
|
||||
return stringlib_find(haystack, len_haystack,
|
||||
needle, len_needle, offset);
|
||||
assert(len_haystack >= 0);
|
||||
assert(len_needle >= 0);
|
||||
// Extra checks because stringlib_find accesses haystack[len_haystack].
|
||||
if (len_needle == 0) {
|
||||
return offset;
|
||||
}
|
||||
if (len_needle > len_haystack) {
|
||||
return -1;
|
||||
}
|
||||
assert(len_haystack >= 1);
|
||||
Py_ssize_t res = stringlib_find(haystack, len_haystack - 1,
|
||||
needle, len_needle, offset);
|
||||
if (res == -1) {
|
||||
Py_ssize_t last_align = len_haystack - len_needle;
|
||||
if (memcmp(haystack + last_align, needle, len_needle) == 0) {
|
||||
return offset + last_align;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Py_ssize_t
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue