gh-150449: Fix sqlite3.Blob crash with negative-step slices#150450
gh-150449: Fix sqlite3.Blob crash with negative-step slices#150450ever0de wants to merge 1 commit into
Conversation
Reading or writing a sqlite3.Blob with a negative-step slice such as blob[9:0:-2] caused a crash (SystemError on older versions, ValueError on current main) because the C code computed stop - start as the read length, which is negative for negative steps. Fix subscript_slice() and ass_subscript_slice() in Modules/_sqlite/blob.c to handle both positive and negative steps correctly: - For step > 1: keep reading [start, stop) and indexing forward as before. - For step < -1: read the contiguous range [start+(len-1)*step, start] and index backward with stride -step.
561ce7f to
24ade8a
Compare
| // For positive step: read the contiguous range [start, stop) and pick | ||
| // every step-th byte. For negative step: read the contiguous range | ||
| // [start+(len-1)*step, start] and pick bytes in reverse stride order. | ||
| Py_ssize_t read_offset, read_length; |
There was a problem hiding this comment.
We have an adjust slice function somewhere in the codebase, use the latter instead.
picnixz
left a comment
There was a problem hiding this comment.
Please also exercise negative stepsize when start <= end (it doesn't make sense but still it needs to be checked).
| } | ||
| else { | ||
| PyObject *blob_bytes = read_multiple(self, stop - start, start); | ||
| // For positive step: read the contiguous range [start, stop) and |
There was a problem hiding this comment.
It's not a contiguous range if step != 1.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Reading or writing a sqlite3.Blob with a negative-step slice such as blob[9:0:-2] caused a crash (SystemError on older versions, ValueError on current main) because the C code computed stop - start as the read length, which is negative for negative steps.
Fix subscript_slice() and ass_subscript_slice() in Modules/_sqlite/blob.c to handle both positive and negative steps correctly: