-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-137928: Centralize size validation in multiprocessing.heap #137929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,15 @@ def _roundup(n, alignment): | |
| mask = alignment - 1 | ||
| return (n + mask) & ~mask | ||
|
|
||
| @staticmethod | ||
| # Bind sys.maxsize once at function definition time to avoid global lookups. | ||
| def _validate_size(size, _maxsize=sys.maxsize): | ||
| """Validate requested size; raise ValueError if < 0, OverflowError if >= _maxsize.""" | ||
| if size < 0: | ||
| raise ValueError("Size {0:n} out of range".format(size)) | ||
| if _maxsize <= size: | ||
| raise OverflowError("Size {0:n} too large".format(size)) | ||
|
|
||
| def _new_arena(self, size): | ||
| # Create a new arena with at least the given *size* | ||
| length = self._roundup(max(self._size, size), mmap.PAGESIZE) | ||
|
|
@@ -295,10 +304,7 @@ def free(self, block): | |
|
|
||
| def malloc(self, size): | ||
| # return a block of right size (possibly rounded up) | ||
| if size < 0: | ||
| raise ValueError("Size {0:n} out of range".format(size)) | ||
| if sys.maxsize <= size: | ||
| raise OverflowError("Size {0:n} too large".format(size)) | ||
| Heap._validate_size(size) | ||
| if os.getpid() != self._lastpid: | ||
| self.__init__() # reinitialize after fork | ||
| with self._lock: | ||
|
|
@@ -324,10 +330,7 @@ class BufferWrapper(object): | |
| _heap = Heap() | ||
|
|
||
| def __init__(self, size): | ||
| if size < 0: | ||
| raise ValueError("Size {0:n} out of range".format(size)) | ||
| if sys.maxsize <= size: | ||
| raise OverflowError("Size {0:n} too large".format(size)) | ||
| Heap._validate_size(size) | ||
|
||
| block = BufferWrapper._heap.malloc(size) | ||
| self._state = (block, size) | ||
| util.Finalize(self, BufferWrapper._heap.free, args=(block,)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will no longer apply when moving it to the module level, but thanks for the suggestion