Skip to content

Commit

Permalink
Merge pull request #5113 from radarhere/memory
Browse files Browse the repository at this point in the history
Replaced PyErr_NoMemory with ImagingError_MemoryError
  • Loading branch information
hugovk committed Dec 20, 2020
2 parents 19648fa + b7fb39f commit 396b329
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
12 changes: 5 additions & 7 deletions src/_imaging.c
Expand Up @@ -377,7 +377,7 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type)
calloc checks for overflow */
list = calloc(n, type & 0xff);
if ( ! list) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}

seq = PySequence_Fast(arg, must_be_sequence);
Expand Down Expand Up @@ -789,7 +789,7 @@ _prepare_lut_table(PyObject* table, Py_ssize_t table_size)
if (free_table_data) {
free(table_data);
}
return (INT16*) PyErr_NoMemory();
return (INT16*) ImagingError_MemoryError();
}

for (i = 0; i < table_size; i++) {
Expand Down Expand Up @@ -2234,7 +2234,7 @@ _getprojection(ImagingObject* self, PyObject* args)
if (xprofile == NULL || yprofile == NULL) {
free(xprofile);
free(yprofile);
return PyErr_NoMemory();
return ImagingError_MemoryError();
}

ImagingGetProjection(self->image, (unsigned char *)xprofile, (unsigned char *)yprofile);
Expand Down Expand Up @@ -2711,8 +2711,7 @@ _font_getmask(ImagingFontObject* self, PyObject* args)
im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
if (!im) {
free(text);
ImagingError_MemoryError();
return NULL;
return ImagingError_MemoryError();
}

b = 0;
Expand Down Expand Up @@ -3933,8 +3932,7 @@ _set_blocks_max(PyObject* self, PyObject* args)


if ( ! ImagingMemorySetBlocksMax(&ImagingDefaultArena, blocks_max)) {
ImagingError_MemoryError();
return NULL;
return ImagingError_MemoryError();
}

Py_INCREF(Py_None);
Expand Down
6 changes: 3 additions & 3 deletions src/decode.c
Expand Up @@ -80,7 +80,7 @@ PyImaging_DecoderNew(int contextsize)
context = (void*) calloc(1, contextsize);
if (!context) {
Py_DECREF(decoder);
(void) PyErr_NoMemory();
(void) ImagingError_MemoryError();
return NULL;
}
} else {
Expand Down Expand Up @@ -204,14 +204,14 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args)
if (state->bits > 0) {
if (!state->bytes) {
if (state->xsize > ((INT_MAX / state->bits)-7)){
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
state->bytes = (state->bits * state->xsize+7)/8;
}
/* malloc check ok, overflow checked above */
state->buffer = (UINT8*) malloc(state->bytes);
if (!state->buffer) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/encode.c
Expand Up @@ -72,7 +72,7 @@ PyImaging_EncoderNew(int contextsize)
context = (void*) calloc(1, contextsize);
if (!context) {
Py_DECREF(encoder);
(void) PyErr_NoMemory();
(void) ImagingError_MemoryError();
return NULL;
}
} else {
Expand Down Expand Up @@ -194,7 +194,7 @@ _encode_to_file(ImagingEncoderObject* encoder, PyObject* args)
/* malloc check ok, either constant int, or checked by PyArg_ParseTuple */
buf = (UINT8*) malloc(bufsize);
if (!buf) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}

ImagingSectionEnter(&cookie);
Expand Down Expand Up @@ -271,13 +271,13 @@ _setimage(ImagingEncoderObject* encoder, PyObject* args)
/* Allocate memory buffer (if bits field is set) */
if (state->bits > 0) {
if (state->xsize > ((INT_MAX / state->bits)-7)) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
state->bytes = (state->bits * state->xsize+7)/8;
/* malloc check ok, overflow checked above */
state->buffer = (UINT8*) malloc(state->bytes);
if (!state->buffer) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
}

Expand Down Expand Up @@ -604,7 +604,7 @@ PyImaging_ZipEncoderNew(PyObject* self, PyObject* args)
/* malloc check ok, size comes from PyArg_ParseTuple */
char* p = malloc(dictionary_size);
if (!p) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
memcpy(p, dictionary, dictionary_size);
dictionary = p;
Expand Down Expand Up @@ -1005,8 +1005,7 @@ static unsigned int* get_qtables_arrays(PyObject* qtables, int* qtablesLen) {
qarrays = (unsigned int*) malloc(num_tables * DCTSIZE2 * sizeof(unsigned int));
if (!qarrays) {
Py_DECREF(tables);
PyErr_NoMemory();
return NULL;
return ImagingError_MemoryError();
}
for (i = 0; i < num_tables; i++) {
table = PySequence_Fast_GET_ITEM(tables, i);
Expand Down Expand Up @@ -1091,7 +1090,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
/* malloc check ok, length is from python parsearg */
char* p = malloc(extra_size); // Freed in JpegEncode, Case 5
if (!p) {
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
memcpy(p, extra, extra_size);
extra = p;
Expand All @@ -1106,7 +1105,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
if (extra) {
free(extra);
}
return PyErr_NoMemory();
return ImagingError_MemoryError();
}
memcpy(pp, rawExif, rawExifLen);
rawExif = pp;
Expand Down
8 changes: 3 additions & 5 deletions src/path.c
Expand Up @@ -53,16 +53,14 @@ alloc_array(Py_ssize_t count)
{
double* xy;
if (count < 0) {
PyErr_NoMemory();
return NULL;
return ImagingError_MemoryError();
}
if ((unsigned long long)count > (SIZE_MAX / (2 * sizeof(double))) - 1 ) {
PyErr_NoMemory();
return NULL;
return ImagingError_MemoryError();
}
xy = malloc(2 * count * sizeof(double) + 1);
if (!xy) {
PyErr_NoMemory();
ImagingError_MemoryError();
}
return xy;
}
Expand Down

0 comments on commit 396b329

Please sign in to comment.