mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-09-21 11:28:58 +03:00
Rework block allocator
This commit is contained in:
parent
768936fa33
commit
54a979749c
|
@ -345,27 +345,30 @@ ImagingNewBlock(const char *mode, int xsize, int ysize)
|
||||||
{
|
{
|
||||||
Imaging im;
|
Imaging im;
|
||||||
Py_ssize_t y, i;
|
Py_ssize_t y, i;
|
||||||
Py_ssize_t bytes;
|
|
||||||
|
|
||||||
im = ImagingNewPrologue(mode, xsize, ysize);
|
im = ImagingNewPrologue(mode, xsize, ysize);
|
||||||
if (!im)
|
if (!im)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
/* We shouldn't overflow, since the threshold defined
|
||||||
|
below says that we're only going to allocate max 4M
|
||||||
|
here before going to the array allocator. Check anyway.
|
||||||
|
*/
|
||||||
if (im->linesize &&
|
if (im->linesize &&
|
||||||
im->ysize > SIZE_MAX / im->linesize) {
|
im->ysize > INT_MAX / im->linesize) {
|
||||||
/* punt if we're going to overflow */
|
/* punt if we're going to overflow */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
/* Use a single block */
|
|
||||||
bytes = ((Py_ssize_t) im->ysize) * im->linesize;
|
if (im->ysize * im->linesize <= 0) {
|
||||||
if (bytes <= 0)
|
|
||||||
/* some platforms return NULL for malloc(0); this fix
|
/* some platforms return NULL for malloc(0); this fix
|
||||||
prevents MemoryError on zero-sized images on such
|
prevents MemoryError on zero-sized images on such
|
||||||
platforms */
|
platforms */
|
||||||
bytes = 1;
|
im->block = (char *) malloc(1);
|
||||||
|
} else {
|
||||||
/* malloc check ok, overflow check above */
|
/* malloc check ok, overflow check above */
|
||||||
im->block = (char *) calloc(bytes, 1);
|
im->block = (char *) calloc(im->ysize, im->linesize);
|
||||||
|
}
|
||||||
|
|
||||||
if (im->block) {
|
if (im->block) {
|
||||||
for (y = i = 0; y < im->ysize; y++) {
|
for (y = i = 0; y < im->ysize; y++) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user