FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Protect getlist() walk with a critical section (free-threading, #9852) by espressolee · Pull Request #9854 · python-pillow/Pillow · GitHub

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 14 additions & 1 deletion src/_imaging.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@

#include "libImaging/Imaging.h"

#include "thirdparty/pythoncapi_compat.h"

#define _USE_MATH_DEFINES
#include <math.h>
#include <stddef.h>
Expand Down Expand Up @@ -461,7 +463,17 @@ getlist(PyObject *arg, Py_ssize_t *length, const char *wrong_length, int type) {
return NULL;
}

for (i = 0; i < n; i++) {
// On a free-threaded build PySequence_Fast returns the list itself for a list
// input, so the walk below aliases the caller's list. Hold a critical section on
// it and clamp to its current length, so a concurrent resize cannot make
// PySequence_Fast_GET_ITEM read out of bounds (#9852). `n` was read before this
// point and may be stale.
Py_BEGIN_CRITICAL_SECTION(seq);
Py_ssize_t count = PySequence_Fast_GET_SIZE(seq);
if (count > n) {
count = n;
}
for (i = 0; i < count; i++) {
op = PySequence_Fast_GET_ITEM(seq, i);
// DRY, branch prediction is going to work _really_ well
// on this switch. And 3 fewer loops to copy/paste.
Expand All @@ -484,6 +496,7 @@ getlist(PyObject *arg, Py_ssize_t *length, const char *wrong_length, int type) {
break;
}
}
Py_END_CRITICAL_SECTION();

Py_DECREF(seq);

Expand Down
Loading

Back | FazBrowse Home | New Git URL