Skip to content

Commit

Permalink
Merge 0564fb8 into dc9ecaa
Browse files Browse the repository at this point in the history
  • Loading branch information
wiredfool committed Apr 1, 2016
2 parents dc9ecaa + 0564fb8 commit e01ec86
Show file tree
Hide file tree
Showing 18 changed files with 945 additions and 826 deletions.
56 changes: 35 additions & 21 deletions _imaging.c
Expand Up @@ -360,9 +360,20 @@ getbands(const char* mode)
#define TYPE_DOUBLE (0x400|sizeof(double))

static void*
getlist(PyObject* arg, int* length, const char* wrong_length, int type)
{
int i, n, itemp;
getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type)
{
/* - allocates and returns a c array of the items in the
python sequence arg.
- the size of the returned array is in length
- all of the arg items must be numeric items of the type
specified in type
- sequence length is checked against the length parameter IF
an error parameter is passed in wrong_length
- caller is responsible for freeing the memory
*/

Py_ssize_t i, n;
int itemp;
double dtemp;
void* list;
PyObject* seq;
Expand All @@ -374,12 +385,14 @@ getlist(PyObject* arg, int* length, const char* wrong_length, int type)
}

n = PyObject_Length(arg);
if (length && wrong_length && n != *length) {
if (length && wrong_length && n != *length) {
PyErr_SetString(PyExc_ValueError, wrong_length);
return NULL;
}

list = malloc(n * (type & 0xff));
/* malloc check ok, type & ff is just a sizeof(something)
calloc checks for overflow */
list = calloc(n, type & 0xff);
if (!list)
return PyErr_NoMemory();

Expand Down Expand Up @@ -843,7 +856,7 @@ static PyObject*
_filter(ImagingObject* self, PyObject* args)
{
PyObject* imOut;
int kernelsize;
Py_ssize_t kernelsize;
FLOAT32* kerneldata;

int xsize, ysize;
Expand All @@ -857,7 +870,7 @@ _filter(ImagingObject* self, PyObject* args)
kerneldata = getlist(kernel, &kernelsize, NULL, TYPE_FLOAT32);
if (!kerneldata)
return NULL;
if (kernelsize != xsize * ysize) {
if (kernelsize != (Py_ssize_t) xsize * (Py_ssize_t) ysize) {
free(kerneldata);
return ImagingError_ValueError("bad kernel size");
}
Expand Down Expand Up @@ -1146,8 +1159,8 @@ _point(ImagingObject* self, PyObject* args)
{
static const char* wrong_number = "wrong number of lut entries";

int n, i;
int bands;
Py_ssize_t n;
int i, bands;
Imaging im;

PyObject* list;
Expand Down Expand Up @@ -1662,7 +1675,7 @@ _transform2(ImagingObject* self, PyObject* args)

Imaging imIn;
Imaging imOut;
int n;
Py_ssize_t n;
double *a;

ImagingObject* imagep;
Expand Down Expand Up @@ -1918,6 +1931,7 @@ _getprojection(ImagingObject* self, PyObject* args)
unsigned char* yprofile;
PyObject* result;

/* malloc check ok */
xprofile = malloc(self->image->xsize);
yprofile = malloc(self->image->ysize);

Expand Down Expand Up @@ -2364,7 +2378,7 @@ _draw_dealloc(ImagingDrawObject* self)
PyObject_Del(self);
}

extern int PyPath_Flatten(PyObject* data, double **xy);
extern Py_ssize_t PyPath_Flatten(PyObject* data, double **xy);

static PyObject*
_draw_ink(ImagingDrawObject* self, PyObject* args)
Expand All @@ -2385,7 +2399,7 @@ static PyObject*
_draw_arc(ImagingDrawObject* self, PyObject* args)
{
double* xy;
int n;
Py_ssize_t n;

PyObject* data;
int ink;
Expand Down Expand Up @@ -2423,7 +2437,7 @@ static PyObject*
_draw_bitmap(ImagingDrawObject* self, PyObject* args)
{
double *xy;
int n;
Py_ssize_t n;

PyObject *data;
ImagingObject* bitmap;
Expand Down Expand Up @@ -2459,7 +2473,7 @@ static PyObject*
_draw_chord(ImagingDrawObject* self, PyObject* args)
{
double* xy;
int n;
Py_ssize_t n;

PyObject* data;
int ink, fill;
Expand Down Expand Up @@ -2497,7 +2511,7 @@ static PyObject*
_draw_ellipse(ImagingDrawObject* self, PyObject* args)
{
double* xy;
int n;
Py_ssize_t n;

PyObject* data;
int ink;
Expand Down Expand Up @@ -2550,7 +2564,7 @@ static PyObject*
_draw_lines(ImagingDrawObject* self, PyObject* args)
{
double *xy;
int i, n;
Py_ssize_t i, n;

PyObject *data;
int ink;
Expand Down Expand Up @@ -2618,7 +2632,7 @@ static PyObject*
_draw_points(ImagingDrawObject* self, PyObject* args)
{
double *xy;
int i, n;
Py_ssize_t i, n;

PyObject *data;
int ink;
Expand Down Expand Up @@ -2680,7 +2694,7 @@ static PyObject*
_draw_pieslice(ImagingDrawObject* self, PyObject* args)
{
double* xy;
int n;
Py_ssize_t n;

PyObject* data;
int ink, fill;
Expand Down Expand Up @@ -2718,7 +2732,7 @@ _draw_polygon(ImagingDrawObject* self, PyObject* args)
{
double *xy;
int *ixy;
int n, i;
Py_ssize_t n, i;

PyObject* data;
int ink;
Expand All @@ -2737,7 +2751,7 @@ _draw_polygon(ImagingDrawObject* self, PyObject* args)
}

/* Copy list of vertices to array */
ixy = (int*) malloc(n * 2 * sizeof(int));
ixy = (int*) calloc(n, 2 * sizeof(int));

for (i = 0; i < n; i++) {
ixy[i+i] = (int) xy[i+i];
Expand All @@ -2762,7 +2776,7 @@ static PyObject*
_draw_rectangle(ImagingDrawObject* self, PyObject* args)
{
double* xy;
int n;
Py_ssize_t n;

PyObject* data;
int ink;
Expand Down
17 changes: 11 additions & 6 deletions encode.c
Expand Up @@ -559,6 +559,7 @@ static unsigned int* get_qtables_arrays(PyObject* qtables, int* qtablesLen) {
Py_DECREF(tables);
return NULL;
}
/* malloc check ok, num_tables <4, DCTSIZE2 == 64 from jpeglib.h */
qarrays = (unsigned int*) malloc(num_tables * DCTSIZE2 * sizeof(unsigned int));
if (!qarrays) {
Py_DECREF(tables);
Expand Down Expand Up @@ -634,6 +635,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
qarrays = get_qtables_arrays(qtables, &qtablesLen);

if (extra && extra_size > 0) {
/* malloc check ok, length is from python parsearg */
char* p = malloc(extra_size); // Freed in JpegEncode, Case 5
if (!p)
return PyErr_NoMemory();
Expand All @@ -643,6 +645,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
extra = NULL;

if (rawExif && rawExifLen > 0) {
/* malloc check ok, length is from python parsearg */
char* pp = malloc(rawExifLen); // Freed in JpegEncode, Case 5
if (!pp)
return PyErr_NoMemory();
Expand Down Expand Up @@ -757,15 +760,16 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
(ttag_t) PyInt_AsLong(key),
PyBytes_AsString(value));
} else if (PyTuple_Check(value)) {
int len,i;
Py_ssize_t len,i;
float *floatav;
int *intav;
TRACE(("Setting from Tuple: %d \n", (int)PyInt_AsLong(key)));
len = (int)PyTuple_Size(value);
len = PyTuple_Size(value);
if (len) {
if (PyInt_Check(PyTuple_GetItem(value,0))) {
TRACE((" %d elements, setting as ints \n", len));
intav = malloc(sizeof(int)*len);
TRACE((" %d elements, setting as ints \n", (int)len));
/* malloc check ok, calloc checks for overflow */
intav = calloc(len, sizeof(int));
if (intav) {
for (i=0;i<len;i++) {
intav[i] = (int)PyInt_AsLong(PyTuple_GetItem(value,i));
Expand All @@ -776,8 +780,9 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
free(intav);
}
} else if (PyFloat_Check(PyTuple_GetItem(value,0))) {
TRACE((" %d elements, setting as floats \n", len));
floatav = malloc(sizeof(float)*len);
TRACE((" %d elements, setting as floats \n", (int)len));
/* malloc check ok, calloc checks for overflow */
floatav = calloc(len, sizeof(float));
if (floatav) {
for (i=0;i<len;i++) {
floatav[i] = (float)PyFloat_AsDouble(PyTuple_GetItem(value,i));
Expand Down

0 comments on commit e01ec86

Please sign in to comment.