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;
@ -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);
@ -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,7 +125,7 @@ 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;
}
@ -174,14 +171,14 @@ PyDoc_STRVAR(block_avgdiff_doc,
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;
}
@ -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,9 +2,9 @@
* 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
@ -12,22 +12,12 @@
/* 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,9 +38,9 @@ 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++) {
int j;
@ -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.
* 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)
{
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,7 +109,7 @@ 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;
}
@ -133,7 +123,8 @@ block_getblocks(PyObject *self, PyObject *args)
PyObject *pblock;
left = min(iw * block_width, width - block_width - 1);
pcrop = PyObject_CallMethod(image, "copy", "iiii", left, top, block_width, block_height);
pcrop = PyObject_CallMethod(image, "copy", "iiii", left, top, block_width,
block_height);
if (pcrop == NULL) {
Py_DECREF(result);
return NULL;
@ -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;