From 4080eef6a709390a5355d9bb1e7706e70117bf77 Mon Sep 17 00:00:00 2001 From: Ari Sundholm Date: Sat, 9 Sep 2017 14:10:56 +0300 Subject: [PATCH 1/3] gtk, unix: fixes to unbreak build and loading ROMs --- gtk/src/gtk_file.cpp | 36 ++++++++++++++++++++++++------------ msu1.cpp | 2 +- unix/unix.cpp | 34 ++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/gtk/src/gtk_file.cpp b/gtk/src/gtk_file.cpp index b6607f75..2722719d 100644 --- a/gtk/src/gtk_file.cpp +++ b/gtk/src/gtk_file.cpp @@ -33,7 +33,8 @@ _splitpath (const char *path, char *drive, char *dir, char *fname, char *ext) char *slash = strrchr ((char *) path, SLASH_CHAR); char *dot = strrchr ((char *) path, '.'); - *drive = '\0'; + if (drive) + *drive = '\0'; if (dot && slash && dot < slash) { @@ -42,33 +43,44 @@ _splitpath (const char *path, char *drive, char *dir, char *fname, char *ext) if (!slash) { - *dir = '\0'; - strcpy (fname, path); + if (dir) + *dir = '\0'; + if (fname) + strcpy (fname, path); if (dot) { - fname[dot - path] = '\0'; - strcpy (ext, dot + 1); + if (fname) + fname[dot - path] = '\0'; + if (ext) + strcpy (ext, dot + 1); } else { - *ext = '\0'; + if (ext) + *ext = '\0'; } } else { - strcpy (dir, path); - dir[slash - path] = '\0'; - strcpy (fname, slash + 1); + if (dir) { + strcpy (dir, path); + dir[slash - path] = '\0'; + } + if (fname) + strcpy (fname, slash + 1); if (dot) { - fname[(dot - slash) - 1] = '\0'; - strcpy (ext, dot + 1); + if (fname) + fname[(dot - slash) - 1] = '\0'; + if (ext) + strcpy (ext, dot + 1); } else { - *ext = '\0'; + if (ext) + *ext = '\0'; } } diff --git a/msu1.cpp b/msu1.cpp index bf0a96a1..341f5226 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -388,7 +388,7 @@ bool S9xMSU1ROMExists(void) } #ifdef UNZIP_SUPPORT char ext[_MAX_EXT + 1]; - _splitpath(Memory.ROMFilename, nullptr, nullptr, nullptr, ext); + _splitpath(Memory.ROMFilename, NULL, NULL, NULL, ext); if (!strcasecmp(ext, ".msu1")) return true; diff --git a/unix/unix.cpp b/unix/unix.cpp index b44e0617..89360e70 100644 --- a/unix/unix.cpp +++ b/unix/unix.cpp @@ -354,7 +354,8 @@ static void ReadJoysticks (void); void _splitpath (const char *path, char *drive, char *dir, char *fname, char *ext) { - *drive = 0; + if (drive) + *drive = 0; const char *slash = strrchr(path, SLASH_CHAR), *dot = strrchr(path, '.'); @@ -364,31 +365,40 @@ void _splitpath (const char *path, char *drive, char *dir, char *fname, char *ex if (!slash) { - *dir = 0; + if (dir) + *dir = 0; - strcpy(fname, path); + if (fname) + strcpy(fname, path); if (dot) { - fname[dot - path] = 0; - strcpy(ext, dot + 1); + if (fname) + fname[dot - path] = 0; + if (ext) + strcpy(ext, dot + 1); } - else + else if (ext) *ext = 0; } else { - strcpy(dir, path); - dir[slash - path] = 0; + if (dir) { + strcpy(dir, path); + dir[slash - path] = 0; + } - strcpy(fname, slash + 1); + if (fname) + strcpy(fname, slash + 1); if (dot) { - fname[dot - slash - 1] = 0; - strcpy(ext, dot + 1); + if (fname) + fname[dot - slash - 1] = 0; + if (ext) + strcpy(ext, dot + 1); } - else + else if (ext) *ext = 0; } } From 60aabb91d71c51c3c094b1964f75fc71849ec295 Mon Sep 17 00:00:00 2001 From: Ari Sundholm Date: Sat, 9 Sep 2017 14:12:33 +0300 Subject: [PATCH 2/3] Fix some undefined behavior The destructor of Resampler needs to be virtual, as it is subclassed and pointers to objects subclassed from it are being deleted. The issue in controls.cpp is that the loop ends up reading past the end of an array. The small rewrite of the loop also makes it more readable. In memmap.cpp, there is an assignment statement of the following form: a[i++] = b[i]; It is undefined what i's value should be in b[i], so this was made explicit. --- apu/resampler.h | 2 +- controls.cpp | 6 ++++-- memmap.cpp | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/apu/resampler.h b/apu/resampler.h index f4b09c4a..4f8cf6f6 100644 --- a/apu/resampler.h +++ b/apu/resampler.h @@ -17,7 +17,7 @@ class Resampler : public ring_buffer { } - ~Resampler () + virtual ~Resampler () { } diff --git a/controls.cpp b/controls.cpp index 32c09754..cca9f85f 100644 --- a/controls.cpp +++ b/controls.cpp @@ -2879,8 +2879,9 @@ void S9xSetJoypadLatch (bool latch) switch (i = curcontrollers[n]) { case MP5: - for (int j = 0, k = mp5[n].pads[j]; j < 4; k = mp5[n].pads[++j]) + for (int j = 0, k; j < 4; ++j) { + k = mp5[n].pads[j]; if (k == NONE) continue; do_polling(k); @@ -3170,8 +3171,9 @@ void S9xControlEOF (void) switch (i = curcontrollers[n]) { case MP5: - for (j = 0, i = mp5[n].pads[j]; j < 4; i = mp5[n].pads[++j]) + for (j = 0; j < 4; ++j) { + i = mp5[n].pads[j]; if (i == NONE) continue; diff --git a/memmap.cpp b/memmap.cpp index 60d38986..513db49e 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -4054,7 +4054,10 @@ static bool8 ReadBPSPatch (Stream *r, long, int32 &rom_size) switch((int)mode) { case SourceRead: - while(length--) patched_rom[outputOffset++] = Memory.ROM[outputOffset]; + while(length--) { + patched_rom[outputOffset] = Memory.ROM[outputOffset]; + outputOffset++; + } break; case TargetRead: while(length--) patched_rom[outputOffset++] = data[addr++]; From d7394bc7b913163e5079f503b3f9e622f06a15d7 Mon Sep 17 00:00:00 2001 From: Ari Sundholm Date: Sat, 9 Sep 2017 14:17:55 +0300 Subject: [PATCH 3/3] Fix some build warnings generated by g++ --- apu/bapu/smp/core.cpp | 4 ++-- loadzip.cpp | 4 ++-- msu1.cpp | 2 +- msu1.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apu/bapu/smp/core.cpp b/apu/bapu/smp/core.cpp index 5303bed7..b46041b4 100644 --- a/apu/bapu/smp/core.cpp +++ b/apu/bapu/smp/core.cpp @@ -51,8 +51,8 @@ void SMP::op_write(uint16 addr, uint8 data) { void SMP::op_step() { #define op_readpc() op_read(regs.pc++) - #define op_readdp(addr) op_read((regs.p.p << 8) + (addr & 0xff)) - #define op_writedp(addr, data) op_write((regs.p.p << 8) + (addr & 0xff), data) + #define op_readdp(addr) op_read((regs.p.p << 8) + ((addr) & 0xff)) + #define op_writedp(addr, data) op_write((regs.p.p << 8) + ((addr) & 0xff), data) #define op_readaddr(addr) op_read(addr) #define op_writeaddr(addr, data) op_write(addr, data) #define op_readstack() op_read(0x0100 | ++regs.sp) diff --git a/loadzip.cpp b/loadzip.cpp index 840da42e..56e65b67 100644 --- a/loadzip.cpp +++ b/loadzip.cpp @@ -270,7 +270,7 @@ bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer) uint8 *ptr = buffer; bool8 more = FALSE; - unzLocateFile(file, filename, NULL); + unzLocateFile(file, filename, 0); unzGetCurrentFileInfo(file, &info, filename, 128, NULL, 0, NULL, 0); if (unzOpenCurrentFile(file) != UNZ_OK) @@ -330,7 +330,7 @@ bool8 LoadZip (const char *zipname, uint32 *TotalFileSize, uint8 *buffer) if (more) { - if (unzLocateFile(file, filename, NULL) != UNZ_OK || + if (unzLocateFile(file, filename, 0) != UNZ_OK || unzGetCurrentFileInfo(file, &info, filename, 128, NULL, 0, NULL, 0) != UNZ_OK || unzOpenCurrentFile(file) != UNZ_OK) break; diff --git a/msu1.cpp b/msu1.cpp index 341f5226..f857000d 100644 --- a/msu1.cpp +++ b/msu1.cpp @@ -240,7 +240,7 @@ static int unzFindExtension(unzFile &file, const char *ext, bool restart = TRUE, } #endif -STREAM S9xMSU1OpenFile(char *msu_ext, bool skip_unpacked) +STREAM S9xMSU1OpenFile(const char *msu_ext, bool skip_unpacked) { const char *filename = S9xGetFilename(msu_ext, ROMFILENAME_DIR); STREAM file = 0; diff --git a/msu1.h b/msu1.h index e51739b0..1e6ea15f 100644 --- a/msu1.h +++ b/msu1.h @@ -229,7 +229,7 @@ extern struct SMSU1 MSU1; void S9xResetMSU(void); void S9xMSU1Init(void); bool S9xMSU1ROMExists(void); -STREAM S9xMSU1OpenFile(char *msu_ext, bool skip_unpacked = FALSE); +STREAM S9xMSU1OpenFile(const char *msu_ext, bool skip_unpacked = FALSE); void S9xMSU1Init(void); void S9xMSU1Generate(size_t sample_count); uint8 S9xMSU1ReadPort(uint8 port);