diff --git a/include/support.h b/include/support.h index 7087027e..90fc1324 100644 --- a/include/support.h +++ b/include/support.h @@ -34,6 +34,11 @@ #define strncasecmp(a, b, n) _strnicmp(a, b, n) #endif +// Include a message in assert, similar to static_assert: +#define assertm(exp, msg) assert(((void)msg, exp)) +// Use (void) to silent unused warnings. +// https://en.cppreference.com/w/cpp/error/assert + /// Copy a string into C array /// /// This function copies string pointed by src to fixed-size buffer dst. diff --git a/src/dos/cdrom_image.cpp b/src/dos/cdrom_image.cpp index 7bd78562..4a5467b8 100644 --- a/src/dos/cdrom_image.cpp +++ b/src/dos/cdrom_image.cpp @@ -667,7 +667,7 @@ track_iter CDROM_Interface_Image::GetTrack(const int sector) if (lower_bound <= sector && sector < upper_bound) { break; } - track++; + ++track; lower_bound = upper_bound; } // If we made it here without breaking, then the track // wasn't found and the iterator is now the end() item. @@ -978,7 +978,7 @@ bool CDROM_Interface_Image::LoadCueSheet(char *cuefile) // ignored commands else if (command == "CDTEXTFILE" || command == "FLAGS" || command == "ISRC" || command == "PERFORMER" || command == "POSTGAP" || command == "REM" || - command == "SONGWRITER" || command == "TITLE" || command == "") { + command == "SONGWRITER" || command == "TITLE" || command.empty()) { success = true; } // failure diff --git a/src/dos/dos_mscdex.cpp b/src/dos/dos_mscdex.cpp index 233617d7..f18f2319 100644 --- a/src/dos/dos_mscdex.cpp +++ b/src/dos/dos_mscdex.cpp @@ -18,6 +18,7 @@ #include "dos_mscdex.h" +#include #include #include #include @@ -174,11 +175,10 @@ public: CMscdex::CMscdex() : numDrives(0), defaultBufSeg(0), + cdrom{nullptr}, rootDriverHeaderSeg(0) { memset(dinfo, 0, sizeof(dinfo)); - for (auto &drive : cdrom) - drive = nullptr; } CMscdex::~CMscdex() @@ -282,10 +282,11 @@ int CMscdex::AddDrive(Bit16u _drive, char* physicalPath, Bit8u& subUnit) if (rootDriverHeaderSeg==0) { - Bit16u driverSize = sizeof(DOS_DeviceHeader::sDeviceHeader) + 10; // 10 = Bytes for 3 callbacks + constexpr uint16_t driverSize = sizeof(DOS_DeviceHeader::sDeviceHeader) + 10; // 10 = Bytes for 3 callbacks // Create Device Header - Bit16u seg = DOS_GetMemory(driverSize/16+((driverSize%16)>0)); + static_assert((driverSize % 16) == 0, "should always be zero"); + Bit16u seg = DOS_GetMemory(driverSize / 16); DOS_DeviceHeader devHeader(PhysMake(seg,0)); devHeader.SetNextDeviceHeader (0xFFFFFFFF); devHeader.SetAttribute(0xc800); @@ -710,8 +711,7 @@ bool CMscdex::GetDirectoryEntry(Bit16u drive, bool copyFlag, PhysPt pathname, Ph LOG(LOG_MISC,LOG_WARN)("MSCDEX: GetDirEntry: Copyflag structure not entirely accurate maybe"); Bit8u readBuf[256]; Bit8u writeBuf[256]; - if (entryLength > 256) - return false; + assertm(entryLength <= 256, "entryLength should never exceed 256"); MEM_BlockRead( defBuffer+index, readBuf, entryLength ); writeBuf[0] = readBuf[1]; // 00h BYTE length of XAR in Logical Block Numbers memcpy( &writeBuf[1], &readBuf[0x2], 4); // 01h DWORD Logical Block Number of file start diff --git a/src/libs/decoders/SDL_sound.c b/src/libs/decoders/SDL_sound.c index 4dc30bb0..25f4cc1f 100644 --- a/src/libs/decoders/SDL_sound.c +++ b/src/libs/decoders/SDL_sound.c @@ -345,28 +345,24 @@ static Sound_Sample *alloc_sample(SDL_RWops *rw, Sound_AudioInfo *desired) * !!! FIXME: We're going to need to pool samples, since the mixer * !!! FIXME: might be allocating tons of these on a regular basis. */ - Sound_Sample *retval = malloc(sizeof (Sound_Sample)); + Sound_Sample *retval = NULL; + Sound_Sample *sample = malloc(sizeof (Sound_Sample)); Sound_SampleInternal *internal = malloc(sizeof (Sound_SampleInternal)); - if ((retval == NULL) || (internal == NULL)) - { + if (sample && internal) { + memset(sample, '\0', sizeof (Sound_Sample)); + memset(internal, '\0', sizeof (Sound_SampleInternal)); + if (desired != NULL) { + memcpy(&sample->desired, desired, sizeof (Sound_AudioInfo)); + } + internal->rw = rw; + sample->opaque = internal; + retval = sample; + } else { __Sound_SetError(ERR_OUT_OF_MEMORY); - if (retval) - free(retval); - if (internal) - free(internal); - - return(NULL); - } /* if */ - - memset(retval, '\0', sizeof (Sound_Sample)); - memset(internal, '\0', sizeof (Sound_SampleInternal)); - - if (desired != NULL) - memcpy(&retval->desired, desired, sizeof (Sound_AudioInfo)); - - internal->rw = rw; - retval->opaque = internal; - return(retval); + free(sample); + free(internal); + } + return retval; } /* alloc_sample */