From 7b79d9db4ca85235ae8e7eee028fa32987768cbc Mon Sep 17 00:00:00 2001 From: harry Date: Fri, 23 Feb 2024 20:53:01 -0500 Subject: [PATCH] Partial cleanup of sprintf usage in favor of snprintf. This is to resolve deprecation warnings on Mac OSX --- src/asm.cpp | 26 ++++++------- src/drivers/Qt/AboutWindow.cpp | 32 ++++++++-------- src/drivers/Qt/sdl-joystick.cpp | 18 ++++----- src/fds.cpp | 4 +- src/file.cpp | 68 ++++++++++++++++----------------- src/ines.cpp | 32 +++++++++++----- src/lua-engine.cpp | 10 ++--- src/movie.cpp | 16 ++++---- src/nsf.cpp | 2 +- src/profiler.cpp | 6 +-- src/state.cpp | 2 +- src/video.cpp | 4 +- src/vsuni.cpp | 43 ++++++++++++++------- 13 files changed, 146 insertions(+), 117 deletions(-) diff --git a/src/asm.cpp b/src/asm.cpp index 0aa2a61e..1c57d93b 100644 --- a/src/asm.cpp +++ b/src/asm.cpp @@ -286,7 +286,7 @@ char *Disassemble(int addr, uint8 *opcode) { #ifdef BRK_3BYTE_HACK case 0x00: - sprintf(str,"BRK %02X %02X", opcode[1], opcode[2]); + snprintf(str, sizeof(str), "BRK %02X %02X", opcode[1], opcode[2]); break; #else case 0x00: strcpy(str,"BRK"); break; @@ -333,7 +333,7 @@ char *Disassemble(int addr, uint8 *opcode) { case 0xE1: strcpy(chr,"SBC"); goto _indirectx; _indirectx: indirectX(tmp); - sprintf(str,"%s ($%02X,X) @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); + snprintf(str, sizeof(str), "%s ($%02X,X) @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); break; //Zero Page @@ -361,7 +361,7 @@ char *Disassemble(int addr, uint8 *opcode) { _zeropage: // ################################## Start of SP CODE ########################### // Change width to %04X // don't! - sprintf(str,"%s $%02X = #$%02X", chr,opcode[1],GetMem(opcode[1])); + snprintf(str, sizeof(str), "%s $%02X = #$%02X", chr,opcode[1],GetMem(opcode[1])); // ################################## End of SP CODE ########################### break; @@ -379,7 +379,7 @@ char *Disassemble(int addr, uint8 *opcode) { case 0xE0: strcpy(chr,"CPX"); goto _immediate; case 0xE9: strcpy(chr,"SBC"); goto _immediate; _immediate: - sprintf(str,"%s #$%02X", chr,opcode[1]); + snprintf(str, sizeof(str), "%s #$%02X", chr,opcode[1]); break; //Absolute @@ -406,7 +406,7 @@ char *Disassemble(int addr, uint8 *opcode) { case 0xEE: strcpy(chr,"INC"); goto _absolute; _absolute: absolute(tmp); - sprintf(str,"%s $%04X = #$%02X", chr,tmp,GetMem(tmp)); + snprintf(str, sizeof(str), "%s $%04X = #$%02X", chr,tmp,GetMem(tmp)); break; //branches @@ -420,7 +420,7 @@ char *Disassemble(int addr, uint8 *opcode) { case 0xF0: strcpy(chr,"BEQ"); goto _branch; _branch: relative(tmp); - sprintf(str,"%s $%04X", chr,tmp); + snprintf(str, sizeof(str), "%s $%04X", chr,tmp); break; //(Indirect),Y @@ -434,7 +434,7 @@ char *Disassemble(int addr, uint8 *opcode) { case 0xF1: strcpy(chr,"SBC"); goto _indirecty; _indirecty: indirectY(tmp); - sprintf(str,"%s ($%02X),Y @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); + snprintf(str, sizeof(str), "%s ($%02X),Y @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); break; //Zero Page,X @@ -458,7 +458,7 @@ char *Disassemble(int addr, uint8 *opcode) { zpIndex(tmp,RX); // ################################## Start of SP CODE ########################### // Change width to %04X // don't! - sprintf(str,"%s $%02X,X @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); + snprintf(str, sizeof(str), "%s $%02X,X @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); // ################################## End of SP CODE ########################### break; @@ -475,7 +475,7 @@ char *Disassemble(int addr, uint8 *opcode) { _absolutey: absolute(tmp); tmp2=(tmp+RY); - sprintf(str,"%s $%04X,Y @ $%04X = #$%02X", chr,tmp,tmp2,GetMem(tmp2)); + snprintf(str, sizeof(str), "%s $%04X,Y @ $%04X = #$%02X", chr,tmp,tmp2,GetMem(tmp2)); break; //Absolute,X @@ -497,16 +497,16 @@ char *Disassemble(int addr, uint8 *opcode) { _absolutex: absolute(tmp); tmp2=(tmp+RX); - sprintf(str,"%s $%04X,X @ $%04X = #$%02X", chr,tmp,tmp2,GetMem(tmp2)); + snprintf(str, sizeof(str), "%s $%04X,X @ $%04X = #$%02X", chr,tmp,tmp2,GetMem(tmp2)); break; //jumps case 0x20: strcpy(chr,"JSR"); goto _jump; case 0x4C: strcpy(chr,"JMP"); goto _jump; - case 0x6C: absolute(tmp); sprintf(str,"JMP ($%04X) = $%04X", tmp,GetMem(tmp)|GetMem(tmp+1)<<8); break; + case 0x6C: absolute(tmp); snprintf(str, sizeof(str), "JMP ($%04X) = $%04X", tmp,GetMem(tmp)|GetMem(tmp+1)<<8); break; _jump: absolute(tmp); - sprintf(str,"%s $%04X", chr,tmp); + snprintf(str, sizeof(str), "%s $%04X", chr,tmp); break; //Zero Page,Y @@ -516,7 +516,7 @@ char *Disassemble(int addr, uint8 *opcode) { zpIndex(tmp,RY); // ################################## Start of SP CODE ########################### // Change width to %04X // don't! - sprintf(str,"%s $%02X,Y @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); + snprintf(str, sizeof(str), "%s $%02X,Y @ $%04X = #$%02X", chr,opcode[1],tmp,GetMem(tmp)); // ################################## End of SP CODE ########################### break; diff --git a/src/drivers/Qt/AboutWindow.cpp b/src/drivers/Qt/AboutWindow.cpp index 20719222..2ea28c7b 100644 --- a/src/drivers/Qt/AboutWindow.cpp +++ b/src/drivers/Qt/AboutWindow.cpp @@ -130,7 +130,7 @@ AboutWindow::AboutWindow(QWidget *parent) mainLayout->addLayout( hbox1 ); - sprintf( stmp, "git URL: %s", fceu_get_git_url() ); + snprintf( stmp, sizeof(stmp), "git URL: %s", fceu_get_git_url() ); hbox1 = new QHBoxLayout(); lbl = new QLabel( tr(stmp) ); @@ -140,7 +140,7 @@ AboutWindow::AboutWindow(QWidget *parent) mainLayout->addLayout( hbox1 ); - sprintf( stmp, "git Revision: %s", fceu_get_git_rev() ); + snprintf( stmp, sizeof(stmp), "git Revision: %s", fceu_get_git_rev() ); hbox1 = new QHBoxLayout(); lbl = new QLabel( tr(stmp) ); @@ -191,23 +191,23 @@ AboutWindow::AboutWindow(QWidget *parent) credits->insertPlainText( "\nOpen Source Dependencies:\n" ); - sprintf( stmp, " Compiled with Qt version %d.%d.%d\n", QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH ); + snprintf( stmp, sizeof(stmp), " Compiled with Qt version %d.%d.%d\n", QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH ); credits->insertPlainText( stmp ); - sprintf( stmp, " Compiled with SDL version %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL ); + snprintf( stmp, sizeof(stmp), " Compiled with SDL version %d.%d.%d\n", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL ); credits->insertPlainText( stmp ); SDL_version v; SDL_GetVersion(&v); - sprintf( stmp, " Linked with SDL version %d.%d.%d\n", v.major, v.minor, v.patch); + snprintf( stmp, sizeof(stmp), " Linked with SDL version %d.%d.%d\n", v.major, v.minor, v.patch); credits->insertPlainText( stmp ); #ifdef ZLIB_VERSION - sprintf( stmp, " Compiled with zlib %s\n", ZLIB_VERSION ); + snprintf( stmp, sizeof(stmp), " Compiled with zlib %s\n", ZLIB_VERSION ); credits->insertPlainText( stmp ); #endif #ifdef _USE_LIBARCHIVE - sprintf( stmp, " Compiled with libarchive %s\n", ARCHIVE_VERSION_ONLY_STRING ); + snprintf( stmp, sizeof(stmp), " Compiled with libarchive %s\n", ARCHIVE_VERSION_ONLY_STRING ); credits->insertPlainText( stmp ); const char *libArcName[] = { "zlib", "liblzma", "bzlib", "liblz4", "libzstd", nullptr }; const char *libArcVersion[] = { archive_zlib_version(), archive_liblzma_version(), @@ -217,7 +217,7 @@ AboutWindow::AboutWindow(QWidget *parent) { if (libArcVersion[i]) { - sprintf( stmp, " %s %s\n", libArcName[i], libArcVersion[i]); + snprintf( stmp, sizeof(stmp), " %s %s\n", libArcName[i], libArcVersion[i]); credits->insertPlainText( stmp ); } i++; @@ -225,27 +225,27 @@ AboutWindow::AboutWindow(QWidget *parent) #endif #ifdef _S9XLUA_H - sprintf( stmp, " Compiled with %s\n", LUA_RELEASE ); + snprintf( stmp, sizeof(stmp), " Compiled with %s\n", LUA_RELEASE ); credits->insertPlainText( stmp ); #endif #ifdef _USE_LIBAV - sprintf( stmp, " Compiled with ffmpeg libraries:\n"); + snprintf( stmp, sizeof(stmp), " Compiled with ffmpeg libraries:\n"); credits->insertPlainText( stmp ); - sprintf( stmp, " libavutil %i.%i.%i\n", LIBAVUTIL_VERSION_MAJOR, LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO); + snprintf( stmp, sizeof(stmp), " libavutil %i.%i.%i\n", LIBAVUTIL_VERSION_MAJOR, LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO); credits->insertPlainText( stmp ); - sprintf( stmp, " libavformat %i.%i.%i\n", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO); + snprintf( stmp, sizeof(stmp), " libavformat %i.%i.%i\n", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO); credits->insertPlainText( stmp ); - sprintf( stmp, " libavcodec %i.%i.%i\n", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO); + snprintf( stmp, sizeof(stmp), " libavcodec %i.%i.%i\n", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO); credits->insertPlainText( stmp ); - sprintf( stmp, " libswscale %i.%i.%i\n", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO); + snprintf( stmp, sizeof(stmp), " libswscale %i.%i.%i\n", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO); credits->insertPlainText( stmp ); - sprintf( stmp, " libswresample %i.%i.%i\n", LIBSWRESAMPLE_VERSION_MAJOR, LIBSWRESAMPLE_VERSION_MINOR, LIBSWRESAMPLE_VERSION_MICRO); + snprintf( stmp, sizeof(stmp), " libswresample %i.%i.%i\n", LIBSWRESAMPLE_VERSION_MAJOR, LIBSWRESAMPLE_VERSION_MINOR, LIBSWRESAMPLE_VERSION_MICRO); credits->insertPlainText( stmp ); #endif #ifdef _USE_X264 - sprintf( stmp, " Compiled with x264 version %s\n", X264_POINTVER ); + snprintf( stmp, sizeof(stmp), " Compiled with x264 version %s\n", X264_POINTVER ); credits->insertPlainText( stmp ); #endif diff --git a/src/drivers/Qt/sdl-joystick.cpp b/src/drivers/Qt/sdl-joystick.cpp index 29f6b28c..5445fd9a 100644 --- a/src/drivers/Qt/sdl-joystick.cpp +++ b/src/drivers/Qt/sdl-joystick.cpp @@ -946,7 +946,7 @@ int GamePad_t::saveCurrentMapToFile(const char *name) output.append(name); output.append(","); output.append("config:"); - sprintf( stmp, "%i,", c ); + snprintf( stmp, sizeof(stmp), "%i,", c ); output.append(stmp); for (i = 0; i < GAMEPAD_NUM_BUTTONS; i++) @@ -970,26 +970,26 @@ int GamePad_t::saveCurrentMapToFile(const char *name) } stmp[k] = 0; - //sprintf(stmp, "k%s", SDL_GetKeyName(bmap[c][i].ButtonNum)); + //snprintf(stmp, sizeof(stmp), "k%s", SDL_GetKeyName(bmap[c][i].ButtonNum)); } else { if (bmap[c][i].ButtonNum & 0x2000) { /* Hat "button" */ - sprintf(stmp, "h%i.%i", + snprintf(stmp, sizeof(stmp), "h%i.%i", (bmap[c][i].ButtonNum >> 8) & 0x1F, bmap[c][i].ButtonNum & 0xFF); } else if (bmap[c][i].ButtonNum & 0x8000) { /* Axis "button" */ - sprintf(stmp, "%ca%i", + snprintf(stmp, sizeof(stmp), "%ca%i", (bmap[c][i].ButtonNum & 0x4000) ? '-' : '+', bmap[c][i].ButtonNum & 0x3FFF); } else { /* Button */ - sprintf(stmp, "b%i", bmap[c][i].ButtonNum); + snprintf(stmp, sizeof(stmp), "b%i", bmap[c][i].ButtonNum); } } output.append(buttonNames[i]); @@ -1034,26 +1034,26 @@ int GamePad_t::saveCurrentMapToFile(const char *name) stmp[k] = keyName[j]; k++; j++; } stmp[k] = 0; - //sprintf(stmp, "k%s", SDL_GetKeyName(fk->bmap[i].ButtonNum)); + //snprintf(stmp, sizeof(stmp), "k%s", SDL_GetKeyName(fk->bmap[i].ButtonNum)); } else { if (fk->bmap[i].ButtonNum & 0x2000) { /* Hat "button" */ - sprintf(stmp, "h%i.%i", + snprintf(stmp, sizeof(stmp), "h%i.%i", (fk->bmap[i].ButtonNum >> 8) & 0x1F, fk->bmap[i].ButtonNum & 0xFF); } else if (fk->bmap[i].ButtonNum & 0x8000) { /* Axis "button" */ - sprintf(stmp, "%ca%i", + snprintf(stmp, sizeof(stmp), "%ca%i", (fk->bmap[i].ButtonNum & 0x4000) ? '-' : '+', fk->bmap[i].ButtonNum & 0x3FFF); } else { /* Button */ - sprintf(stmp, "b%i", fk->bmap[i].ButtonNum); + snprintf(stmp, sizeof(stmp), "b%i", fk->bmap[i].ButtonNum); } } if ( i == 0 ) diff --git a/src/fds.cpp b/src/fds.cpp index 532dc14c..885728fb 100644 --- a/src/fds.cpp +++ b/src/fds.cpp @@ -866,8 +866,8 @@ int FDSLoad(const char *name, FCEUFILE *fp) { FDSSoundStateAdd(); for (x = 0; x < TotalSides; x++) { - char temp[5]; - sprintf(temp, "DDT%d", x); + char temp[8]; + snprintf(temp, sizeof(temp), "DDT%d", x); AddExState(diskdata[x], 65500, 0, temp); } diff --git a/src/file.cpp b/src/file.cpp index ef3e2c14..ae4b5301 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -148,7 +148,7 @@ end: std::string FCEU_MakeIpsFilename(FileBaseInfo fbi) { char ret[FILENAME_MAX] = ""; - sprintf(ret,"%s" PSS "%s%s.ips",fbi.filebasedirectory.c_str(),fbi.filebase.c_str(),fbi.ext.c_str()); + snprintf(ret, sizeof(ret), "%s" PSS "%s%s.ips",fbi.filebasedirectory.c_str(),fbi.filebase.c_str(),fbi.ext.c_str()); return ret; } @@ -618,9 +618,9 @@ std::string FCEU_MakeFName(int type, int id1, const char *cd1) struct stat fileInfo; do { if(odirs[FCEUIOD_MOVIES]) - sprintf(ret,"%s" PSS "%s-%d.fm2",odirs[FCEUIOD_MOVIES],FileBase, id1); + snprintf(ret, sizeof(ret), "%s" PSS "%s-%d.fm2",odirs[FCEUIOD_MOVIES],FileBase, id1); else - sprintf(ret,"%s" PSS "movies" PSS "%s-%d.fm2",BaseDirectory.c_str(),FileBase, id1); + snprintf(ret, sizeof(ret), "%s" PSS "movies" PSS "%s-%d.fm2",BaseDirectory.c_str(),FileBase, id1); id1++; } while (stat(ret, &fileInfo) == 0); break; @@ -644,19 +644,19 @@ std::string FCEU_MakeFName(int type, int id1, const char *cd1) if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s%s.fc%d",odirs[FCEUIOD_STATES],FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "%s%s.fc%d",odirs[FCEUIOD_STATES],FileBase,mfn,id1); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s%s.fc%d",BaseDirectory.c_str(),FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s%s.fc%d",BaseDirectory.c_str(),FileBase,mfn,id1); } if(stat(ret,&tmpstat)==-1) { if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s%s.fc%d",odirs[FCEUIOD_STATES],FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "%s%s.fc%d",odirs[FCEUIOD_STATES],FileBase,mfn,id1); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s%s.fc%d",BaseDirectory.c_str(),FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s%s.fc%d",BaseDirectory.c_str(),FileBase,mfn,id1); } } } @@ -665,46 +665,46 @@ std::string FCEU_MakeFName(int type, int id1, const char *cd1) { if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s-resume.fcs",odirs[FCEUIOD_STATES],FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "%s-resume.fcs",odirs[FCEUIOD_STATES],FileBase); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s-resume.fcs",BaseDirectory.c_str(),FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s-resume.fcs",BaseDirectory.c_str(),FileBase); } if(stat(ret,&tmpstat)==-1) { if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s-resume.fcs",odirs[FCEUIOD_STATES],FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "%s-resume.fcs",odirs[FCEUIOD_STATES],FileBase); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s-resume.fcs",BaseDirectory.c_str(),FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s-resume.fcs",BaseDirectory.c_str(),FileBase); } } } break; case FCEUMKF_SNAP: if(odirs[FCEUIOD_SNAPS]) - sprintf(ret,"%s" PSS "%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1); else - sprintf(ret,"%s" PSS "snaps" PSS "%s-%d.%s",BaseDirectory.c_str(),FileBase,id1,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "snaps" PSS "%s-%d.%s",BaseDirectory.c_str(),FileBase,id1,cd1); break; case FCEUMKF_FDS: if(odirs[FCEUIOD_NV]) - sprintf(ret,"%s" PSS "%s.fds",odirs[FCEUIOD_NV],FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "%s.fds",odirs[FCEUIOD_NV],FileBase); else - sprintf(ret,"%s" PSS "sav" PSS "%s.fds",BaseDirectory.c_str(),FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "sav" PSS "%s.fds",BaseDirectory.c_str(),FileBase); break; case FCEUMKF_SAV: if(odirs[FCEUIOD_NV]) - sprintf(ret,"%s" PSS "%s.%s",odirs[FCEUIOD_NV],FileBase,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "%s.%s",odirs[FCEUIOD_NV],FileBase,cd1); else - sprintf(ret,"%s" PSS "sav" PSS "%s.%s",BaseDirectory.c_str(),FileBase,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "sav" PSS "%s.%s",BaseDirectory.c_str(),FileBase,cd1); if(stat(ret,&tmpstat)==-1) { if(odirs[FCEUIOD_NV]) - sprintf(ret,"%s" PSS "%s.%s",odirs[FCEUIOD_NV],FileBase,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "%s.%s",odirs[FCEUIOD_NV],FileBase,cd1); else - sprintf(ret,"%s" PSS "sav" PSS "%s.%s",BaseDirectory.c_str(),FileBase,cd1); + snprintf(ret, sizeof(ret), "%s" PSS "sav" PSS "%s.%s",BaseDirectory.c_str(),FileBase,cd1); } break; case FCEUMKF_AUTOSTATE: @@ -722,52 +722,52 @@ std::string FCEU_MakeFName(int type, int id1, const char *cd1) if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s%s-autosave%d.fcs",odirs[FCEUIOD_STATES],FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "%s%s-autosave%d.fcs",odirs[FCEUIOD_STATES],FileBase,mfn,id1); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s%s-autosave%d.fcs",BaseDirectory.c_str(),FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s%s-autosave%d.fcs",BaseDirectory.c_str(),FileBase,mfn,id1); } if(stat(ret,&tmpstat)==-1) { if(odirs[FCEUIOD_STATES]) { - sprintf(ret,"%s" PSS "%s%s-autosave%d.fcs",odirs[FCEUIOD_STATES],FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "%s%s-autosave%d.fcs",odirs[FCEUIOD_STATES],FileBase,mfn,id1); } else { - sprintf(ret,"%s" PSS "fcs" PSS "%s%s-autosave%d.fcs",BaseDirectory.c_str(),FileBase,mfn,id1); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s%s-autosave%d.fcs",BaseDirectory.c_str(),FileBase,mfn,id1); } } break; case FCEUMKF_CHEAT: if(odirs[FCEUIOD_CHEATS]) - sprintf(ret,"%s" PSS "%s.cht",odirs[FCEUIOD_CHEATS],FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "%s.cht",odirs[FCEUIOD_CHEATS],FileBase); else - sprintf(ret,"%s" PSS "cheats" PSS "%s.cht",BaseDirectory.c_str(),FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "cheats" PSS "%s.cht",BaseDirectory.c_str(),FileBase); break; case FCEUMKF_IPS: strcpy(ret,FCEU_MakeIpsFilename(CurrentFileBase()).c_str()); break; - case FCEUMKF_GGROM:sprintf(ret,"%s" PSS "gg.rom",BaseDirectory.c_str());break; + case FCEUMKF_GGROM:snprintf(ret, sizeof(ret), "%s" PSS "gg.rom",BaseDirectory.c_str());break; case FCEUMKF_FDSROM: if(odirs[FCEUIOD_FDSROM]) - sprintf(ret,"%s" PSS "disksys.rom",odirs[FCEUIOD_FDSROM]); + snprintf(ret, sizeof(ret), "%s" PSS "disksys.rom",odirs[FCEUIOD_FDSROM]); else - sprintf(ret,"%s" PSS "disksys.rom",BaseDirectory.c_str()); + snprintf(ret, sizeof(ret), "%s" PSS "disksys.rom",BaseDirectory.c_str()); break; - case FCEUMKF_PALETTE:sprintf(ret,"%s" PSS "%s.pal",BaseDirectory.c_str(),FileBase);break; + case FCEUMKF_PALETTE:snprintf(ret, sizeof(ret), "%s" PSS "%s.pal",BaseDirectory.c_str(),FileBase);break; case FCEUMKF_MOVIEGLOB: //these globs use ??? because we can load multiple formats if(odirs[FCEUIOD_MOVIES]) - sprintf(ret,"%s" PSS "*.???",odirs[FCEUIOD_MOVIES]); + snprintf(ret, sizeof(ret), "%s" PSS "*.???",odirs[FCEUIOD_MOVIES]); else - sprintf(ret,"%s" PSS "movies" PSS "*.???",BaseDirectory.c_str()); + snprintf(ret, sizeof(ret), "%s" PSS "movies" PSS "*.???",BaseDirectory.c_str()); break; - case FCEUMKF_MOVIEGLOB2:sprintf(ret,"%s" PSS "*.???",BaseDirectory.c_str());break; + case FCEUMKF_MOVIEGLOB2:snprintf(ret, sizeof(ret), "%s" PSS "*.???",BaseDirectory.c_str());break; case FCEUMKF_STATEGLOB: if(odirs[FCEUIOD_STATES]) - sprintf(ret,"%s" PSS "%s*.fc?",odirs[FCEUIOD_STATES],FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "%s*.fc?",odirs[FCEUIOD_STATES],FileBase); else - sprintf(ret,"%s" PSS "fcs" PSS "%s*.fc?",BaseDirectory.c_str(),FileBase); + snprintf(ret, sizeof(ret), "%s" PSS "fcs" PSS "%s*.fc?",BaseDirectory.c_str(),FileBase); break; } diff --git a/src/ines.cpp b/src/ines.cpp index 5d4427df..3dd08318 100644 --- a/src/ines.cpp +++ b/src/ines.cpp @@ -475,21 +475,33 @@ static void CheckHInfo(uint64 partialmd5) { if (MapperNo == 99) Mirroring = 2; - if (tofix) { - char gigastr[768]; - strcpy(gigastr, "The iNES header contains incorrect information. For now, the information will be corrected in RAM. "); + if (tofix) + { + char tmpStr[128]; + std::string gigastr; + gigastr.reserve(768); + gigastr.assign("The iNES header contains incorrect information. For now, the information will be corrected in RAM. "); if (tofix & 1) - sprintf(gigastr + strlen(gigastr), "The mapper number should be set to %d. ", MapperNo); - if (tofix & 2) { + { + snprintf(tmpStr, sizeof(tmpStr), "The mapper number should be set to %d. ", MapperNo); + gigastr.append(tmpStr); + } + if (tofix & 2) + { const char *mstr[3] = { "Horizontal", "Vertical", "Four-screen" }; - sprintf(gigastr + strlen(gigastr), "Mirroring should be set to \"%s\". ", mstr[Mirroring & 3]); + snprintf(tmpStr, sizeof(tmpStr), "Mirroring should be set to \"%s\". ", mstr[Mirroring & 3]); + gigastr.append(tmpStr); } if (tofix & 4) - strcat(gigastr, "The battery-backed bit should be set. "); + { + gigastr.append("The battery-backed bit should be set. "); + } if (tofix & 8) - strcat(gigastr, "This game should not have any CHR ROM. "); - strcat(gigastr, "\n"); - FCEU_printf("%s", gigastr); + { + gigastr.append("This game should not have any CHR ROM. "); + } + gigastr.append("\n"); + FCEU_printf("%s", gigastr.c_str()); } } diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 450cd94b..c9711d85 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -906,7 +906,7 @@ static void LuaStackToBinaryConverter(lua_State* L, int i, std::vector",CurrentSong,NSFHeader.TotalSongs); + snprintf(snbuf, sizeof(snbuf), "<%d/%d>",CurrentSong,NSFHeader.TotalSongs); DrawTextTrans(XBuf+82*256+4+(((31-strlen(snbuf))<<2)), 256, (uint8*)snbuf, kFgColor); { diff --git a/src/profiler.cpp b/src/profiler.cpp index 077605cc..a355f407 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -136,7 +136,7 @@ profileExecVector::profileExecVector(void) strcpy( threadName, thread->objectName().toStdString().c_str()); } #endif - sprintf( fileName, "fceux-profile-%s.log", threadName); + snprintf( fileName, sizeof(fileName), "fceux-profile-%s.log", threadName); logFp = ::fopen(fileName, "w"); @@ -212,7 +212,7 @@ int profilerFuncMap::addRecord(const char *fileNameStringLiteral, autoScopedLock aLock(_mapMtx); char lineString[64]; - sprintf( lineString, ":%i", fileLineNumber); + snprintf( lineString, sizeof(lineString), ":%i", fileLineNumber); std::string fname(fileNameStringLiteral); @@ -233,7 +233,7 @@ funcProfileRecord *profilerFuncMap::findRecord(const char *fileNameStringLiteral char lineString[64]; funcProfileRecord *rec = nullptr; - sprintf( lineString, ":%i", fileLineNumber); + snprintf( lineString, sizeof(lineString), ":%i", fileLineNumber); std::string fname(fileNameStringLiteral); diff --git a/src/state.cpp b/src/state.cpp index 622168a2..734a58c2 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -345,7 +345,7 @@ static bool ReadStateChunks(EMUFILE* is, int32 totalsize) if(!warned) { char str [256]; - sprintf(str, "Warning: Found unknown save chunk of type %d.\nThis could indicate the save state is corrupted\nor made with a different (incompatible) emulator version.", t); + snprintf(str, sizeof(str), "Warning: Found unknown save chunk of type %d.\nThis could indicate the save state is corrupted\nor made with a different (incompatible) emulator version.", t); FCEUD_PrintError(str); warned=true; } diff --git a/src/video.cpp b/src/video.cpp index b0f7dd0d..cab54fd0 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -765,7 +765,7 @@ void ShowFPS(void) if ( da > FCEUD_GetTimeFreq() ) { - sprintf(fpsmsg, "%.1f", (double)boopcount / ((double)da / FCEUD_GetTimeFreq())); + snprintf(fpsmsg, sizeof(fpsmsg), "%.1f", (double)boopcount / ((double)da / FCEUD_GetTimeFreq())); boopcount = 0; boop_ts = ts; @@ -797,7 +797,7 @@ static void FCEU_DrawPauseCountDown(uint8 *XBuf) framesPerSec = 60; } - sprintf(text, "Unpausing in %d...", (pauseFramesLeft / framesPerSec) + 1); + snprintf(text, sizeof(text), "Unpausing in %d...", (pauseFramesLeft / framesPerSec) + 1); if (text[0]) { diff --git a/src/vsuni.cpp b/src/vsuni.cpp index 0111c863..915d96b2 100644 --- a/src/vsuni.cpp +++ b/src/vsuni.cpp @@ -354,33 +354,50 @@ void FCEU_VSUniCheck(uint64 md5partial, int *MapperNo, uint8 *Mirroring) { GameInfo->vs_cswitch = 1; } - if (tofix) { - char gigastr[768]; - strcpy(gigastr, "The iNES header contains incorrect information. For now, the information will be corrected in RAM. "); + if (tofix) + { + char tmpStr[128]; + std::string gigastr; + gigastr.reserve(768); + gigastr.assign("The iNES header contains incorrect information. For now, the information will be corrected in RAM. "); if (tofix & 4) { - sprintf(gigastr + strlen(gigastr), "Game type should be set to Vs. System. "); + snprintf(tmpStr, sizeof(tmpStr), "Game type should be set to Vs. System. "); + gigastr.append(tmpStr); } if (tofix & 1) - sprintf(gigastr + strlen(gigastr), "The mapper number should be set to %d. ", *MapperNo); - if (tofix & 2) { + { + snprintf(tmpStr, sizeof(tmpStr), "The mapper number should be set to %d. ", *MapperNo); + gigastr.append(tmpStr); + } + if (tofix & 2) + { const char* mstr[3] = { "Horizontal", "Vertical", "Four-screen" }; - sprintf(gigastr + strlen(gigastr), "Mirroring should be set to \"%s\". ", mstr[vs->mirroring & 3]); + snprintf(tmpStr, sizeof(tmpStr), "Mirroring should be set to \"%s\". ", mstr[vs->mirroring & 3]); + gigastr.append(tmpStr); } if (tofix & 8) { const char* mstr[4] = { "Normal", "RBI Baseball protection", "TKO Boxing protection", "Super Xevious protection"}; - sprintf(gigastr + strlen(gigastr), "Vs. System type should be set to \"%s\". ", mstr[vs->type]); + snprintf(tmpStr, sizeof(tmpStr), "Vs. System type should be set to \"%s\". ", mstr[vs->type]); + gigastr.append(tmpStr); } if (tofix & 16) { const char* mstr[10] = { "Default", "RP2C04-0001", "RP2C04-0002", "RP2C04-0003", "RP2C04-0004", "RC2C03B", "RC2C05-01", "RC2C05-02" , "RC2C05-03" , "RC2C05-04" }; - sprintf(gigastr + strlen(gigastr), "Vs. System PPU should be set to \"%s\". ", mstr[vs->ppu]); + snprintf(tmpStr, sizeof(tmpStr), "Vs. System PPU should be set to \"%s\". ", mstr[vs->ppu]); + gigastr.append(tmpStr); } if (tofix & 32) - sprintf(gigastr + strlen(gigastr), "The controller type should be set to zapper. "); + { + snprintf(tmpStr, sizeof(tmpStr), "The controller type should be set to zapper. "); + gigastr.append(tmpStr); + } if (tofix & 64) - sprintf(gigastr + strlen(gigastr), "The controllers should be swapped. "); - strcat(gigastr, "\n"); - FCEU_printf("%s", gigastr); + { + snprintf(tmpStr, sizeof(tmpStr), "The controllers should be swapped. "); + gigastr.append(tmpStr); + } + gigastr.append("\n"); + FCEU_printf("%s", gigastr.c_str()); } return;