Skip to content

Commit

Permalink
Fix CodeQL cpp/integer-multiplication-cast-to-long warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 10, 2024
1 parent 5ac56b1 commit 5c86716
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 37 deletions.
11 changes: 6 additions & 5 deletions src/interpolation.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ int msInterpolationDataset(mapObj *map, imageObj *image,
int l, p, s, c;
double weight = 1.0;
if (!values) { /* defer allocation until we effectively have a feature */
values = (float *)msSmallCalloc(im_width * im_height, sizeof(float));
xyz_values =
(float *)msSmallCalloc(im_width * im_height, sizeof(float));
values = (float *)msSmallCalloc(((size_t)im_width) * im_height,
sizeof(float));
xyz_values = (float *)msSmallCalloc(((size_t)im_width) * im_height,
sizeof(float));
}
if (layer->project)
msProjectShape(&layer->projection, &map->projection, &shape);
Expand Down Expand Up @@ -232,10 +233,10 @@ int msInterpolationDataset(mapObj *map, imageObj *image,

if (npoints > 0 && interpParams.expand_searchrect) {
iValues =
msSmallMalloc(image->width * image->height * sizeof(unsigned char));
msSmallMalloc(sizeof(unsigned char) * image->width * image->height);
} else {
iValues =
msSmallCalloc(1, image->width * image->height * sizeof(unsigned char));
msSmallCalloc(1, sizeof(unsigned char) * image->width * image->height);
}

if (npoints >
Expand Down
4 changes: 2 additions & 2 deletions src/kerneldensity.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "gdal.h"

static void gaussian_blur(float *values, int width, int height, int radius) {
float *tmp = (float *)msSmallMalloc(width * height * sizeof(float));
float *tmp = (float *)msSmallMalloc(sizeof(float) * width * height);
int length = radius * 2 + 1;
float *kernel = (float *)msSmallMalloc(length * sizeof(float));
float sigma = radius / 3.0;
Expand All @@ -46,7 +46,7 @@ static void gaussian_blur(float *values, int width, int height, int radius) {
float v = a * exp(-(x * x) / den);
kernel[i] = v;
}
memset(tmp, 0, width * height * sizeof(float));
memset(tmp, 0, sizeof(float) * width * height);

for (y = 0; y < height; y++) {
float *src_row = values + width * y;
Expand Down
12 changes: 6 additions & 6 deletions src/mapcairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,10 @@ int getRasterBufferCopyCairo(imageObj *img, rasterBufferObj *rb) {
rb->data.rgba.pixel_step = 4;
rb->width = cairo_image_surface_get_width(r->surface);
rb->height = cairo_image_surface_get_height(r->surface);
pb = (unsigned char *)malloc(rb->height * rb->data.rgba.row_step *
sizeof(unsigned char));
pb = (unsigned char *)malloc(sizeof(unsigned char) * rb->height *
rb->data.rgba.row_step);
memcpy(pb, cairo_image_surface_get_data(r->surface),
rb->height * rb->data.rgba.row_step);
((size_t)rb->height) * rb->data.rgba.row_step);
rb->data.rgba.pixels = pb;
rb->data.rgba.r = &(pb[2]);
rb->data.rgba.g = &(pb[1]);
Expand Down Expand Up @@ -950,8 +950,8 @@ int initializeRasterBufferCairo(rasterBufferObj *rb, int width, int height,
rb->height = height;
rb->data.rgba.pixel_step = 4;
rb->data.rgba.row_step = width * 4;
rb->data.rgba.pixels =
(unsigned char *)calloc(width * height * 4, sizeof(unsigned char));
rb->data.rgba.pixels = (unsigned char *)calloc(((size_t)width) * height * 4,
sizeof(unsigned char));
rb->data.rgba.r = &(rb->data.rgba.pixels[2]);
rb->data.rgba.g = &(rb->data.rgba.pixels[1]);
rb->data.rgba.b = &(rb->data.rgba.pixels[0]);
Expand Down Expand Up @@ -1113,7 +1113,7 @@ int msRenderRasterizedSVGSymbol(imageObj *img, double x, double y,
initializeRasterBufferCairo(svg_cache->pixmap_buffer, surface_w, surface_h,
0);
memcpy(svg_cache->pixmap_buffer->data.rgba.pixels, pb,
surface_w * surface_h * 4 * sizeof(unsigned char));
sizeof(unsigned char) * surface_w * surface_h * 4);
svg_cache->scale = style->scale;
svg_cache->rotation = style->rotation;
cairo_destroy(cr);
Expand Down
4 changes: 2 additions & 2 deletions src/mapcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,9 +1270,9 @@ int msCopyRasterBuffer(rasterBufferObj *dst, const rasterBufferObj *src) {
if (src->type == MS_BUFFER_BYTE_RGBA) {
dst->data.rgba = src->data.rgba;
dst->data.rgba.pixels =
msSmallMalloc(src->height * src->data.rgba.row_step);
msSmallMalloc(((size_t)src->height) * src->data.rgba.row_step);
memcpy(dst->data.rgba.pixels, src->data.rgba.pixels,
src->data.rgba.row_step * src->height);
((size_t)src->data.rgba.row_step) * src->height);
dst->data.rgba.r =
dst->data.rgba.pixels + (src->data.rgba.r - src->data.rgba.pixels);
dst->data.rgba.g =
Expand Down
9 changes: 5 additions & 4 deletions src/mapdrawgdal.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ int msDrawRasterLayerGDAL(mapObj *map, layerObj *layer, imageObj *image,
/*
* Allocate imagery buffers.
*/
pabyRaw1 = (unsigned char *)malloc(dst_xsize * dst_ysize * band_count);
pabyRaw1 =
(unsigned char *)malloc(((size_t)dst_xsize) * dst_ysize * band_count);
if (pabyRaw1 == NULL) {
msSetError(MS_MEMERR, "Allocating work image of size %dx%dx%d failed.",
"msDrawRasterLayerGDAL()", dst_xsize, dst_ysize, band_count);
Expand Down Expand Up @@ -710,8 +711,8 @@ int msDrawRasterLayerGDAL(mapObj *map, layerObj *layer, imageObj *image,

band_count++;

pabyRaw1 = (unsigned char *)realloc(pabyOrig,
dst_xsize * dst_ysize * band_count);
pabyRaw1 = (unsigned char *)realloc(pabyOrig, ((size_t)dst_xsize) *
dst_ysize * band_count);

if (pabyRaw1 == NULL) {
msSetError(MS_MEMERR, "Allocating work image of size %dx%dx%d failed.",
Expand Down Expand Up @@ -1779,7 +1780,7 @@ msDrawRasterLayerGDAL_RawMode(mapObj *map, layerObj *layer, imageObj *image,
/* -------------------------------------------------------------------- */
/* Allocate buffer, and read data into it. */
/* -------------------------------------------------------------------- */
pBuffer = malloc(dst_xsize * dst_ysize * image->format->bands *
pBuffer = malloc(((size_t)dst_xsize) * dst_ysize * image->format->bands *
(GDALGetDataTypeSize(eDataType) / 8));
if (pBuffer == NULL) {
msSetError(MS_MEMERR, "Allocating work image of size %dx%d failed.",
Expand Down
10 changes: 5 additions & 5 deletions src/mapimageio.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ int saveAsJPEG(mapObj *map, rasterBufferObj *rb, streamInfo *info,

jpeg_start_compress(&cinfo, TRUE);
rowdata =
(JSAMPLE *)malloc(rb->width * cinfo.input_components * sizeof(JSAMPLE));
(JSAMPLE *)malloc(sizeof(JSAMPLE) * rb->width * cinfo.input_components);

for (row = 0; row < rb->height; row++) {
JSAMPLE *pixptr = rowdata;
Expand Down Expand Up @@ -476,7 +476,7 @@ int saveAsPNG(mapObj *map, rasterBufferObj *rb, streamInfo *info,
qrb.width = rb->width;
qrb.height = rb->height;
qrb.data.palette.pixels =
(unsigned char *)malloc(qrb.width * qrb.height * sizeof(unsigned char));
(unsigned char *)malloc(sizeof(unsigned char) * qrb.width * qrb.height);
qrb.data.palette.scaling_maxval = 255;
int ret;
if (force_pc256) {
Expand Down Expand Up @@ -667,7 +667,7 @@ int readPNG(char *path, rasterBufferObj *rb) {
rb->height = height;
rb->type = MS_BUFFER_BYTE_RGBA;
rb->data.rgba.pixels =
(unsigned char *)malloc(width * height * 4 * sizeof(unsigned char));
(unsigned char *)malloc(sizeof(unsigned char) * width * height * 4);
row_pointers = (unsigned char **)malloc(height * sizeof(unsigned char *));
rb->data.rgba.pixel_step = 4;
rb->data.rgba.row_step = width * 4;
Expand Down Expand Up @@ -897,8 +897,8 @@ int readGIF(char *path, rasterBufferObj *rb) {
rb->height = image->SHeight;
rb->data.rgba.row_step = rb->width * 4;
rb->data.rgba.pixel_step = 4;
rb->data.rgba.pixels = (unsigned char *)malloc(rb->width * rb->height * 4 *
sizeof(unsigned char));
rb->data.rgba.pixels = (unsigned char *)malloc(sizeof(unsigned char) *
rb->width * rb->height * 4);
b = rb->data.rgba.b = &rb->data.rgba.pixels[0];
g = rb->data.rgba.g = &rb->data.rgba.pixels[1];
r = rb->data.rgba.r = &rb->data.rgba.pixels[2];
Expand Down
4 changes: 2 additions & 2 deletions src/maplegend.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ static int msDrawGradientSymbol(rendererVTableObj *renderer,
rb->height = height;
rb->data.rgba.row_step = rb->width * 4;
rb->data.rgba.pixel_step = 4;
rb->data.rgba.pixels = (unsigned char *)malloc(rb->width * rb->height * 4 *
sizeof(unsigned char));
rb->data.rgba.pixels = (unsigned char *)malloc(sizeof(unsigned char) *
rb->width * rb->height * 4);
b = rb->data.rgba.b = &rb->data.rgba.pixels[0];
g = rb->data.rgba.g = &rb->data.rgba.pixels[1];
r = rb->data.rgba.r = &rb->data.rgba.pixels[2];
Expand Down
4 changes: 2 additions & 2 deletions src/mappostgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ static void wkbSkipGeometry(wkbObj *w) {
case WKB_CIRCULARSTRING:
case WKB_LINESTRING: {
const int npoints = wkbReadInt(w);
w->ptr += npoints * nCoordDim * sizeof(double);
w->ptr += sizeof(double) * npoints * nCoordDim;
break;
}
case WKB_POLYGON: {
Expand All @@ -357,7 +357,7 @@ static void wkbSkipGeometry(wkbObj *w) {
return;
for (int i = 0; i < nrings; i++) {
const int npoints = wkbReadInt(w);
w->ptr += npoints * nCoordDim * sizeof(double);
w->ptr += sizeof(double) * npoints * nCoordDim;
}
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/maprasterquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ static void msRasterQueryAddPixel(layerObj *layer, pointObj *location,
rlinfo->qc_y_reproj =
(double *)msSmallCalloc(sizeof(double), rlinfo->query_alloc_max);
rlinfo->qc_values = (float *)msSmallCalloc(
sizeof(float), rlinfo->query_alloc_max * rlinfo->band_count);
sizeof(float),
((size_t)rlinfo->query_alloc_max) * rlinfo->band_count);
rlinfo->qc_red =
(int *)msSmallCalloc(sizeof(int), rlinfo->query_alloc_max);
rlinfo->qc_green =
Expand Down Expand Up @@ -503,8 +504,8 @@ static int msRasterQueryByRectLow(mapObj *map, layerObj *layer,
/* band in the file. Later we will deal with the various band */
/* selection criteria. */
/* -------------------------------------------------------------------- */
pafRaster =
(float *)calloc(sizeof(float), nWinXSize * nWinYSize * nBandCount);
pafRaster = (float *)calloc(sizeof(float),
((size_t)nWinXSize) * nWinYSize * nBandCount);
MS_CHECK_ALLOC(pafRaster, sizeof(float) * nWinXSize * nWinYSize * nBandCount,
-1);

Expand Down
2 changes: 1 addition & 1 deletion src/maputfgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static imageObj *utfgridCreateImage(int width, int height,
r->width = width / r->utfresolution;
r->height = height / r->utfresolution;

r->buffer.resize(r->width * r->height);
r->buffer.resize(static_cast<size_t>(r->width) * r->height);

/* AGG specific operations */
r->m_rendering_buffer.attach(&r->buffer[0], r->width, r->height, r->width);
Expand Down
10 changes: 5 additions & 5 deletions src/maputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1767,14 +1767,14 @@ imageObj *msImageCreate(int width, int height, outputFormatObj *format,
}

if (format->imagemode == MS_IMAGEMODE_INT16)
image->img.raw_16bit =
(short *)msSmallCalloc(sizeof(short), width * height * format->bands);
image->img.raw_16bit = (short *)msSmallCalloc(
sizeof(short), ((size_t)width) * height * format->bands);
else if (format->imagemode == MS_IMAGEMODE_FLOAT32)
image->img.raw_float =
(float *)msSmallCalloc(sizeof(float), width * height * format->bands);
image->img.raw_float = (float *)msSmallCalloc(
sizeof(float), ((size_t)width) * height * format->bands);
else if (format->imagemode == MS_IMAGEMODE_BYTE)
image->img.raw_byte = (unsigned char *)msSmallCalloc(
sizeof(unsigned char), width * height * format->bands);
sizeof(unsigned char), ((size_t)width) * height * format->bands);

if (image->img.raw_16bit == NULL) {
msFree(image);
Expand Down

0 comments on commit 5c86716

Please sign in to comment.