Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLOBAL: Bulk MSVC warning cleanup (Update) #6399

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

bpulliam
Copy link

@bpulliam bpulliam commented Jan 10, 2025

This PR builds on the work by elasota in #5128.

The fixes are broken up into multiple commits for clarity, but the summary is:

  • MULTIPLE: Add casts to prevent signed/unsigned conversion and mismatch warnings
  • IMGUI: Use an existing pattern to prevent redefinition of ARRAYSIZE when windows.h is included directly.
  • DIRECTOR,HYPNO,PRIVATE: Remove limit macros from flex skeleton to fix MSVC redefinition warnings
    A fix was checked into flex to fix this issue, but it has not been included in a public release yet. It will be in 2.6.5 whenever that is released. The macros in question are not used in the skeleton and the fix prevents their inclusion. They may be removed them from the generated code with zero impact.
  • DIRECTOR: Fix format warning/remove MSVC-specific
    In VS 2015, support was added for the %zX standard to support size_t in printf formats. The MSVC-specific code may be removed as it is no longer needed.
  • SAGA: Fixed a switch statement that can contain no case statements due to code inclusion/exclusion.
  • NANCY,COMMON: Fixed unary minus MSVC warnings
  • LUA: Fixed 32-bit/64-bit shift warning (MSVC)
  • FREESPACE: Fixed mismatch and eliminated instance where same calculation was done multiple times.

Remaining warnings (x64):
engines\tinsel\noir\spriter.cpp(376,11): warning C4146: unary minus operator applied to unsigned type, result still unsigned [...\dists\msvc\tinsel.vcxproj]
engines\tinsel\noir\spriter.cpp(380,11): warning C4146: unary minus operator applied to unsigned type, result still unsigned [...\dists\msvc\tinsel.vcxproj]
common\lua\ldo.cpp(141,3): warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable [...\dists\msvc\scummvm.vcxproj]

Remaining warnings (Win32):
engines\mtropolis\boot.cpp(62,43): warning C4121: 'MTropolis::Boot::Game': alignment of a member was sensitive to packing [...\dists\msvc\mtropolis.vcxproj]
engines\tinsel\noir\spriter.cpp(376,11): warning C4146: unary minus operator applied to unsigned type, result still unsigned [...\dists\msvc\tinsel.vcxproj]
engines\tinsel\noir\spriter.cpp(380,11): warning C4146: unary minus operator applied to unsigned type, result still unsigned [...\dists\msvc\tinsel.vcxproj]
engines\twp\squirrel\sqcompiler.cpp(174,12): warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable [...\dists\msvc\twp.vcxproj]
engines\twp\squirrel\sqstdrex.cpp(568,8): warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable [...\dists\msvc\twp.vcxproj]
engines\twp\syslib.cpp(899,27): warning C4309: 'argument': truncation of constant value [...\dists\msvc\twp.vcxproj]
engines\ultima\ultima4\game\item.h(50,20): warning C4121: 'Ultima::Ultima4::ItemLocation': alignment of a member was sensitive to packing [...\dists\msvc\ultima.vcxproj]

@@ -97,7 +97,7 @@ bool BaseImage::loadFile(const Common::String &filename) {
//
if (_filename.hasSuffix(".tga")) {
_paletteTga = new byte[_paletteCount * 3];
for (uint32 i = 0; i < _paletteCount * 3; i += 3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casting should be avoided

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be simpler to change i type to int as _paletteCount is promoted to int before being multiplied by 3.
This is guaranteed to not overflow.

Copy link
Member

@lephilousophe lephilousophe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fixes.

I have a bunch of remarks because several changes are either incorrect or hide some real problems in our code (which should be properly fixed).

@@ -107,8 +107,24 @@ extern "C" {
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif

// HACK: Windows.h defines its own ARRAYSIZE. However, we want
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARRAYSIZE is not used in this file.
It's simpler to undefine it after the inclusion of common/system.h
Please also update the patch file.

@@ -356,7 +356,7 @@ typedef struct Table {
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))


#define twoto(x) (1<<(x))
#define twoto(x) ((size_t)1<<(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look correct as twoto is also used with int variables.

@@ -53,7 +53,7 @@
#endif

/** Template method to return the absolute value of @p x. */
template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
template<typename T> inline T ABS(T x) { return (x >= 0) ? x : 0 - (T)x; } // 0 - (T)x is used instead of -x to silence MSVC C4146
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is not meant to be silenced.
Using ABS on an unsigned type is an error which should be fixed.
In fact the ABS call is just useless in this case: x will always be greater or equal to 0.

@@ -367,13 +367,13 @@ void Image::loadBitmap4(Graphics::ManagedSurface *surf, uint32 toffset, Common::
stream->skip(toffset >> 1);

if (highByte) {
for (uint i = 0; i < tw * th; i += 2) {
for (uint16 i = 0; i < tw * th; i += 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct.
Multiplication of tw by th will not be limited to a 16-bits integer.
Please cast tw to uint32 in the loop condition instead.

byte val = stream->readByte();
data[i + 0] |= val & 0xF0;
data[i + 1] |= (val & 0x0F) << 4;
}
} else {
for (uint i = 0; i < tw * th; i += 2) {
for (uint16 i = 0; i < tw * th; i += 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -123,7 +123,7 @@ bool CurlSocket::connect(const Common::String &url) {

size_t CurlSocket::send(const char *data, int len) {
if (!_socket)
return -1;
return (size_t)-1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't a u suffix silence the warning?

@@ -150,7 +150,7 @@ ADDetectedGame TinselMetaEngineDetection::fallbackDetect(const FileMap &allFiles
tmp.size = (int32)testFile.size();
tmp.md5 = computeStreamMD5AsString(testFile, _md5Bytes);
} else {
tmp.size = -1;
tmp.size = (uint)-1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use a u suffix?

@@ -164,7 +164,7 @@ bool MidiParser_M::processEvent(const EventInfo& info, bool fireEvents) {
}

void MidiParser_M::parseNextEvent(EventInfo &info) {
assert(_position._playPos - _tracks[0] < _trackLength);
assert((uint32)(_position._playPos - _tracks[0]) < _trackLength);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NMIError is this subtraction always positive?

@@ -109,6 +109,7 @@ void unpack_data(unsigned char *unpacked_data, unsigned char *buf, unsigned int
unsigned char *save_buf = NULL;
unsigned char *save_unp = NULL;
unsigned int cur_unpacked_data_size = 0x00;
unsigned int data_left = 0x00;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name seems incorrect.
This represents the data consumed: save_buf represents the start of the buffer and buf the current position.
Subtracting save_buf to buf equals to the current index of data (or the quantity consumed).

@@ -97,7 +97,7 @@ bool BaseImage::loadFile(const Common::String &filename) {
//
if (_filename.hasSuffix(".tga")) {
_paletteTga = new byte[_paletteCount * 3];
for (uint32 i = 0; i < _paletteCount * 3; i += 3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be simpler to change i type to int as _paletteCount is promoted to int before being multiplied by 3.
This is guaranteed to not overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants