Codechange: address CodeQL issue "Multiplication result converted to larger type" (#10306)

Most are very unlikely to ever be triggered in our codebase; two
stand out: linkgraph and money cheat. Those, potentially, could
wrap earlier than expected.
This commit is contained in:
Patric Stout
2023-01-02 21:30:02 +01:00
committed by GitHub
parent fcbe390353
commit 1fb101eabb
26 changed files with 74 additions and 66 deletions

View File

@@ -92,7 +92,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t f
if (num != 0) return WarnCorruptSprite(file, file_pos, __LINE__);
sprite->AllocateData(zoom_lvl, sprite->width * sprite->height);
sprite->AllocateData(zoom_lvl, static_cast<size_t>(sprite->width) * sprite->height);
/* Convert colour depth to pixel size. */
int bpp = 0;
@@ -168,13 +168,14 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, SpriteFile &file, size_t f
} while (!last_item);
}
} else {
if (dest_size < sprite->width * sprite->height * bpp) {
int64 sprite_size = static_cast<int64>(sprite->width) * sprite->height * bpp;
if (dest_size < sprite_size) {
return WarnCorruptSprite(file, file_pos, __LINE__);
}
if (dest_size > sprite->width * sprite->height * bpp) {
if (dest_size > sprite_size) {
static byte warning_level = 0;
Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite->width * sprite->height * bpp, file.GetSimplifiedFilename(), file_pos);
Debug(sprite, warning_level, "Ignoring {} unused extra bytes from the sprite from {} at position {}", dest_size - sprite_size, file.GetSimplifiedFilename(), file_pos);
warning_level = 6;
}