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

@@ -204,7 +204,7 @@ static bool CreateMainSurface(uint w, uint h)
_screen.dst_ptr = _allegro_screen->line[0];
/* Initialise the screen so we don't blit garbage to the screen */
memset(_screen.dst_ptr, 0, _screen.height * _screen.pitch);
memset(_screen.dst_ptr, 0, static_cast<size_t>(_screen.height) * _screen.pitch);
/* Set the mouse at the place where we expect it */
poll_mouse();

View File

@@ -139,7 +139,7 @@ const char *VideoDriver_Dedicated::Start(const StringList &parm)
this->UpdateAutoResolution();
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(_cur_resolution.width * _cur_resolution.height * (bpp / 8));
_dedicated_video_mem = (bpp == 0) ? nullptr : MallocT<byte>(static_cast<size_t>(_cur_resolution.width) * _cur_resolution.height * (bpp / 8));
_screen.width = _screen.pitch = _cur_resolution.width;
_screen.height = _cur_resolution.height;

View File

@@ -918,6 +918,7 @@ bool OpenGLBackend::Resize(int w, int h, bool force)
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
int pitch = Align(w, 4);
size_t line_pixel_count = static_cast<size_t>(pitch) * h;
_glViewport(0, 0, w, h);
@@ -928,27 +929,27 @@ bool OpenGLBackend::Resize(int w, int h, bool force)
_glDeleteBuffers(1, &this->vid_pbo);
_glGenBuffers(1, &this->vid_pbo);
_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, line_pixel_count * bpp / 8, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
} else {
/* Re-allocate video buffer texture and backing store. */
_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_DYNAMIC_DRAW);
_glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count * bpp / 8, nullptr, GL_DYNAMIC_DRAW);
}
if (bpp == 32) {
/* Initialize backing store alpha to opaque for 32bpp modes. */
Colour black(0, 0, 0);
if (_glClearBufferSubData != nullptr) {
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_RGBA8, 0, pitch * h * bpp / 8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &black.data);
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_RGBA8, 0, line_pixel_count * bpp / 8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &black.data);
} else {
ClearPixelBuffer<uint32>(pitch * h, black.data);
ClearPixelBuffer<uint32>(line_pixel_count, black.data);
}
} else if (bpp == 8) {
if (_glClearBufferSubData != nullptr) {
byte b = 0;
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
} else {
ClearPixelBuffer<byte>(pitch * h, 0);
ClearPixelBuffer<byte>(line_pixel_count, 0);
}
}
@@ -972,18 +973,18 @@ bool OpenGLBackend::Resize(int w, int h, bool force)
_glDeleteBuffers(1, &this->anim_pbo);
_glGenBuffers(1, &this->anim_pbo);
_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
_glBufferStorage(GL_PIXEL_UNPACK_BUFFER, line_pixel_count, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
} else {
_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_DYNAMIC_DRAW);
_glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count, nullptr, GL_DYNAMIC_DRAW);
}
/* Initialize buffer as 0 == no remap. */
if (_glClearBufferSubData != nullptr) {
byte b = 0;
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
_glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
} else {
ClearPixelBuffer<byte>(pitch * h, 0);
ClearPixelBuffer<byte>(line_pixel_count, 0);
}
_glBindTexture(GL_TEXTURE_2D, this->anim_texture);
@@ -1191,7 +1192,7 @@ uint8 *OpenGLBackend::GetAnimBuffer()
this->anim_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
} else if (this->anim_buffer == nullptr) {
_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
this->anim_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _screen.pitch * _screen.height, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
this->anim_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, static_cast<GLsizeiptr>(_screen.pitch) * _screen.height, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
}
return (uint8 *)this->anim_buffer;
@@ -1491,8 +1492,9 @@ void OpenGLSprite::Update(uint width, uint height, uint level, const SpriteLoade
if (this->tex[TEX_RGBA] != 0) {
/* Unpack pixel data */
Colour *rgba = buf_rgba.Allocate(width * height);
for (size_t i = 0; i < width * height; i++) {
size_t size = static_cast<size_t>(width) * height;
Colour *rgba = buf_rgba.Allocate(size);
for (size_t i = 0; i < size; i++) {
rgba[i].r = data[i].r;
rgba[i].g = data[i].g;
rgba[i].b = data[i].b;
@@ -1505,7 +1507,7 @@ void OpenGLSprite::Update(uint width, uint height, uint level, const SpriteLoade
if (this->tex[TEX_REMAP] != 0) {
/* Unpack and align pixel data. */
int pitch = Align(width, 4);
size_t pitch = Align(width, 4);
uint8 *pal = buf_pal.Allocate(pitch * height);
const SpriteLoader::CommonPixel *row = data;