FIX memory leak

ImagingNewEpilogue now is always success
The Imaging object itself is freed through ImagingDelete in case
when memory is not allocated in ImagingNewBlock or ImagingNewArray
This commit is contained in:
Alexander 2017-08-05 20:25:57 +03:00
parent 152104bba3
commit fd9cf03d01
3 changed files with 24 additions and 25 deletions

View File

@ -172,7 +172,7 @@ extern Imaging ImagingNewPrologue(const char *mode,
extern Imaging ImagingNewPrologueSubtype(const char *mode, extern Imaging ImagingNewPrologueSubtype(const char *mode,
int xsize, int ysize, int xsize, int ysize,
int structure_size); int structure_size);
extern Imaging ImagingNewEpilogue(Imaging im); extern void ImagingNewEpilogue(Imaging im);
extern void ImagingCopyInfo(Imaging destination, Imaging source); extern void ImagingCopyInfo(Imaging destination, Imaging source);

View File

@ -232,15 +232,9 @@ ImagingNewPrologue(const char *mode, int xsize, int ysize)
mode, xsize, ysize, sizeof(struct ImagingMemoryInstance)); mode, xsize, ysize, sizeof(struct ImagingMemoryInstance));
} }
Imaging void
ImagingNewEpilogue(Imaging im) ImagingNewEpilogue(Imaging im)
{ {
/* If the raster data allocator didn't setup a destructor,
assume that it couldn't allocate the required amount of
memory. */
if (!im->destroy)
return (Imaging) ImagingError_MemoryError();
/* Initialize alias pointers to pixel data. */ /* Initialize alias pointers to pixel data. */
switch (im->pixelsize) { switch (im->pixelsize) {
case 1: case 2: case 3: case 1: case 2: case 3:
@ -250,8 +244,6 @@ ImagingNewEpilogue(Imaging im)
im->image32 = (INT32 **) im->image; im->image32 = (INT32 **) im->image;
break; break;
} }
return im;
} }
void void
@ -316,10 +308,15 @@ ImagingNewArray(const char *mode, int xsize, int ysize)
ImagingSectionLeave(&cookie); ImagingSectionLeave(&cookie);
if (y == im->ysize) if (y != im->ysize) {
im->destroy = ImagingDestroyArray; ImagingDelete(im);
return (Imaging) ImagingError_MemoryError();
}
return ImagingNewEpilogue(im); im->destroy = ImagingDestroyArray;
ImagingNewEpilogue(im);
return im;
} }
@ -364,16 +361,20 @@ ImagingNewBlock(const char *mode, int xsize, int ysize)
im->block = (char *) calloc(im->ysize, im->linesize); im->block = (char *) calloc(im->ysize, im->linesize);
} }
if (im->block) { if ( ! im->block) {
for (y = i = 0; y < im->ysize; y++) { ImagingDelete(im);
im->image[y] = im->block + i; return (Imaging) ImagingError_MemoryError();
i += im->linesize;
}
im->destroy = ImagingDestroyBlock;
} }
return ImagingNewEpilogue(im); for (y = i = 0; y < im->ysize; y++) {
im->image[y] = im->block + i;
i += im->linesize;
}
im->destroy = ImagingDestroyBlock;
ImagingNewEpilogue(im);
return im;
} }
/* -------------------------------------------------------------------- /* --------------------------------------------------------------------

6
map.c
View File

@ -229,8 +229,7 @@ mapping_readimage(ImagingMapperObject* mapper, PyObject* args)
im->destroy = ImagingDestroyMap; im->destroy = ImagingDestroyMap;
if (!ImagingNewEpilogue(im)) ImagingNewEpilogue(im);
return NULL;
mapper->offset += size; mapper->offset += size;
@ -387,8 +386,7 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args)
((ImagingBufferInstance*) im)->target = target; ((ImagingBufferInstance*) im)->target = target;
((ImagingBufferInstance*) im)->view = view; ((ImagingBufferInstance*) im)->view = view;
if (!ImagingNewEpilogue(im)) ImagingNewEpilogue(im);
return NULL;
return PyImagingNew(im); return PyImagingNew(im);
} }