1
0
Fork 0

Codefix: Resolve CodeQL warnings by converting to size_t.

pull/12897/head
Peter Nelson 2024-08-10 17:26:52 +01:00 committed by Peter Nelson
parent d0d5c5c400
commit 367e508984
2 changed files with 27 additions and 41 deletions

View File

@ -80,17 +80,14 @@ static inline void SetStreamOffset(BmpBuffer *buffer, int offset)
*/ */
static inline bool BmpRead1(BmpBuffer *buffer, BmpInfo &info, BmpData &data) static inline bool BmpRead1(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint x, y, i;
uint8_t pad = GB(4 - info.width / 8, 0, 2); uint8_t pad = GB(4 - info.width / 8, 0, 2);
uint8_t *pixel_row; for (uint y = info.height; y > 0; y--) {
uint8_t b; uint x = 0;
for (y = info.height; y > 0; y--) { uint8_t *pixel_row = &data.bitmap[(y - 1) * static_cast<size_t>(info.width)];
x = 0;
pixel_row = &data.bitmap[(y - 1) * info.width];
while (x < info.width) { while (x < info.width) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
b = ReadByte(buffer); uint8_t b = ReadByte(buffer);
for (i = 8; i > 0; i--) { for (uint i = 8; i > 0; i--) {
if (x < info.width) *pixel_row++ = GB(b, i - 1, 1); if (x < info.width) *pixel_row++ = GB(b, i - 1, 1);
x++; x++;
} }
@ -107,16 +104,13 @@ static inline bool BmpRead1(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
*/ */
static inline bool BmpRead4(BmpBuffer *buffer, BmpInfo &info, BmpData &data) static inline bool BmpRead4(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint x, y;
uint8_t pad = GB(4 - info.width / 2, 0, 2); uint8_t pad = GB(4 - info.width / 2, 0, 2);
uint8_t *pixel_row; for (uint y = info.height; y > 0; y--) {
uint8_t b; uint x = 0;
for (y = info.height; y > 0; y--) { uint8_t *pixel_row = &data.bitmap[(y - 1) * static_cast<size_t>(info.width)];
x = 0;
pixel_row = &data.bitmap[(y - 1) * info.width];
while (x < info.width) { while (x < info.width) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
b = ReadByte(buffer); uint8_t b = ReadByte(buffer);
*pixel_row++ = GB(b, 4, 4); *pixel_row++ = GB(b, 4, 4);
x++; x++;
if (x < info.width) { if (x < info.width) {
@ -138,7 +132,7 @@ static inline bool BmpRead4Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint x = 0; uint x = 0;
uint y = info.height - 1; uint y = info.height - 1;
uint8_t *pixel = &data.bitmap[y * info.width]; uint8_t *pixel = &data.bitmap[y * static_cast<size_t>(info.width)];
while (y != 0 || x < info.width) { while (y != 0 || x < info.width) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
@ -149,7 +143,7 @@ static inline bool BmpRead4Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
case 0: // end of line case 0: // end of line
x = 0; x = 0;
if (y == 0) return false; if (y == 0) return false;
pixel = &data.bitmap[--y * info.width]; pixel = &data.bitmap[--y * static_cast<size_t>(info.width)];
break; break;
case 1: // end of bitmap case 1: // end of bitmap
@ -210,14 +204,11 @@ static inline bool BmpRead4Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
*/ */
static inline bool BmpRead8(BmpBuffer *buffer, BmpInfo &info, BmpData &data) static inline bool BmpRead8(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint i;
uint y;
uint8_t pad = GB(4 - info.width, 0, 2); uint8_t pad = GB(4 - info.width, 0, 2);
uint8_t *pixel; for (uint y = info.height; y > 0; y--) {
for (y = info.height; y > 0; y--) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
pixel = &data.bitmap[(y - 1) * info.width]; uint8_t *pixel = &data.bitmap[(y - 1) * static_cast<size_t>(info.width)];
for (i = 0; i < info.width; i++) *pixel++ = ReadByte(buffer); for (uint i = 0; i < info.width; i++) *pixel++ = ReadByte(buffer);
/* Padding for 32 bit align */ /* Padding for 32 bit align */
SkipBytes(buffer, pad); SkipBytes(buffer, pad);
} }
@ -231,7 +222,7 @@ static inline bool BmpRead8Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint x = 0; uint x = 0;
uint y = info.height - 1; uint y = info.height - 1;
uint8_t *pixel = &data.bitmap[y * info.width]; uint8_t *pixel = &data.bitmap[y * static_cast<size_t>(info.width)];
while (y != 0 || x < info.width) { while (y != 0 || x < info.width) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
@ -242,7 +233,7 @@ static inline bool BmpRead8Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
case 0: // end of line case 0: // end of line
x = 0; x = 0;
if (y == 0) return false; if (y == 0) return false;
pixel = &data.bitmap[--y * info.width]; pixel = &data.bitmap[--y * static_cast<size_t>(info.width)];
break; break;
case 1: // end of bitmap case 1: // end of bitmap
@ -258,7 +249,7 @@ static inline bool BmpRead8Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
x += dx; x += dx;
y -= dy; y -= dy;
pixel = &data.bitmap[y * info.width + x]; pixel = &data.bitmap[y * static_cast<size_t>(info.width) + x];
break; break;
} }
@ -291,12 +282,10 @@ static inline bool BmpRead8Rle(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
*/ */
static inline bool BmpRead24(BmpBuffer *buffer, BmpInfo &info, BmpData &data) static inline bool BmpRead24(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint x, y;
uint8_t pad = GB(4 - info.width * 3, 0, 2); uint8_t pad = GB(4 - info.width * 3, 0, 2);
uint8_t *pixel_row; for (uint y = info.height; y > 0; --y) {
for (y = info.height; y > 0; y--) { uint8_t *pixel_row = &data.bitmap[(y - 1) * static_cast<size_t>(info.width) * 3];
pixel_row = &data.bitmap[(y - 1) * info.width * 3]; for (uint x = 0; x < info.width; ++x) {
for (x = 0; x < info.width; x++) {
if (EndOfBuffer(buffer)) return false; // the file is shorter than expected if (EndOfBuffer(buffer)) return false; // the file is shorter than expected
*(pixel_row + 2) = ReadByte(buffer); // green *(pixel_row + 2) = ReadByte(buffer); // green
*(pixel_row + 1) = ReadByte(buffer); // blue *(pixel_row + 1) = ReadByte(buffer); // blue
@ -314,7 +303,6 @@ static inline bool BmpRead24(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
*/ */
bool BmpReadHeader(BmpBuffer *buffer, BmpInfo &info, BmpData &data) bool BmpReadHeader(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
{ {
uint32_t header_size;
info = {}; info = {};
/* Reading BMP header */ /* Reading BMP header */
@ -323,7 +311,7 @@ bool BmpReadHeader(BmpBuffer *buffer, BmpInfo &info, BmpData &data)
info.offset = ReadDword(buffer); info.offset = ReadDword(buffer);
/* Reading info header */ /* Reading info header */
header_size = ReadDword(buffer); uint32_t header_size = ReadDword(buffer);
if (header_size < 12) return false; // info header should be at least 12 bytes long if (header_size < 12) return false; // info header should be at least 12 bytes long
info.os2_bmp = (header_size == 12); // OS/2 1.x or windows 2.x info header is 12 bytes long info.os2_bmp = (header_size == 12); // OS/2 1.x or windows 2.x info header is 12 bytes long

View File

@ -208,15 +208,13 @@ static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, uint8_t **m
*/ */
static void ReadHeightmapBMPImageData(uint8_t *map, const BmpInfo &info, const BmpData &data) static void ReadHeightmapBMPImageData(uint8_t *map, const BmpInfo &info, const BmpData &data)
{ {
uint x, y;
uint8_t gray_palette[256]; uint8_t gray_palette[256];
if (!data.palette.empty()) { if (!data.palette.empty()) {
uint i;
bool all_gray = true; bool all_gray = true;
if (info.palette_size != 2) { if (info.palette_size != 2) {
for (i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) { for (uint i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) {
all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b; all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
gray_palette[i] = RGBToGrayscale(data.palette[i].r, data.palette[i].g, data.palette[i].b); gray_palette[i] = RGBToGrayscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
} }
@ -228,7 +226,7 @@ static void ReadHeightmapBMPImageData(uint8_t *map, const BmpInfo &info, const B
* level 1, etc. * level 1, etc.
*/ */
if (info.palette_size == 16 && !all_gray) { if (info.palette_size == 16 && !all_gray) {
for (i = 0; i < info.palette_size; i++) { for (uint i = 0; i < info.palette_size; i++) {
gray_palette[i] = 256 * i / info.palette_size; gray_palette[i] = 256 * i / info.palette_size;
} }
} }
@ -243,11 +241,11 @@ static void ReadHeightmapBMPImageData(uint8_t *map, const BmpInfo &info, const B
} }
/* Read the raw image data and convert in 8-bit grayscale */ /* Read the raw image data and convert in 8-bit grayscale */
for (y = 0; y < info.height; y++) { for (uint y = 0; y < info.height; y++) {
uint8_t *pixel = &map[y * info.width]; uint8_t *pixel = &map[y * static_cast<size_t>(info.width)];
const uint8_t *bitmap = &data.bitmap[y * info.width * (info.bpp == 24 ? 3 : 1)]; const uint8_t *bitmap = &data.bitmap[y * static_cast<size_t>(info.width) * (info.bpp == 24 ? 3 : 1)];
for (x = 0; x < info.width; x++) { for (uint x = 0; x < info.width; x++) {
if (info.bpp != 24) { if (info.bpp != 24) {
*pixel++ = gray_palette[*bitmap++]; *pixel++ = gray_palette[*bitmap++];
} else { } else {