From 026f9eaa1edde2dd1361f903fd3f87f994112ec5 Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Tue, 27 Sep 2016 15:25:37 -0500 Subject: [PATCH] Fix some possible problems from cppcheck. Everything else looks fine. --- apu/apu.cpp | 7 +++++-- apu/resampler.h | 12 +++++------ apu/ring_buffer.h | 6 +++--- bsx.cpp | 6 ++---- c4.cpp | 1 - conffile.cpp | 2 +- controls.cpp | 3 +-- dsp3.cpp | 2 +- fxinst.cpp | 6 +++--- getset.h | 3 +-- gtk/configure.ac | 2 +- gtk/src/gtk_binding.cpp | 21 ++++++++++--------- gtk/src/gtk_display_driver_xv.cpp | 2 +- gtk/src/gtk_sound_driver_alsa.cpp | 10 +++++---- gtk/src/sourcify.c | 35 +++++++++++++++++-------------- movie.cpp | 6 ++++++ netplay.cpp | 22 +++++++++---------- server.cpp | 6 +++--- unix/x11.cpp | 2 +- 19 files changed, 82 insertions(+), 72 deletions(-) diff --git a/apu/apu.cpp b/apu/apu.cpp index b7d1b3d4..ec967a42 100644 --- a/apu/apu.cpp +++ b/apu/apu.cpp @@ -746,7 +746,6 @@ void S9xAPULoadBlarggState(uint8 *oldblock) spc::reference_time = SNES::get_le32(ptr); ptr += sizeof(int32); spc::remainder = SNES::get_le32(ptr); - ptr += sizeof(int32); // blargg stores CPUIx in regs_in memcpy (SNES::cpu.registers, regs_in + 4, 4); @@ -766,8 +765,12 @@ bool8 S9xSPCDump (const char *filename) SNES::smp.save_spc (buf); - if ((ignore = fwrite(buf, SPC_FILE_SIZE, 1, fs)) <= 0) + ignore = fwrite (buf, SPC_FILE_SIZE, 1, fs); + + if (ignore == 0) + { fprintf (stderr, "Couldn't write file %s.\n", filename); + } fclose(fs); diff --git a/apu/resampler.h b/apu/resampler.h index 2e62addb..f4b09c4a 100644 --- a/apu/resampler.h +++ b/apu/resampler.h @@ -12,7 +12,7 @@ class Resampler : public ring_buffer virtual void time_ratio (double) = 0; virtual void read (short *, int) = 0; virtual int avail (void) = 0; - + Resampler (int num_samples) : ring_buffer (num_samples << 1) { } @@ -33,19 +33,19 @@ class Resampler : public ring_buffer } inline int - space_empty (void) + space_empty (void) const { return buffer_size - size; } - + inline int - space_filled (void) + space_filled (void) const { return size; } - + inline int - max_write (void) + max_write (void) const { return space_empty () >> 1; } diff --git a/apu/ring_buffer.h b/apu/ring_buffer.h index b743c471..eb8cbda9 100644 --- a/apu/ring_buffer.h +++ b/apu/ring_buffer.h @@ -69,13 +69,13 @@ public: } inline int - space_empty (void) + space_empty (void) const { return buffer_size - size; } inline int - space_filled (void) + space_filled (void) const { return size; } @@ -96,7 +96,7 @@ public: buffer = new unsigned char[buffer_size]; memset (buffer, 0, this->buffer_size); - size = 0; + this->size = 0; start = 0; } diff --git a/bsx.cpp b/bsx.cpp index 88f27dc3..f90d711d 100644 --- a/bsx.cpp +++ b/bsx.cpp @@ -358,12 +358,10 @@ static void BSX_Map_MMC (void) static void BSX_Map_FlashIO (void) { - int c; - if (BSX.MMC[0x0C] || BSX.MMC[0x0D]) { // Bank C0:0000, 2AAA, 5555, FF00-FF1F - for (c = 0; c < 16; c++) + for (int c = 0; c < 16; c++) { Map[c + 0xC00] = (uint8 *) MAP_BSX; BlockIsRAM[c + 0xC00] = TRUE; @@ -833,7 +831,7 @@ uint8 S9xGetBSXPPU (uint16 address) // Data register? (r/w) case 0x2192: - t = BSX.PPU[0x2192 - BSXPPUBASE]; + // t = BSX.PPU[0x2192 - BSXPPUBASE]; // test t = BSX.test2192[BSX.out_index++]; diff --git a/c4.cpp b/c4.cpp index 92d843ff..9fd0c065 100644 --- a/c4.cpp +++ b/c4.cpp @@ -295,7 +295,6 @@ void C4Op1F (void) { tanval = (double) C41FYVal / C41FXVal; C41FAngleRes = (int16) (atan(tanval) / (C4_PI * 2) * 512); - C41FAngleRes = C41FAngleRes; if (C41FXVal< 0) C41FAngleRes += 0x100; C41FAngleRes &= 0x1FF; diff --git a/conffile.cpp b/conffile.cpp index b6afe379..627e58dc 100644 --- a/conffile.cpp +++ b/conffile.cpp @@ -392,7 +392,7 @@ bool ConfigFile::SaveTo(const char *filename){ if(ferror(fp)) { - fp = fp; + printf ("Error writing config file %s\n", filename); } fclose(fp); diff --git a/controls.cpp b/controls.cpp index 714bb206..9731f453 100644 --- a/controls.cpp +++ b/controls.cpp @@ -2051,7 +2051,6 @@ void S9xApplyCommand (s9xcommand_t cmd, int16 data1, int16 data2) { uint16 r, s, t, st; - s = t = st = 0; r = cmd.button.joypad.buttons; st = r & joypad[cmd.button.joypad.idx].togglestick & joypad[cmd.button.joypad.idx].toggleturbo; r ^= st; @@ -2123,7 +2122,7 @@ void S9xApplyCommand (s9xcommand_t cmd, int16 data1, int16 data2) if (data1) mouse[cmd.button.mouse.idx].buttons |= i; - else + else mouse[cmd.button.mouse.idx].buttons &= ~i; return; diff --git a/dsp3.cpp b/dsp3.cpp index a4d0c96b..f447d288 100644 --- a/dsp3.cpp +++ b/dsp3.cpp @@ -1140,7 +1140,7 @@ static void DSP3_OP10 (void) DSP3_Reset(); else // absorb 2 bytes - DSP3.DR = DSP3.DR; + DSP3.DR = DSP3.DR; // FIXME? } /* diff --git a/fxinst.cpp b/fxinst.cpp index c67bb6e4..dffcd960 100644 --- a/fxinst.cpp +++ b/fxinst.cpp @@ -893,7 +893,7 @@ static void fx_plot_2bit (void) #endif if (GSU.vPlotOptionReg & 0x02) - c = (x ^ y) & 1 ? (uint8) (GSU.vColorReg >> 4) : (uint8) GSU.vColorReg; + c = ((x ^ y) & 1) ? (uint8) (GSU.vColorReg >> 4) : (uint8) GSU.vColorReg; else c = (uint8) GSU.vColorReg; @@ -957,7 +957,7 @@ static void fx_plot_4bit (void) #endif if (GSU.vPlotOptionReg & 0x02) - c = (x ^ y) & 1 ? (uint8) (GSU.vColorReg >> 4) : (uint8) GSU.vColorReg; + c = ((x ^ y) & 1) ? (uint8) (GSU.vColorReg >> 4) : (uint8) GSU.vColorReg; else c = (uint8) GSU.vColorReg; @@ -1172,7 +1172,7 @@ static void fx_cmode (void) { GSU.vPlotOptionReg = SREG; - if (GSU.vPlotOptionReg & 0x10) + if (GSU.vPlotOptionReg & 0x10) GSU.vScreenHeight = 256; // OBJ Mode (for drawing into sprites) else GSU.vScreenHeight = GSU.vScreenRealHeight; diff --git a/getset.h b/getset.h index b52f8987..2ba5b29b 100644 --- a/getset.h +++ b/getset.h @@ -873,8 +873,7 @@ inline void S9xSetPCBase (uint32 Address) Registers.PBPC = Address & 0xffffff; ICPU.ShiftedPB = Address & 0xff0000; - int block; - uint8 *GetAddress = Memory.Map[block = ((Address & 0xffffff) >> MEMMAP_SHIFT)]; + uint8 *GetAddress = Memory.Map[(int)((Address & 0xffffff) >> MEMMAP_SHIFT)]; CPU.MemSpeed = memory_speed(Address); CPU.MemSpeedx2 = CPU.MemSpeed << 1; diff --git a/gtk/configure.ac b/gtk/configure.ac index d2b899ec..16772f6f 100644 --- a/gtk/configure.ac +++ b/gtk/configure.ac @@ -7,7 +7,7 @@ if test -z "$CXXFLAGS"; then CXXFLAGS="$CFLAGS" fi -EXTRA_FLAGS="-Wall -W -pedantic -Wno-unused-parameter" +EXTRA_FLAGS="-Wall -W -pedantic -Wno-unused-parameter -Wno-unused-but-set-variable" CFLAGS="$CFLAGS $EXTRA_FLAGS" CXXFLAGS="$CXXFLAGS $EXTRA_FLAGS" diff --git a/gtk/src/gtk_binding.cpp b/gtk/src/gtk_binding.cpp index 4a0dc19d..f89658d3 100644 --- a/gtk/src/gtk_binding.cpp +++ b/gtk/src/gtk_binding.cpp @@ -154,9 +154,9 @@ Binding::get_axis (void) GdkModifierType Binding::get_gdk_modifiers (void) { - return (GdkModifierType) ((BINDING_CTRL & value ? GDK_CONTROL_MASK : 0) | - (BINDING_ALT & value ? GDK_MOD1_MASK : 0) | - (BINDING_SHIFT & value ? GDK_SHIFT_MASK : 0)); + return (GdkModifierType) (((BINDING_CTRL & value) ? GDK_CONTROL_MASK : 0) | + ((BINDING_ALT & value) ? GDK_MOD1_MASK : 0) | + ((BINDING_SHIFT & value) ? GDK_SHIFT_MASK : 0)); } bool @@ -192,9 +192,10 @@ Binding::to_string (char *str) else { + memset (buf, 0, 256); strncpy (buf, keyval_name, - 256); + 255); } while ((c = strstr (buf, "_"))) @@ -203,9 +204,9 @@ Binding::to_string (char *str) } sprintf (str, _("Keyboard %s%s%s%s"), - value & BINDING_SHIFT ? "Shift+" : "", - value & BINDING_CTRL ? "Ctrl+" : "", - value & BINDING_ALT ? "Alt+" : "", + (value & BINDING_SHIFT) ? "Shift+" : "", + (value & BINDING_CTRL) ? "Ctrl+" : "", + (value & BINDING_ALT) ? "Alt+" : "", buf); } @@ -213,14 +214,14 @@ Binding::to_string (char *str) { if ((get_key ()) >= 512) sprintf (buf, - _("Axis #%d %s %d%%"), + _("Axis #%u %s %u%%"), get_axis (), is_positive () ? "+" : "-", get_threshold ()); else - sprintf (buf, _("Button %d"), get_key ()); + sprintf (buf, _("Button %u"), get_key ()); - sprintf (str, _("Joystick %d %s"), get_device (), buf); + sprintf (str, _("Joystick %u %s"), get_device (), buf); } else diff --git a/gtk/src/gtk_display_driver_xv.cpp b/gtk/src/gtk_display_driver_xv.cpp index c512dfc8..03979afb 100644 --- a/gtk/src/gtk_display_driver_xv.cpp +++ b/gtk/src/gtk_display_driver_xv.cpp @@ -314,7 +314,7 @@ S9xXVDisplayDriver::init (void) { if (!strcmp (port_attr[i].name, "XV_AUTOPAINT_COLORKEY")) { - Atom colorkey = None; + Atom colorkey; colorkey = XInternAtom (display, "XV_AUTOPAINT_COLORKEY", True); if (colorkey != None) diff --git a/gtk/src/gtk_sound_driver_alsa.cpp b/gtk/src/gtk_sound_driver_alsa.cpp index 8f033d15..a49e9db6 100644 --- a/gtk/src/gtk_sound_driver_alsa.cpp +++ b/gtk/src/gtk_sound_driver_alsa.cpp @@ -71,10 +71,12 @@ S9xAlsaSoundDriver::open_device (void) printf ("ALSA sound driver initializing...\n"); printf (" --> (Device: default)..."); - if ((err = snd_pcm_open (&pcm, - "default", - SND_PCM_STREAM_PLAYBACK, - SND_PCM_NONBLOCK) < 0)) + err = snd_pcm_open (&pcm, + "default", + SND_PCM_STREAM_PLAYBACK, + SND_PCM_NONBLOCK); + + if (err < 0) { goto fail; } diff --git a/gtk/src/sourcify.c b/gtk/src/sourcify.c index 030c06ae..34b0b47c 100644 --- a/gtk/src/sourcify.c +++ b/gtk/src/sourcify.c @@ -9,33 +9,33 @@ main (int argc, unsigned char inchar; unsigned int counter; struct stat file_info; - - if (argc != 4) + + if (argc != 4) { fprintf (stderr, "Usage: %s infile outfile identifier\n", argv[0]); return 1; } - + stat (argv[1], &file_info); - + infile = fopen (argv[1], "r"); - + if (!infile) { fprintf (stderr, "Input file doesn't exist.\n"); return 2; } - + outfile = fopen (argv[2], "w+"); - - fprintf (outfile, - "int %s_size = %d;\n\n", - argv[3], + + fprintf (outfile, + "int %s_size = %d;\n\n", + argv[3], (int) file_info.st_size); fprintf (outfile, "unsigned char %s [] = \n{\n ", argv[3]); - + counter = 0; - + while (fread (&inchar, 1, 1, infile)) { if (counter >= 32) @@ -43,15 +43,18 @@ main (int argc, counter = 0; fprintf (outfile, "\n "); } - + fprintf (outfile, "%d,", inchar); counter++; } - + /* Erase extra "," */ fseek (outfile, -1, SEEK_CUR); - + fprintf (outfile, "\n};\n"); - + + fclose (infile); + fclose (outfile); + return 0; } diff --git a/movie.cpp b/movie.cpp index 6b42745a..2cc69876 100644 --- a/movie.cpp +++ b/movie.cpp @@ -822,7 +822,10 @@ int S9xMovieOpen (const char *filename, bool8 read_only) } if (fseek(fd, Movie.ControllerDataOffset, SEEK_SET)) + { + fclose(fd); return (WRONG_FORMAT); + } Movie.File = fd; Movie.BytesPerSample = bytes_per_sample(); @@ -970,7 +973,10 @@ int S9xMovieGetInfo (const char *filename, struct MovieInfo *info) result = read_movie_header(fd, &local_movie); if (result != SUCCESS) + { + fclose(fd); return (result); + } info->TimeCreated = (time_t) local_movie.MovieId; info->Version = local_movie.Version; diff --git a/netplay.cpp b/netplay.cpp index 6e18291c..8f01ca7c 100644 --- a/netplay.cpp +++ b/netplay.cpp @@ -386,10 +386,10 @@ on the remote machine on this port?"); { S9xNPSetError ("Sending 'HELLO' message failed."); S9xNPDisconnect (); - delete tmp; + delete[] tmp; return (FALSE); } - delete tmp; + delete[] tmp; #ifdef NP_DEBUG printf ("CLIENT: Waiting for 'WELCOME' reply from server @%ld...\n", S9xGetMilliTime () - START); @@ -420,7 +420,7 @@ on the remote machine on this port?"); if (!S9xNPGetData (NetPlay.Socket, data, len - 7)) { S9xNPSetError ("Error in 'HELLO' reply packet received from server."); - delete data; + delete[] data; S9xNPDisconnect (); return (FALSE); } @@ -430,7 +430,7 @@ on the remote machine on this port?"); S9xNPSetError ("\ The Snes9X NetPlay server implements a different\n\ version of the protocol. Disconnecting."); - delete data; + delete[] data; S9xNPDisconnect (); return (FALSE); } @@ -442,13 +442,13 @@ version of the protocol. Disconnecting."); { if (!S9xNPLoadROMDialog ((char *) data + 4 + 2)) { - delete data; + delete[] data; S9xNPDisconnect (); return (FALSE); } } NetPlay.Player = data [1]; - delete data; + delete[] data; NetPlay.PendingWait4Sync = TRUE; Settings.NetPlay = TRUE; @@ -754,7 +754,7 @@ bool8 S9xNPLoadROM (uint32 len) if (!S9xNPGetData (NetPlay.Socket, data, len)) { S9xNPSetError ("Error while receiving ROM name."); - delete data; + delete[] data; S9xNPDisconnect (); return (FALSE); } @@ -763,11 +763,11 @@ bool8 S9xNPLoadROM (uint32 len) if (!S9xNPLoadROMDialog ((char *) data)) { S9xNPSetError ("Disconnected from NetPlay server because you are playing a different game!"); - delete data; + delete[] data; S9xNPDisconnect (); return (FALSE); } - delete data; + delete[] data; return (TRUE); } @@ -878,7 +878,7 @@ void S9xNPGetFreezeFile (uint32 len) { S9xNPSetError ("Error while receiving freeze file from server."); S9xNPDisconnect (); - delete data; + delete[] data; return; } S9xNPSetAction ("", TRUE); @@ -924,7 +924,7 @@ void S9xNPGetFreezeFile (uint32 len) remove (fname); } else S9xNPSetError ("Unable to get name for temporary freeze file."); - delete data; + delete[] data; } uint32 S9xNPGetJoypad (int which1) diff --git a/server.cpp b/server.cpp index 6a376132..938d0a14 100644 --- a/server.cpp +++ b/server.cpp @@ -535,7 +535,7 @@ void S9xNPProcessClient (int c) len = 7 + 1 + 1 + 4 + strlen (NPServer.ROMName) + 1; - delete data; + delete[] data; ptr = data = new uint8 [len]; *ptr++ = NP_SERV_MAGIC; *ptr++ = NPServer.Clients [c].SendSequenceNum++; @@ -563,7 +563,7 @@ void S9xNPProcessClient (int c) S9xNPShutdownClient (c, TRUE); return; } - delete data; + delete[] data; #ifdef NP_DEBUG printf ("SERVER: Waiting for a response from the client @%ld...\n", S9xGetMilliTime () - START); #endif @@ -1306,7 +1306,7 @@ void S9xNPSendROMLoadRequest (const char *filename) } } } - delete data; + delete[] data; } void S9xNPSendSRAMToAllClients () diff --git a/unix/x11.cpp b/unix/x11.cpp index 65e395c8..d2d9847e 100644 --- a/unix/x11.cpp +++ b/unix/x11.cpp @@ -701,7 +701,7 @@ static bool8 SetupXvideo() { if (!strcmp (port_attr[i].name, "XV_AUTOPAINT_COLORKEY")) { - Atom colorkey = None; + Atom colorkey; colorkey = XInternAtom (GUI.display, "XV_AUTOPAINT_COLORKEY", True); if (colorkey != None)