Fix CodeQL Alerts

- Cast int to Py_ssize_t for multiplication
This commit is contained in:
Andrew Senetar 2021-08-26 03:43:31 -05:00
parent 83f401595d
commit 809116c764
Signed by: arsenetar
GPG Key ID: C63300DCE48AB2F1
2 changed files with 336 additions and 354 deletions

View File

@ -2,9 +2,9 @@
* Created On: 2010-01-30
* Copyright 2014 Hardcoded Software (http://www.hardcoded.net)
*
* This software is licensed under the "BSD" License as described in the "LICENSE" file,
* which should be included with this package. The terms are also available at
* http://www.hardcoded.net/licenses/bsd_license
* This software is licensed under the "BSD" License as described in the
* "LICENSE" file, which should be included with this package. The terms are
* also available at http://www.hardcoded.net/licenses/bsd_license
*/
#include "common.h"
@ -17,8 +17,7 @@ static PyObject *DifferentBlockCountError;
/* Returns a 3 sized tuple containing the mean color of 'image'.
* image: a PIL image or crop.
*/
static PyObject* getblock(PyObject *image)
{
static PyObject *getblock(PyObject *image) {
int i, totr, totg, totb;
Py_ssize_t pixel_count;
PyObject *ppixels;
@ -30,7 +29,7 @@ static PyObject* getblock(PyObject *image)
}
pixel_count = PySequence_Length(ppixels);
for (i=0; i<pixel_count; i++) {
for (i = 0; i < pixel_count; i++) {
PyObject *ppixel, *pr, *pg, *pb;
int r, g, b;
@ -65,8 +64,7 @@ static PyObject* getblock(PyObject *image)
/* Returns the difference between the first block and the second.
* It returns an absolute sum of the 3 differences (RGB).
*/
static int diff(PyObject *first, PyObject *second)
{
static int diff(PyObject *first, PyObject *second) {
int r1, g1, b1, r2, b2, g2;
PyObject *pr, *pg, *pb;
pr = PySequence_ITEM(first, 0);
@ -93,7 +91,7 @@ static int diff(PyObject *first, PyObject *second)
}
PyDoc_STRVAR(block_getblocks2_doc,
"Returns a list of blocks (3 sized tuples).\n\
"Returns a list of blocks (3 sized tuples).\n\
\n\
image: A PIL image to base the blocks on.\n\
block_count_per_side: This integer determine the number of blocks the function will return.\n\
@ -101,8 +99,7 @@ If it is 10, for example, 100 blocks will be returns (10 width, 10 height). The
necessarely cover square areas. The area covered by each block will be proportional to the image\n\
itself.\n");
static PyObject* block_getblocks2(PyObject *self, PyObject *args)
{
static PyObject *block_getblocks2(PyObject *self, PyObject *args) {
int block_count_per_side, width, height, block_width, block_height, ih;
PyObject *image;
PyObject *pimage_size, *pwidth, *pheight;
@ -128,23 +125,23 @@ static PyObject* block_getblocks2(PyObject *self, PyObject *args)
block_width = max(width / block_count_per_side, 1);
block_height = max(height / block_count_per_side, 1);
result = PyList_New(block_count_per_side * block_count_per_side);
result = PyList_New((Py_ssize_t)block_count_per_side * block_count_per_side);
if (result == NULL) {
return NULL;
}
for (ih=0; ih<block_count_per_side; ih++) {
for (ih = 0; ih < block_count_per_side; ih++) {
int top, bottom, iw;
top = min(ih*block_height, height-block_height);
top = min(ih * block_height, height - block_height);
bottom = top + block_height;
for (iw=0; iw<block_count_per_side; iw++) {
for (iw = 0; iw < block_count_per_side; iw++) {
int left, right;
PyObject *pbox;
PyObject *pmethodname;
PyObject *pcrop;
PyObject *pblock;
left = min(iw*block_width, width-block_width);
left = min(iw * block_width, width - block_width);
right = left + block_width;
pbox = inttuple(4, left, top, right, bottom);
pmethodname = PyUnicode_FromString("crop");
@ -161,7 +158,7 @@ static PyObject* block_getblocks2(PyObject *self, PyObject *args)
Py_DECREF(result);
return NULL;
}
PyList_SET_ITEM(result, ih*block_count_per_side+iw, pblock);
PyList_SET_ITEM(result, ih * block_count_per_side + iw, pblock);
}
}
@ -169,19 +166,19 @@ static PyObject* block_getblocks2(PyObject *self, PyObject *args)
}
PyDoc_STRVAR(block_avgdiff_doc,
"Returns the average diff between first blocks and seconds.\n\
"Returns the average diff between first blocks and seconds.\n\
\n\
If the result surpasses limit, limit + 1 is returned, except if less than min_iterations\n\
iterations have been made in the blocks.\n");
static PyObject* block_avgdiff(PyObject *self, PyObject *args)
{
static PyObject *block_avgdiff(PyObject *self, PyObject *args) {
PyObject *first, *second;
int limit, min_iterations;
Py_ssize_t count;
int sum, i, result;
if (!PyArg_ParseTuple(args, "OOii", &first, &second, &limit, &min_iterations)) {
if (!PyArg_ParseTuple(args, "OOii", &first, &second, &limit,
&min_iterations)) {
return NULL;
}
@ -196,7 +193,7 @@ static PyObject* block_avgdiff(PyObject *self, PyObject *args)
}
sum = 0;
for (i=0; i<count; i++) {
for (i = 0; i < count; i++) {
int iteration_count;
PyObject *item1, *item2;
@ -206,7 +203,8 @@ static PyObject* block_avgdiff(PyObject *self, PyObject *args)
sum += diff(item1, item2);
Py_DECREF(item1);
Py_DECREF(item2);
if ((sum > limit*iteration_count) && (iteration_count >= min_iterations)) {
if ((sum > limit * iteration_count) &&
(iteration_count >= min_iterations)) {
return PyLong_FromLong(limit + 1);
}
}
@ -224,8 +222,7 @@ static PyMethodDef BlockMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef BlockDef = {
PyModuleDef_HEAD_INIT,
static struct PyModuleDef BlockDef = {PyModuleDef_HEAD_INIT,
"_block",
NULL,
-1,
@ -233,12 +230,9 @@ static struct PyModuleDef BlockDef = {
NULL,
NULL,
NULL,
NULL
};
NULL};
PyObject *
PyInit__block(void)
{
PyObject *PyInit__block(void) {
PyObject *m = PyModule_Create(&BlockDef);
if (m == NULL) {
return NULL;
@ -246,7 +240,8 @@ PyInit__block(void)
NoBlocksError = PyErr_NewException("_block.NoBlocksError", NULL, NULL);
PyModule_AddObject(m, "NoBlocksError", NoBlocksError);
DifferentBlockCountError = PyErr_NewException("_block.DifferentBlockCountError", NULL, NULL);
DifferentBlockCountError =
PyErr_NewException("_block.DifferentBlockCountError", NULL, NULL);
PyModule_AddObject(m, "DifferentBlockCountError", DifferentBlockCountError);
return m;

View File

@ -2,32 +2,22 @@
* Created On: 2010-01-31
* Copyright 2014 Hardcoded Software (http://www.hardcoded.net)
*
* This software is licensed under the "BSD" License as described in the "LICENSE" file,
* which should be included with this package. The terms are also available at
* http://www.hardcoded.net/licenses/bsd_license
**/
* This software is licensed under the "BSD" License as described in the
*"LICENSE" file, which should be included with this package. The terms are also
*available at http://www.hardcoded.net/licenses/bsd_license
**/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
/* It seems like MS VC defines min/max already */
#ifndef _MSC_VER
static int
max(int a, int b)
{
return b > a ? b : a;
}
static int max(int a, int b) { return b > a ? b : a; }
static int
min(int a, int b)
{
return b < a ? b : a;
}
static int min(int a, int b) { return b < a ? b : a; }
#endif
static PyObject*
getblock(PyObject *image, int width, int height)
{
static PyObject *getblock(PyObject *image, int width, int height) {
int pixel_count, red, green, blue, bytes_per_line;
PyObject *pred, *pgreen, *pblue;
PyObject *result;
@ -48,13 +38,13 @@ getblock(PyObject *image, int width, int height)
Py_DECREF(sipptr);
s = (char *)PyCapsule_GetPointer(bits_capsule, NULL);
Py_DECREF(bits_capsule);
/* Qt aligns all its lines on 32bit, which means that if the number of bytes per
* line for image is not divisible by 4, there's going to be crap inserted in "s"
* We have to take this into account when calculating offsets
/* Qt aligns all its lines on 32bit, which means that if the number of bytes
*per line for image is not divisible by 4, there's going to be crap
*inserted in "s" We have to take this into account when calculating offsets
**/
for (i=0; i<height; i++) {
for (i = 0; i < height; i++) {
int j;
for (j=0; j<width; j++) {
for (j = 0; j < width; j++) {
int offset;
unsigned char r, g, b;
@ -84,18 +74,18 @@ getblock(PyObject *image, int width, int height)
return result;
}
/* block_getblocks(QImage image, int block_count_per_side) -> [(int r, int g, int b), ...]
/* block_getblocks(QImage image, int block_count_per_side) -> [(int r, int g,
*int b), ...]
*
* Compute blocks out of `image`. Note the use of min/max when compes the time of computing widths
* and heights and positions. This is to cover the case where the width or height of the image is
* smaller than `block_count_per_side`. In these cases, blocks will be, of course, 1 pixel big. But
* also, because all compared block lists are required to be of the same size, any block that has
* no pixel to be assigned to will simply be assigned the last pixel. This is why we have
* min(..., height-block_height-1) and stuff like that.
**/
static PyObject*
block_getblocks(PyObject *self, PyObject *args)
{
* Compute blocks out of `image`. Note the use of min/max when compes the time
*of computing widths and heights and positions. This is to cover the case where
*the width or height of the image is smaller than `block_count_per_side`. In
*these cases, blocks will be, of course, 1 pixel big. But also, because all
*compared block lists are required to be of the same size, any block that has
* no pixel to be assigned to will simply be assigned the last pixel. This is
*why we have min(..., height-block_height-1) and stuff like that.
**/
static PyObject *block_getblocks(PyObject *self, PyObject *args) {
int block_count_per_side, width, height, block_width, block_height, ih;
PyObject *image;
PyObject *pi;
@ -119,21 +109,22 @@ block_getblocks(PyObject *self, PyObject *args)
block_width = max(width / block_count_per_side, 1);
block_height = max(height / block_count_per_side, 1);
result = PyList_New(block_count_per_side * block_count_per_side);
result = PyList_New((Py_ssize_t)block_count_per_side * block_count_per_side);
if (result == NULL) {
return NULL;
}
for (ih=0; ih<block_count_per_side; ih++) {
for (ih = 0; ih < block_count_per_side; ih++) {
int top, iw;
top = min(ih*block_height, height-block_height-1);
for (iw=0; iw<block_count_per_side; iw++) {
top = min(ih * block_height, height - block_height - 1);
for (iw = 0; iw < block_count_per_side; iw++) {
int left;
PyObject *pcrop;
PyObject *pblock;
left = min(iw*block_width, width-block_width-1);
pcrop = PyObject_CallMethod(image, "copy", "iiii", left, top, block_width, block_height);
left = min(iw * block_width, width - block_width - 1);
pcrop = PyObject_CallMethod(image, "copy", "iiii", left, top, block_width,
block_height);
if (pcrop == NULL) {
Py_DECREF(result);
return NULL;
@ -144,7 +135,7 @@ block_getblocks(PyObject *self, PyObject *args)
Py_DECREF(result);
return NULL;
}
PyList_SET_ITEM(result, ih*block_count_per_side+iw, pblock);
PyList_SET_ITEM(result, ih * block_count_per_side + iw, pblock);
}
}
@ -156,8 +147,7 @@ static PyMethodDef BlockMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef BlockDef = {
PyModuleDef_HEAD_INIT,
static struct PyModuleDef BlockDef = {PyModuleDef_HEAD_INIT,
"_block_qt",
NULL,
-1,
@ -165,12 +155,9 @@ static struct PyModuleDef BlockDef = {
NULL,
NULL,
NULL,
NULL
};
NULL};
PyObject *
PyInit__block_qt(void)
{
PyObject *PyInit__block_qt(void) {
PyObject *m = PyModule_Create(&BlockDef);
if (m == NULL) {
return NULL;