Replace sprintf with snprintf (#1884)

Signed-off-by: Isaac Marovitz <isaacryu@icloud.com>
This commit is contained in:
Isaac Marovitz 2025-03-25 13:47:07 -04:00 committed by GitHub
parent 13436a4dda
commit 5a110fd666
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 100 additions and 96 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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)
{

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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

View File

@ -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)
{

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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
{

View File

@ -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;
}

View File

@ -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");

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -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)

View File

@ -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
}

View File

@ -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);

View File

@ -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

View File

@ -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<uintptr_t>(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; }

View File

@ -1351,7 +1351,7 @@ static void controller_mapping_popup(const std::shared_ptr<GamepadDevice>& 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");

View File

@ -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;
}

View File

@ -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
}