1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2025-05-08 01:39:50 +00:00

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 * Created On: 2010-01-30
* Copyright 2014 Hardcoded Software (http://www.hardcoded.net) * Copyright 2014 Hardcoded Software (http://www.hardcoded.net)
* *
* This software is licensed under the "BSD" License as described in the "LICENSE" file, * This software is licensed under the "BSD" License as described in the
* which should be included with this package. The terms are also available at * "LICENSE" file, which should be included with this package. The terms are
* http://www.hardcoded.net/licenses/bsd_license * also available at http://www.hardcoded.net/licenses/bsd_license
*/ */
#include "common.h" #include "common.h"
@ -17,8 +17,7 @@ static PyObject *DifferentBlockCountError;
/* Returns a 3 sized tuple containing the mean color of 'image'. /* Returns a 3 sized tuple containing the mean color of 'image'.
* image: a PIL image or crop. * image: a PIL image or crop.
*/ */
static PyObject* getblock(PyObject *image) static PyObject *getblock(PyObject *image) {
{
int i, totr, totg, totb; int i, totr, totg, totb;
Py_ssize_t pixel_count; Py_ssize_t pixel_count;
PyObject *ppixels; PyObject *ppixels;
@ -65,8 +64,7 @@ static PyObject* getblock(PyObject *image)
/* Returns the difference between the first block and the second. /* Returns the difference between the first block and the second.
* It returns an absolute sum of the 3 differences (RGB). * 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; int r1, g1, b1, r2, b2, g2;
PyObject *pr, *pg, *pb; PyObject *pr, *pg, *pb;
pr = PySequence_ITEM(first, 0); 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\ necessarely cover square areas. The area covered by each block will be proportional to the image\n\
itself.\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; int block_count_per_side, width, height, block_width, block_height, ih;
PyObject *image; PyObject *image;
PyObject *pimage_size, *pwidth, *pheight; 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_width = max(width / block_count_per_side, 1);
block_height = max(height / 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) { if (result == NULL) {
return 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\ If the result surpasses limit, limit + 1 is returned, except if less than min_iterations\n\
iterations have been made in the blocks.\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; PyObject *first, *second;
int limit, min_iterations; int limit, min_iterations;
Py_ssize_t count; Py_ssize_t count;
int sum, i, result; 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; return NULL;
} }
@ -206,7 +203,8 @@ static PyObject* block_avgdiff(PyObject *self, PyObject *args)
sum += diff(item1, item2); sum += diff(item1, item2);
Py_DECREF(item1); Py_DECREF(item1);
Py_DECREF(item2); 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); return PyLong_FromLong(limit + 1);
} }
} }
@ -224,8 +222,7 @@ static PyMethodDef BlockMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
static struct PyModuleDef BlockDef = { static struct PyModuleDef BlockDef = {PyModuleDef_HEAD_INIT,
PyModuleDef_HEAD_INIT,
"_block", "_block",
NULL, NULL,
-1, -1,
@ -233,12 +230,9 @@ static struct PyModuleDef BlockDef = {
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL NULL};
};
PyObject * PyObject *PyInit__block(void) {
PyInit__block(void)
{
PyObject *m = PyModule_Create(&BlockDef); PyObject *m = PyModule_Create(&BlockDef);
if (m == NULL) { if (m == NULL) {
return NULL; return NULL;
@ -246,7 +240,8 @@ PyInit__block(void)
NoBlocksError = PyErr_NewException("_block.NoBlocksError", NULL, NULL); NoBlocksError = PyErr_NewException("_block.NoBlocksError", NULL, NULL);
PyModule_AddObject(m, "NoBlocksError", NoBlocksError); PyModule_AddObject(m, "NoBlocksError", NoBlocksError);
DifferentBlockCountError = PyErr_NewException("_block.DifferentBlockCountError", NULL, NULL); DifferentBlockCountError =
PyErr_NewException("_block.DifferentBlockCountError", NULL, NULL);
PyModule_AddObject(m, "DifferentBlockCountError", DifferentBlockCountError); PyModule_AddObject(m, "DifferentBlockCountError", DifferentBlockCountError);
return m; return m;

View File

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