From 5a110fd666f1536c4a294b8ae5aec975958dc1ff Mon Sep 17 00:00:00 2001 From: Isaac Marovitz <42140194+IsaacMarovitz@users.noreply.github.com> Date: Tue, 25 Mar 2025 13:47:07 -0400 Subject: [PATCH] Replace sprintf with snprintf (#1884) Signed-off-by: Isaac Marovitz --- core/debug/gdb_server.cpp | 12 +++--- core/hw/holly/sb.cpp | 2 +- core/hw/maple/maple_jvs.cpp | 2 +- core/hw/naomi/naomi.cpp | 2 +- core/hw/naomi/naomi_m3comm.cpp | 3 +- core/hw/pvr/pvr_regs.cpp | 8 ++-- core/hw/sh4/dyna/blockmanager.cpp | 2 +- core/hw/sh4/modules/fastmmu.cpp | 2 +- core/hw/sh4/modules/wince.h | 4 +- core/hw/sh4/sh4_mmr.cpp | 2 +- core/hw/sh4/sh4_opcode_list.cpp | 70 +++++++++++++++---------------- core/hw/sh4/sh4_opcode_list.h | 2 +- core/imgread/common.h | 2 +- core/linux-dist/evdev.cpp | 2 +- core/network/output.h | 2 +- core/oslib/http_client.cpp | 2 +- core/oslib/oslib.cpp | 2 +- core/rend/TexCache.cpp | 8 ++-- core/rend/gles/naomi2.h | 32 +++++++------- core/rend/vulkan/texture.cpp | 2 +- core/rend/vulkan/texture.h | 2 +- core/ui/gui.cpp | 18 ++++---- core/ui/gui_util.h | 4 +- core/windows/winmain.cpp | 9 ++-- 24 files changed, 100 insertions(+), 96 deletions(-) diff --git a/core/debug/gdb_server.cpp b/core/debug/gdb_server.cpp index 35bd947b0..a2c3a691d 100644 --- a/core/debug/gdb_server.cpp +++ b/core/debug/gdb_server.cpp @@ -381,7 +381,7 @@ private: } data.push_back('#'); char s[9]; - sprintf(s, "%02x", checksum); + snprintf(s, sizeof(s), "%02x", checksum); data += s; } DEBUG_LOG(NETWORK, "gdb: sent %s", data.c_str()); @@ -397,7 +397,7 @@ private: std::string reportException() { char s[4]; - sprintf(s, "S%02X", agent.currentException()); + snprintf(s, sizeof(s), "S%02X", agent.currentException()); return s; } @@ -457,7 +457,7 @@ private: for (u32 i = 0; i < len; i++) { char s[3]; - sprintf(s,"%02x", mem[i]); + snprintf(s, sizeof(s), "%02x", mem[i]); outpkt += s; } return outpkt; @@ -586,7 +586,7 @@ private: for (u32 i = 0; i < len; i++) { char n[10]; - sprintf(n, "%08x ", *data++); + snprintf(n, sizeof(n), "%08x ", *data++); for (char *p = n; *p != 0; p++) { *r++ = packnb((*p >> 4) & 0xf); @@ -614,7 +614,7 @@ private: { // Obtain from the target OS a printable string description of thread attributes char s[19]; - sprintf(s, "%02x%02x%02x%02x%02x%02x%02x%02x%02x", 'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0); + snprintf(s, sizeof(s), "%02x%02x%02x%02x%02x%02x%02x%02x%02x", 'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0); return { std::string(s, 18) }; } else if (pkt.rfind("qXfer:", 0) == 0) @@ -825,7 +825,7 @@ private: { u32 signal = agentInterrupt(); char s[10]; - sprintf(s, "S%02x", signal); + snprintf(s, sizeof(s), "S%02x", signal); return s; } diff --git a/core/hw/holly/sb.cpp b/core/hw/holly/sb.cpp index 5966141d4..d6d7ab6c5 100644 --- a/core/hw/holly/sb.cpp +++ b/core/hw/holly/sb.cpp @@ -168,7 +168,7 @@ static const char *regName(u32 addr) auto it = sb_reg_names.find(addr & 0x7fffff); // (addr - 0x5f6800) & 0x1fff); if (it == sb_reg_names.end()) { - sprintf(regName, "?%06x", addr& 0x7fffff); + snprintf(regName, sizeof(regName), "?%06x", addr& 0x7fffff); return regName; } else diff --git a/core/hw/maple/maple_jvs.cpp b/core/hw/maple/maple_jvs.cpp index 8a60a39d8..136fd50e8 100644 --- a/core/hw/maple/maple_jvs.cpp +++ b/core/hw/maple/maple_jvs.cpp @@ -1852,7 +1852,7 @@ u32 maple_naomi_jamma::RawDma(u32* buffer_in, u32 buffer_in_len, u32* buffer_out char filename[128]; for (int i = 0; ; i++) { - sprintf(filename, "z80_fw_%d.bin", i); + snprintf(filename, sizeof(filename), "z80_fw_%d.bin", i); fw_dump = fopen(filename, "r"); if (fw_dump == NULL) { diff --git a/core/hw/naomi/naomi.cpp b/core/hw/naomi/naomi.cpp index ea76f9304..f39c06331 100644 --- a/core/hw/naomi/naomi.cpp +++ b/core/hw/naomi/naomi.cpp @@ -704,7 +704,7 @@ struct DriveSimPipe : public SerialPort::Pipe if (!config::NetworkOutput) { char message[16]; - sprintf(message, "Speed: %3d", speed); + snprintf(message, sizeof(message), "Speed: %3d", speed); os_notify(message, 1000); } } diff --git a/core/hw/naomi/naomi_m3comm.cpp b/core/hw/naomi/naomi_m3comm.cpp index 29a928fd3..6987c15f2 100644 --- a/core/hw/naomi/naomi_m3comm.cpp +++ b/core/hw/naomi/naomi_m3comm.cpp @@ -250,7 +250,8 @@ bool NaomiM3Comm::DmaStart(u32 addr, u32 data) for (u32 i = 0; i < SB_GDLEN; i++) { u8 value = comm_ram[comm_offset + i]; - sprintf(buf + strlen(buf), "%02x ", value); + size_t len = strlen(buf); + snprintf(buf + len, sizeof(buf) - len, "%02x ", value); } DEBUG_LOG(NAOMI, "Comm RAM read @%x: %s", comm_offset, buf); } diff --git a/core/hw/pvr/pvr_regs.cpp b/core/hw/pvr/pvr_regs.cpp index 67eb5e714..44bdc9689 100644 --- a/core/hw/pvr/pvr_regs.cpp +++ b/core/hw/pvr/pvr_regs.cpp @@ -92,13 +92,13 @@ static const char *regName(u32 paddr) if (it == pvr_reg_names.end()) { if (addr >= FOG_TABLE_START_addr && addr <= FOG_TABLE_END_addr) - sprintf(regName, "FOG_TABLE[%x]", addr - FOG_TABLE_START_addr); + snprintf(regName, sizeof(regName), "FOG_TABLE[%x]", addr - FOG_TABLE_START_addr); else if (addr >= TA_OL_POINTERS_START_addr && addr <= TA_OL_POINTERS_END_addr) - sprintf(regName, "TA_OL_POINTERS[%x]", addr - TA_OL_POINTERS_START_addr); + snprintf(regName, sizeof(regName), "TA_OL_POINTERS[%x]", addr - TA_OL_POINTERS_START_addr); else if (addr >= PALETTE_RAM_START_addr && addr <= PALETTE_RAM_END_addr) - sprintf(regName, "PALETTE[%x]", addr - PALETTE_RAM_START_addr); + snprintf(regName, sizeof(regName), "PALETTE[%x]", addr - PALETTE_RAM_START_addr); else - sprintf(regName, "?%08x", paddr); + snprintf(regName, sizeof(regName), "?%08x", paddr); return regName; } else diff --git a/core/hw/sh4/dyna/blockmanager.cpp b/core/hw/sh4/dyna/blockmanager.cpp index ea8cce072..222cde7a9 100644 --- a/core/hw/sh4/dyna/blockmanager.cpp +++ b/core/hw/sh4/dyna/blockmanager.cpp @@ -183,7 +183,7 @@ void bm_AddBlock(RuntimeBlockInfo* blk) { char fname[512]; - sprintf(fname,"sh4:%08X,c:%d,s:%d,h:%d", block->addr, block->guest_cycles, block->guest_opcodes, block->host_opcodes); + snprintf(fname, sizeof(fname), "sh4:%08X,c:%d,s:%d,h:%d", block->addr, block->guest_cycles, block->guest_opcodes, block->host_opcodes); if (op_write_native_code(oprofHandle, fname, (uint64_t)block->code, (void*)block->code, block->host_code_size) != 0) { diff --git a/core/hw/sh4/modules/fastmmu.cpp b/core/hw/sh4/modules/fastmmu.cpp index bc9c08487..17f9418e9 100644 --- a/core/hw/sh4/modules/fastmmu.cpp +++ b/core/hw/sh4/modules/fastmmu.cpp @@ -117,7 +117,7 @@ static void dump_table() { static int iter = 1; char filename[128]; - sprintf(filename, "mmutable%03d", iter++); + snprintf(filename, sizeof(filename), "mmutable%03d", iter++); FILE *f = fopen(filename, "wb"); if (f == NULL) return; diff --git a/core/hw/sh4/modules/wince.h b/core/hw/sh4/modules/wince.h index dd2e8e662..fe1ce9024 100644 --- a/core/hw/sh4/modules/wince.h +++ b/core/hw/sh4/modules/wince.h @@ -294,7 +294,7 @@ static bool print_wince_syscall(u32 address) api = wince_apis[api_id]; else { - sprintf(api_buf, "[%d]", api_id); + snprintf(api_buf, sizeof(api_buf), "[%d]", api_id); api = api_buf; } @@ -310,7 +310,7 @@ static bool print_wince_syscall(u32 address) method = wince_methods[api_id][meth_id]; if (method == NULL) { - sprintf(method_buf, "[%d]", meth_id); + snprintf(method_buf, sizeof(method_buf), "[%d]", meth_id); method = method_buf; } printf("WinCE %08x %04x.%04x %s: %s", address, getCurrentProcessId() & 0xffff, getCurrentThreadId() & 0xffff, api, method); diff --git a/core/hw/sh4/sh4_mmr.cpp b/core/hw/sh4/sh4_mmr.cpp index 314ff8f82..69c7b0065 100644 --- a/core/hw/sh4/sh4_mmr.cpp +++ b/core/hw/sh4/sh4_mmr.cpp @@ -168,7 +168,7 @@ static const char *regName(u32 paddr) static char regName[32]; auto it = sh4_reg_names.find(addr); if (it == sh4_reg_names.end()) { - sprintf(regName, "?%08x", paddr); + snprintf(regName, sizeof(regName), "?%08x", paddr); return regName; } else diff --git a/core/hw/sh4/sh4_opcode_list.cpp b/core/hw/sh4/sh4_opcode_list.cpp index 654fd6ac6..98033f6b2 100644 --- a/core/hw/sh4/sh4_opcode_list.cpp +++ b/core/hw/sh4/sh4_opcode_list.cpp @@ -449,131 +449,131 @@ std::string disassemble_op(const char* tx1, u32 pc, u16 opcode) tx1++; if (strcmp2(tx1,"REG_N>")) { - sprintf(buf,"R%d=%x ", GetN(opcode), p_sh4rcb->cntx.r[GetN(opcode)]); + snprintf(buf, sizeof(buf), "R%d=%x ", GetN(opcode), p_sh4rcb->cntx.r[GetN(opcode)]); regs += buf; - sprintf(buf,"R%d",GetN(opcode)); + snprintf(buf, sizeof(buf), "R%d",GetN(opcode)); } else if (strcmp2(tx1,"REG_M>") ) { - sprintf(buf,"R%d=%x ", GetM(opcode), p_sh4rcb->cntx.r[GetM(opcode)]); + snprintf(buf, sizeof(buf), "R%d=%x ", GetM(opcode), p_sh4rcb->cntx.r[GetM(opcode)]); regs += buf; - sprintf(buf,"R%d",GetM(opcode)); + snprintf(buf, sizeof(buf), "R%d",GetM(opcode)); } else if (strcmp2(tx1,"FREG_N>")) { - sprintf(buf,"FR%d=%f ", GetN(opcode), p_sh4rcb->cntx.fr[GetN(opcode)]); + snprintf(buf, sizeof(buf), "FR%d=%f ", GetN(opcode), p_sh4rcb->cntx.fr[GetN(opcode)]); regs += buf; - sprintf(buf,"FR%d",GetN(opcode)); + snprintf(buf, sizeof(buf), "FR%d",GetN(opcode)); } else if (strcmp2(tx1,"FREG_M>")) { - sprintf(buf,"FR%d=%f ", GetM(opcode), p_sh4rcb->cntx.fr[GetM(opcode)]); + snprintf(buf, sizeof(buf), "FR%d=%f ", GetM(opcode), p_sh4rcb->cntx.fr[GetM(opcode)]); regs += buf; - sprintf(buf,"FR%d",GetM(opcode)); + snprintf(buf, sizeof(buf), "FR%d",GetM(opcode)); } else if (strcmp2(tx1, "FREG_M_SD_F>")) { - sprintf(buf,"FR%d=%f ", GetM(opcode), p_sh4rcb->cntx.fr[GetM(opcode)]); + snprintf(buf, sizeof(buf), "FR%d=%f ", GetM(opcode), p_sh4rcb->cntx.fr[GetM(opcode)]); regs += buf; - sprintf(buf,"FR%d", GetM(opcode)); + snprintf(buf, sizeof(buf), "FR%d", GetM(opcode)); } else if (strcmp2(tx1,"FREG_0>")) { - sprintf(buf,"FR0=%f ", p_sh4rcb->cntx.fr[0]); + snprintf(buf, sizeof(buf), "FR0=%f ", p_sh4rcb->cntx.fr[0]); regs += buf; - sprintf(buf,"FR0"); + snprintf(buf, sizeof(buf), "FR0"); } else if (strcmp2(tx1,"RM_BANK>")) { - sprintf(buf,"R%d_BANK",GetM(opcode)&0x7); + snprintf(buf, sizeof(buf), "R%d_BANK",GetM(opcode)&0x7); } else if (strcmp2(tx1,"DFREG_N>")) { - sprintf(buf,"DR%d",GetN(opcode)>>1); + snprintf(buf, sizeof(buf), "DR%d",GetN(opcode)>>1); } else if (strcmp2(tx1,"DFREG_M>")) { - sprintf(buf,"DR%d",GetM(opcode)>>1); + snprintf(buf, sizeof(buf), "DR%d",GetM(opcode)>>1); } else if (strcmp2(tx1,"XDFREG_N>")) { u32 t=GetN(opcode); if (t & 0x1) - sprintf(buf,"XD%d",t>>1); + snprintf(buf, sizeof(buf), "XD%d",t>>1); else - sprintf(buf,"DR%d",t>>1); + snprintf(buf, sizeof(buf), "DR%d",t>>1); } else if (strcmp2(tx1,"XDFREG_M>")) { u32 t=GetM(opcode); if (t & 0x1) - sprintf(buf,"XD%d",t>>1); + snprintf(buf, sizeof(buf), "XD%d",t>>1); else - sprintf(buf,"DR%d",t>>1); + snprintf(buf, sizeof(buf), "DR%d",t>>1); } else if (strcmp2(tx1,"disp4b>")) { - sprintf(buf,"0x%X",GetImm4(opcode)*1); + snprintf(buf, sizeof(buf), "0x%X",GetImm4(opcode)*1); } else if (strcmp2(tx1,"disp4w>")) { - sprintf(buf,"0x%X",GetImm4(opcode)*2); + snprintf(buf, sizeof(buf), "0x%X",GetImm4(opcode)*2); } else if (strcmp2(tx1,"disp4dw>")) { - sprintf(buf,"0x%X",GetImm4(opcode)*4); + snprintf(buf, sizeof(buf), "0x%X",GetImm4(opcode)*4); } else if (strcmp2(tx1,"PCdisp8w>")) { - sprintf(buf,"0x%X[PC]",(pc)+4+(GetImm8(opcode)<<1)); + snprintf(buf, sizeof(buf), "0x%X[PC]",(pc)+4+(GetImm8(opcode)<<1)); } else if (strcmp2(tx1,"PCdisp8d>")) { - sprintf(buf,"0x%X[PC]",(pc&0xFFFFFFFC)+4+(GetImm8(opcode)<<2)); + snprintf(buf, sizeof(buf), "0x%X[PC]",(pc&0xFFFFFFFC)+4+(GetImm8(opcode)<<2)); } else if (strcmp2(tx1,"disp8b>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*1); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*1); } else if (strcmp2(tx1,"disp8w>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*2); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*2); } else if (strcmp2(tx1,"disp8dw>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*4); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*4); } else if (strcmp2(tx1,"GBRdisp8b>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*1 + p_sh4rcb->cntx.gbr); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*1 + p_sh4rcb->cntx.gbr); } else if (strcmp2(tx1,"GBRdisp8w>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*2 + p_sh4rcb->cntx.gbr); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*2 + p_sh4rcb->cntx.gbr); } else if (strcmp2(tx1,"GBRdisp8dw>")) { - sprintf(buf,"0x%X",GetImm8(opcode)*4 + p_sh4rcb->cntx.gbr); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)*4 + p_sh4rcb->cntx.gbr); } else if (strcmp2(tx1,"bdisp8>")) { - sprintf(buf,"0x%X",((GetSImm8(opcode))*2 + 4 + pc)); + snprintf(buf, sizeof(buf), "0x%X",((GetSImm8(opcode))*2 + 4 + pc)); } else if (strcmp2(tx1,"bdisp12>")) { - sprintf(buf,"0x%X",(( ((s16)((GetImm12(opcode))<<4)) >>3) + pc + 4)); + snprintf(buf, sizeof(buf), "0x%X",(( ((s16)((GetImm12(opcode))<<4)) >>3) + pc + 4)); } else if (strcmp2(tx1,"imm8>")) { - sprintf(buf,"0x%X",GetImm8(opcode)); + snprintf(buf, sizeof(buf), "0x%X",GetImm8(opcode)); } else if (strcmp2(tx1,"simm8>")) { - sprintf(buf,"%d",GetSImm8(opcode)); + snprintf(buf, sizeof(buf), "%d",GetSImm8(opcode)); } else if (strcmp2(tx1,"simm8hex>")) { - sprintf(buf,"0x%X",GetSImm8(opcode)); + snprintf(buf, sizeof(buf), "0x%X",GetSImm8(opcode)); } else { diff --git a/core/hw/sh4/sh4_opcode_list.h b/core/hw/sh4/sh4_opcode_list.h index d0b905aae..5c78c35d0 100644 --- a/core/hw/sh4/sh4_opcode_list.h +++ b/core/hw/sh4/sh4_opcode_list.h @@ -71,7 +71,7 @@ struct sh4_opcodelistentry if (!strcmp(disOp, "missing")) { static char tmp[6]; - sprintf(tmp, "?%04X", op); + snprintf(tmp, sizeof(tmp), "?%04X", op); disOp = tmp; } diff --git a/core/imgread/common.h b/core/imgread/common.h index 8366d7fdc..5feed833b 100644 --- a/core/imgread/common.h +++ b/core/imgread/common.h @@ -155,7 +155,7 @@ struct Disc { u32 fmt = tracks[i].isDataTrack() ? 2048 : 2352; char fsto[1024]; - sprintf(fsto,"%s%s%d.img",path.c_str(),".track",i); + snprintf(fsto, sizeof(fsto), "%s%s%d.img", path.c_str(),".track", i); FILE *fo = nowide::fopen(fsto, "wb"); diff --git a/core/linux-dist/evdev.cpp b/core/linux-dist/evdev.cpp index 09d8095a6..56bdf8e15 100644 --- a/core/linux-dist/evdev.cpp +++ b/core/linux-dist/evdev.cpp @@ -189,7 +189,7 @@ void input_evdev_init() char buf[32]; for (int port = 0; port < 100; port++) { - sprintf(buf, EVDEV_DEVICE_STRING, port); + snprintf(buf, sizeof(buf), EVDEV_DEVICE_STRING, port); input_evdev_add_device(buf); } #endif diff --git a/core/network/output.h b/core/network/output.h index c97da5cdd..5a997b7a3 100644 --- a/core/network/output.h +++ b/core/network/output.h @@ -90,7 +90,7 @@ public: gameNameSent = true; } char s[9]; - sprintf(s, "%x", value); + snprintf(s, sizeof(s), "%x", value); std::string msg = std::string(name) + " = " + std::string(s) + "\n"; // mame uses \r send(msg); } diff --git a/core/oslib/http_client.cpp b/core/oslib/http_client.cpp index b5a70d584..e4fa6c194 100644 --- a/core/oslib/http_client.cpp +++ b/core/oslib/http_client.cpp @@ -142,7 +142,7 @@ int post(const std::string& url, const char *payload, const char *contentType, s { char buf[512]; if (contentType != nullptr) { - sprintf(buf, "Content-Type: %s", contentType); + snprintf(buf, sizeof(buf), "Content-Type: %s", contentType); contentType = buf; } return post(url, contentType, (const u8 *)payload, strlen(payload), reply); diff --git a/core/oslib/oslib.cpp b/core/oslib/oslib.cpp index 4595ed42d..549765308 100644 --- a/core/oslib/oslib.cpp +++ b/core/oslib/oslib.cpp @@ -158,7 +158,7 @@ std::string getSavestatePath(int index, bool writable) char index_str[4] = ""; if (index > 0) // When index is 0, use same name before multiple states is added - sprintf(index_str, "_%d", std::min(99, index)); + snprintf(index_str, sizeof(index_str), "_%d", std::min(99, index)); state_file = state_file + index_str + ".state"; if (index == -1) diff --git a/core/rend/TexCache.cpp b/core/rend/TexCache.cpp index 46743b57a..f4d790d1e 100644 --- a/core/rend/TexCache.cpp +++ b/core/rend/TexCache.cpp @@ -198,7 +198,7 @@ void BaseTextureCacheData::PrintTextureName() { #if !defined(NDEBUG) || defined(DEBUGFAST) char str[512]; - sprintf(str, "Texture: %s", GetPixelFormatName()); + snprintf(str, sizeof(str), "Texture: %s", GetPixelFormatName()); if (tcw.VQ_Comp) strcat(str, " VQ"); @@ -212,9 +212,11 @@ void BaseTextureCacheData::PrintTextureName() if (tsp.FilterMode != 0) strcat(str, " Bilinear"); - sprintf(str + strlen(str), " %dx%d @ 0x%X", 8 << tsp.TexU, 8 << tsp.TexV, tcw.TexAddr << 3); + size_t len = strlen(str); + snprintf(str + len, sizeof(str) - len, " %dx%d @ 0x%X", 8 << tsp.TexU, 8 << tsp.TexV, tcw.TexAddr << 3); std::string id = GetId(); - sprintf(str + strlen(str), " id=%s", id.c_str()); + len = strlen(str); + snprintf(str + len, sizeof(str) - len, " id=%s", id.c_str()); DEBUG_LOG(RENDERER, "%s", str); #endif } diff --git a/core/rend/gles/naomi2.h b/core/rend/gles/naomi2.h index f841e4643..ae3da2489 100644 --- a/core/rend/gles/naomi2.h +++ b/core/rend/gles/naomi2.h @@ -64,37 +64,37 @@ void initN2Uniforms(ShaderType *shader) for (u32 i = 0; i < std::size(shader->lights); i++) { char str[128]; - sprintf(str, "lights[%d].color", i); + snprintf(str, sizeof(str), "lights[%d].color", i); shader->lights[i].color = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].direction", i); + snprintf(str, sizeof(str), "lights[%d].direction", i); shader->lights[i].direction = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].position", i); + snprintf(str, sizeof(str), "lights[%d].position", i); shader->lights[i].position = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].parallel", i); + snprintf(str, sizeof(str), "lights[%d].parallel", i); shader->lights[i].parallel = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].diffuse[0]", i); + snprintf(str, sizeof(str), "lights[%d].diffuse[0]", i); shader->lights[i].diffuse[0] = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].diffuse[1]", i); + snprintf(str, sizeof(str), "lights[%d].diffuse[1]", i); shader->lights[i].diffuse[1] = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].specular[0]", i); + snprintf(str, sizeof(str), "lights[%d].specular[0]", i); shader->lights[i].specular[0] = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].specular[1]", i); + snprintf(str, sizeof(str), "lights[%d].specular[1]", i); shader->lights[i].specular[1] = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].routing", i); + snprintf(str, sizeof(str), "lights[%d].routing", i); shader->lights[i].routing = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].dmode", i); + snprintf(str, sizeof(str), "lights[%d].dmode", i); shader->lights[i].dmode = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].smode", i); + snprintf(str, sizeof(str), "lights[%d].smode", i); shader->lights[i].smode = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].distAttnMode", i); + snprintf(str, sizeof(str), "lights[%d].distAttnMode", i); shader->lights[i].distAttnMode = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].attnDistA", i); + snprintf(str, sizeof(str), "lights[%d].attnDistA", i); shader->lights[i].attnDistA = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].attnDistB", i); + snprintf(str, sizeof(str), "lights[%d].attnDistB", i); shader->lights[i].attnDistB = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].attnAngleA", i); + snprintf(str, sizeof(str), "lights[%d].attnAngleA", i); shader->lights[i].attnAngleA = glGetUniformLocation(shader->program, str); - sprintf(str, "lights[%d].attnAngleB", i); + snprintf(str, sizeof(str), "lights[%d].attnAngleB", i); shader->lights[i].attnAngleB = glGetUniformLocation(shader->program, str); } resetN2UniformCache(shader); diff --git a/core/rend/vulkan/texture.cpp b/core/rend/vulkan/texture.cpp index a269762b9..86022ab32 100644 --- a/core/rend/vulkan/texture.cpp +++ b/core/rend/vulkan/texture.cpp @@ -253,7 +253,7 @@ void Texture::CreateImage(vk::ImageTiling tiling, vk::ImageUsageFlags usage, vk: imageView = device.createImageViewUnique(imageViewCreateInfo); #ifdef VK_DEBUG char name[128]; - sprintf(name, "texture @ %x", startAddress); + snprintf(name, sizeof(name), "texture @ %x", startAddress); VulkanContext::Instance()->setObjectName(image.get(), name); VulkanContext::Instance()->setObjectName(imageView.get(), name); #endif diff --git a/core/rend/vulkan/texture.h b/core/rend/vulkan/texture.h index 5acd49775..16797e49c 100644 --- a/core/rend/vulkan/texture.h +++ b/core/rend/vulkan/texture.h @@ -57,7 +57,7 @@ public: void UploadToGPU(int width, int height, const u8 *data, bool mipmapped, bool mipmapsIncluded = false) override; u64 GetIntId() { return (u64)reinterpret_cast(this); } - std::string GetId() override { char s[20]; sprintf(s, "%p", this); return s; } + std::string GetId() override { char s[20]; snprintf(s, sizeof(s), "%p", this); return s; } vk::ImageView GetImageView() const { return *imageView; } vk::Image GetImage() const { return *image; } vk::ImageView GetReadOnlyImageView() const { return readOnlyImageView ? readOnlyImageView : *imageView; } diff --git a/core/ui/gui.cpp b/core/ui/gui.cpp index 9895e1616..7f2d766d2 100644 --- a/core/ui/gui.cpp +++ b/core/ui/gui.cpp @@ -1351,7 +1351,7 @@ static void controller_mapping_popup(const std::shared_ptr& gamep ImGui::SetColumnWidth(1, col_width); continue; } - sprintf(key_id, "key_id%d", systemMapping->key); + snprintf(key_id, sizeof(key_id), "key_id%d", systemMapping->key); ImguiID _(key_id); const char *game_btn_name = nullptr; @@ -1836,7 +1836,7 @@ static void gui_settings_general() if (ImGui::Button("Reveal in Finder")) { char temp[512]; - sprintf(temp, "open \"%s\"", get_writable_config_path("").c_str()); + snprintf(temp, sizeof(temp), "open \"%s\"", get_writable_config_path("").c_str()); system(temp); } #endif @@ -1978,7 +1978,7 @@ static void gui_settings_controls(bool& maple_devices_changed) ImGui::TableSetColumnIndex(2); char port_name[32]; - sprintf(port_name, "##mapleport%d", i); + snprintf(port_name, sizeof(port_name), "##mapleport%d", i); ImguiID _(port_name); ImGui::SetNextItemWidth(portComboWidth); if (ImGui::BeginCombo(port_name, maple_ports[gamepad->maple_port() + 1])) @@ -2056,7 +2056,7 @@ static void gui_settings_controls(bool& maple_devices_changed) ImGui::TableSetColumnIndex(1); char device_name[32]; - sprintf(device_name, "##device%d", bus); + snprintf(device_name, sizeof(device_name), "##device%d", bus); float w = ImGui::CalcItemWidth() / 3; ImGui::PushItemWidth(w); ImGui::SetNextItemWidth(mainComboWidth); @@ -2092,7 +2092,7 @@ static void gui_settings_controls(bool& maple_devices_changed) for (int port = 0; port < port_count; port++) { ImGui::TableSetColumnIndex(2 + port); - sprintf(device_name, "##device%d.%d", bus, port + 1); + snprintf(device_name, sizeof(device_name), "##device%d.%d", bus, port + 1); ImguiID _(device_name); ImGui::SetNextItemWidth(expComboWidth); if (ImGui::BeginCombo(device_name, maple_expansion_device_name(config::MapleExpansionDevices[bus][port]), ImGuiComboFlags_None)) @@ -2114,7 +2114,7 @@ static void gui_settings_controls(bool& maple_devices_changed) if (config::MapleMainDevices[bus] == MDT_LightGun) { ImGui::TableSetColumnIndex(3); - sprintf(device_name, "##device%d.xhair", bus); + snprintf(device_name, sizeof(device_name), "##device%d.xhair", bus); ImguiID _(device_name); u32 color = config::CrosshairColor[bus]; float xhairColor[4] { @@ -2748,7 +2748,7 @@ static void gui_settings_network() if (config::GGPOChatTimeoutToggle) { char chatTimeout[256]; - sprintf(chatTimeout, "%d", (int)config::GGPOChatTimeout); + snprintf(chatTimeout, sizeof(chatTimeout), "%d", (int)config::GGPOChatTimeout); ImGui::InputText("Chat Window Timeout (seconds)", chatTimeout, sizeof(chatTimeout), ImGuiInputTextFlags_CharsDecimal, nullptr, nullptr); ImGui::SameLine(); ShowHelpMarker("Sets duration that chat window stays open after new message is received."); @@ -2769,7 +2769,7 @@ static void gui_settings_network() ShowHelpMarker("The server to connect to. Leave blank to find a server automatically on the default port"); } char localPort[256]; - sprintf(localPort, "%d", (int)config::LocalPort); + snprintf(localPort, sizeof(localPort), "%d", (int)config::LocalPort); ImGui::InputText("Local Port", localPort, sizeof(localPort), ImGuiInputTextFlags_CharsDecimal, nullptr, nullptr); ImGui::SameLine(); ShowHelpMarker("The local UDP port to use"); @@ -2781,7 +2781,7 @@ static void gui_settings_network() ImGui::SameLine(); ShowHelpMarker("The peer to connect to. Leave blank to find a player automatically on the default port"); char localPort[256]; - sprintf(localPort, "%d", (int)config::LocalPort); + snprintf(localPort, sizeof(localPort), "%d", (int)config::LocalPort); ImGui::InputText("Local Port", localPort, sizeof(localPort), ImGuiInputTextFlags_CharsDecimal, nullptr, nullptr); ImGui::SameLine(); ShowHelpMarker("The local UDP port to use"); diff --git a/core/ui/gui_util.h b/core/ui/gui_util.h index 02520f77f..a18afbedb 100644 --- a/core/ui/gui_util.h +++ b/core/ui/gui_util.h @@ -280,7 +280,7 @@ static inline bool iconButton(const char *icon, const std::string& label, const { ImguiStyleVar _{ImGuiStyleVar_ButtonTextAlign, ImVec2(0.f, 0.5f)}; // left aligned std::string s(5 + label.size(), '\0'); - s.resize(sprintf(s.data(), "%s %s", icon, label.c_str())); + s.resize(snprintf(s.data(), s.size() + 1, "%s %s", icon, label.c_str())); return ImGui::Button(s.c_str(), size); } @@ -288,7 +288,7 @@ static inline float iconButtonWidth(const char *icon, const std::string& label) { // TODO avoid doing stuff twice std::string s(5 + label.size(), '\0'); - s.resize(sprintf(s.data(), "%s %s", icon, label.c_str())); + s.resize(snprintf(s.data(), s.size() + 1, "%s %s", icon, label.c_str())); return ImGui::CalcTextSize(s.c_str()).x + ImGui::GetStyle().FramePadding.x * 2; } diff --git a/core/windows/winmain.cpp b/core/windows/winmain.cpp index 61b550cf9..9791db292 100644 --- a/core/windows/winmain.cpp +++ b/core/windows/winmain.cpp @@ -174,10 +174,11 @@ static void reserveBottomMemory() // Print diagnostics showing how many allocations we had to make in // order to reserve all of low memory, typically less than 200. char buffer[1000]; - sprintf_s(buffer, "Reserved %1.3f MB (%d vallocs," - "%d heap allocs) of low-memory.\n", - totalReservation / (1024 * 1024.0), - (int)numVAllocs, (int)numHeapAllocs); + snprintf(buffer, sizeof(buffer), + "Reserved %1.3f MB (%d vallocs," + "%d heap allocs) of low-memory.\n", + totalReservation / (1024 * 1024.0), + (int)numVAllocs, (int)numHeapAllocs); OutputDebugStringA(buffer); #endif }