From 6945deec010769cbdab4a077af88a21acdd01de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 28 Apr 2023 21:04:05 +0200 Subject: [PATCH] Replace a LOT of sprintf with snprintf, and a few strcpy with truncate_cpy --- Common/Data/Text/Parsers.cpp | 2 +- Common/GPU/D3D9/thin3d_d3d9.cpp | 4 +- Common/Math/expression_parser.cpp | 12 ++--- Common/Math/expression_parser.h | 2 +- Common/StringUtils.cpp | 4 +- Common/UI/View.cpp | 12 ++--- Core/CoreTiming.cpp | 2 +- Core/Debugger/DebugInterface.h | 10 ++-- Core/Debugger/DisassemblyManager.cpp | 21 ++++---- Core/HLE/HLE.cpp | 2 +- Core/HLE/KernelThreadDebugInterface.h | 8 +-- Core/HLE/sceIo.cpp | 6 +-- Core/HLE/sceKernel.cpp | 2 +- Core/MIPS/ARM/ArmJit.cpp | 2 +- Core/MIPS/JitCommon/JitBlockCache.cpp | 4 +- Core/MIPS/MIPSAnalyst.cpp | 2 +- Core/MIPS/MIPSDebugInterface.cpp | 31 +++++------- Core/MIPS/MIPSDebugInterface.h | 8 +-- Core/MIPS/MIPSTables.cpp | 59 +++++++++++----------- Core/MIPS/MIPSVFPUUtils.cpp | 19 ++++--- Core/Util/PortManager.cpp | 6 +-- Core/WebServer.cpp | 2 +- GPU/Common/DepalettizeShaderCommon.cpp | 24 ++++----- GPU/Common/GPUDebugInterface.cpp | 6 +-- UI/MainScreen.cpp | 6 +-- Windows/Debugger/CtrlDisAsmView.cpp | 2 - Windows/Debugger/CtrlDisAsmView.h | 31 +++++------- Windows/Debugger/CtrlMemView.cpp | 16 +++--- Windows/Debugger/CtrlMemView.h | 2 - Windows/Debugger/CtrlRegisterList.cpp | 16 +++--- Windows/Debugger/CtrlRegisterList.h | 2 - Windows/Debugger/Debugger_MemoryDlg.cpp | 2 - Windows/Debugger/Debugger_MemoryDlg.h | 2 - Windows/Debugger/Debugger_VFPUDlg.cpp | 2 - Windows/Debugger/DumpMemoryWindow.cpp | 15 +++--- Windows/GEDebugger/CtrlDisplayListView.cpp | 4 +- Windows/MainWindow.cpp | 6 +-- Windows/W32Util/ShellUtil.cpp | 5 -- android/jni/app-android.cpp | 2 +- 39 files changed, 170 insertions(+), 193 deletions(-) diff --git a/Common/Data/Text/Parsers.cpp b/Common/Data/Text/Parsers.cpp index fe0eaf6c5d..03470f8f0a 100644 --- a/Common/Data/Text/Parsers.cpp +++ b/Common/Data/Text/Parsers.cpp @@ -43,7 +43,7 @@ bool Version::ParseVersionString(std::string str) { std::string Version::ToString() const { char temp[128]; - sprintf(temp, "%i.%i.%i", major, minor, sub); + snprintf(temp, sizeof(temp), "%i.%i.%i", major, minor, sub); return std::string(temp); } diff --git a/Common/GPU/D3D9/thin3d_d3d9.cpp b/Common/GPU/D3D9/thin3d_d3d9.cpp index a817bebdf7..32152c6988 100644 --- a/Common/GPU/D3D9/thin3d_d3d9.cpp +++ b/Common/GPU/D3D9/thin3d_d3d9.cpp @@ -751,10 +751,10 @@ D3D9Context::D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, ID } if (SUCCEEDED(result)) { - sprintf(shadeLangVersion_, "PS: %04x VS: %04x", d3dCaps_.PixelShaderVersion & 0xFFFF, d3dCaps_.VertexShaderVersion & 0xFFFF); + snprintf(shadeLangVersion_, sizeof(shadeLangVersion_), "PS: %04x VS: %04x", d3dCaps_.PixelShaderVersion & 0xFFFF, d3dCaps_.VertexShaderVersion & 0xFFFF); } else { WARN_LOG(G3D, "Direct3D9: Failed to get the device caps!"); - strcpy(shadeLangVersion_, "N/A"); + truncate_cpy(shadeLangVersion_, "N/A"); } caps_.deviceID = identifier_.DeviceId; diff --git a/Common/Math/expression_parser.cpp b/Common/Math/expression_parser.cpp index b591845bce..a5f48e64c9 100644 --- a/Common/Math/expression_parser.cpp +++ b/Common/Math/expression_parser.cpp @@ -386,13 +386,13 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf { case EXCOMM_CONST: case EXCOMM_CONST_FLOAT: - testPos += sprintf(&test[testPos],"0x%04X ",dest[i].second); + testPos += snprintf(&test[testPos], sizeof(test) - testPos, "0x%04X ", dest[i].second); break; case EXCOMM_REF: - testPos += sprintf(&test[testPos],"r%d ",dest[i].second); + testPos += snprintf(&test[testPos], sizeof(test) - testPos, "r%d ", dest[i].second); break; case EXCOMM_OP: - testPos += sprintf(&test[testPos],"%s ",ExpressionOpcodes[dest[i].second].Name); + testPos += snprintf(&test[testPos], sizeof(test) - testPos, "%s ", ExpressionOpcodes[dest[i].second].Name); break; }; } @@ -451,7 +451,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, } uint32_t val; - if(funcs->getMemoryValue(arg[1],arg[0],val,expressionError) == false) + if(funcs->getMemoryValue(arg[1],arg[0],val,expressionError, sizeof(expressionError)) == false) { return false; } @@ -460,7 +460,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_MEM: { uint32_t val; - if (funcs->getMemoryValue(arg[0],4,val,expressionError) == false) + if (funcs->getMemoryValue(arg[0],4,val,expressionError, sizeof(expressionError)) == false) { return false; } @@ -479,7 +479,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, valueStack.push_back(~arg[0]); break; case EXOP_LOGNOT: // !b - valueStack.push_back(!arg[0]); + valueStack.push_back(!(arg[0] != 0)); break; case EXOP_MUL: // a*b if (useFloat) diff --git a/Common/Math/expression_parser.h b/Common/Math/expression_parser.h index 55de0a234a..07e6f92639 100644 --- a/Common/Math/expression_parser.h +++ b/Common/Math/expression_parser.h @@ -20,7 +20,7 @@ public: virtual bool parseSymbol(char* str, uint32_t& symbolValue) = 0; virtual uint32_t getReferenceValue(uint32_t referenceIndex) = 0; virtual ExpressionType getReferenceType(uint32_t referenceIndex) = 0; - virtual bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char* error) = 0; + virtual bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char *error, size_t errorBufSize) = 0; }; bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, PostfixExpression& dest); diff --git a/Common/StringUtils.cpp b/Common/StringUtils.cpp index d5bbc02068..c691d4223a 100644 --- a/Common/StringUtils.cpp +++ b/Common/StringUtils.cpp @@ -242,7 +242,7 @@ std::string StringFromFormat(const char* format, ...) std::string StringFromInt(int value) { char temp[16]; - sprintf(temp, "%i", value); + snprintf(temp, sizeof(temp), "%d", value); return temp; } @@ -367,4 +367,4 @@ std::string UnescapeMenuString(const char *input, char *shortcutChar) { } } return output; -} \ No newline at end of file +} diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index 5b82ff07af..d8fa404447 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -1284,7 +1284,7 @@ void ProgressBar::GetContentDimensions(const UIContext &dc, float &w, float &h) void ProgressBar::Draw(UIContext &dc) { char temp[32]; - sprintf(temp, "%i%%", (int)(progress_ * 100.0f)); + snprintf(temp, sizeof(temp), "%d%%", (int)(progress_ * 100.0f)); dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y, bounds_.x + bounds_.w * progress_, bounds_.y2(), 0xc0c0c0c0); dc.SetFontStyle(dc.theme->uiFont); dc.DrawTextRect(temp, bounds_, 0xFFFFFFFF, ALIGN_CENTER); @@ -1443,17 +1443,17 @@ void Slider::Draw(UIContext &dc) { dc.Draw()->DrawImage(dc.theme->sliderKnob, knobX, bounds_.centerY(), 1.0f, knobStyle.fgColor, ALIGN_CENTER); char temp[64]; if (showPercent_) - sprintf(temp, "%i%%", *value_); + snprintf(temp, sizeof(temp), "%d%%", *value_); else - sprintf(temp, "%i", *value_); + snprintf(temp, sizeof(temp), "%d", *value_); dc.SetFontStyle(dc.theme->uiFont); dc.DrawText(temp, bounds_.x2() - 22, bounds_.centerY(), dc.theme->popupStyle.fgColor, ALIGN_CENTER | FLAG_DYNAMIC_ASCII); } std::string Slider::DescribeText() const { if (showPercent_) - return StringFromFormat("%i%% / %i%%", *value_, maxValue_); - return StringFromFormat("%i / %i", *value_, maxValue_); + return StringFromFormat("%d%% / %d%%", *value_, maxValue_); + return StringFromFormat("%d / %d", *value_, maxValue_); } void Slider::Update() { @@ -1567,7 +1567,7 @@ void SliderFloat::Draw(UIContext &dc) { dc.FillRect(Drawable(0xFF808080), Bounds(knobX, bounds_.centerY() - 2, (bounds_.x + bounds_.w - paddingRight_ - knobX), 4)); dc.Draw()->DrawImage(dc.theme->sliderKnob, knobX, bounds_.centerY(), 1.0f, knobStyle.fgColor, ALIGN_CENTER); char temp[64]; - sprintf(temp, "%0.2f", *value_); + snprintf(temp, sizeof(temp), "%0.2f", *value_); dc.SetFontStyle(dc.theme->uiFont); dc.DrawText(temp, bounds_.x2() - 22, bounds_.centerY(), dc.theme->popupStyle.fgColor, ALIGN_CENTER); } diff --git a/Core/CoreTiming.cpp b/Core/CoreTiming.cpp index d91a55ab79..b832b4b74e 100644 --- a/Core/CoreTiming.cpp +++ b/Core/CoreTiming.cpp @@ -672,7 +672,7 @@ std::string GetScheduledEventsSummary() { if (!name) name = "[unknown]"; char temp[512]; - sprintf(temp, "%s : %i %08x%08x\n", name, (int)ptr->time, (u32)(ptr->userdata >> 32), (u32)(ptr->userdata)); + snprintf(temp, sizeof(temp), "%s : %i %08x%08x\n", name, (int)ptr->time, (u32)(ptr->userdata >> 32), (u32)(ptr->userdata)); text += temp; ptr = ptr->next; } diff --git a/Core/Debugger/DebugInterface.h b/Core/Debugger/DebugInterface.h index 5422dc5798..a45997a589 100644 --- a/Core/Debugger/DebugInterface.h +++ b/Core/Debugger/DebugInterface.h @@ -54,14 +54,16 @@ public: virtual u32 GetPC() = 0; virtual void SetPC(u32 _pc) = 0; virtual u32 GetLR() {return GetPC();} - virtual void DisAsm(u32 op, u32 pc, int align, char *out) {sprintf(out,"[%08x] UNKNOWN", op);} - //More stuff for debugger + virtual void DisAsm(u32 op, u32 pc, int align, char *out, size_t outSize) { + snprintf(out, outSize, "[%08x] UNKNOWN", op); + } + // More stuff for debugger virtual int GetNumCategories() {return 0;} virtual int GetNumRegsInCategory(int cat) {return 0;} virtual const char *GetCategoryName(int cat) {return 0;} virtual const char *GetRegName(int cat, int index) {return 0;} - virtual void PrintRegValue(int cat, int index, char *out) { - sprintf(out,"%08X",GetGPR32Value(index)); + virtual void PrintRegValue(int cat, int index, char *out, size_t outSize) { + snprintf(out, outSize, "%08X", GetGPR32Value(index)); } virtual u32 GetRegValue(int cat, int index) {return 0;} virtual void SetRegValue(int cat, int index, u32 value) {} diff --git a/Core/Debugger/DisassemblyManager.cpp b/Core/Debugger/DisassemblyManager.cpp index 2d8ca24284..f9bd9c1f0e 100644 --- a/Core/Debugger/DisassemblyManager.cpp +++ b/Core/Debugger/DisassemblyManager.cpp @@ -16,6 +16,7 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "ppsspp_config.h" + #include #include #include @@ -24,6 +25,7 @@ #include "Common/CommonTypes.h" #include "Common/Data/Encoding/Utf8.h" +#include "Common/StringUtils.h" #include "Core/MemMap.h" #include "Core/System.h" #include "Core/MIPS/MIPSCodeUtils.h" @@ -123,14 +125,13 @@ void parseDisasm(const char* disasm, char* opcode, char* arguments, bool insertS if (disasm == jumpAddress) { u32 branchTarget = 0; - sscanf(disasm+3,"%08x",&branchTarget); - + sscanf(disasm+3, "%08x", &branchTarget); const std::string addressSymbol = g_symbolMap->GetLabelString(branchTarget); if (!addressSymbol.empty() && insertSymbols) { - arguments += sprintf(arguments,"%s",addressSymbol.c_str()); + arguments += sprintf(arguments, "%s", addressSymbol.c_str()); } else { - arguments += sprintf(arguments,"0x%08X",branchTarget); + arguments += sprintf(arguments, "0x%08X", branchTarget); } disasm += 3+8; @@ -849,9 +850,9 @@ bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo &dest, bool addressSymbol = g_symbolMap->GetLabelString(immediate); if (!addressSymbol.empty() && insertSymbols) { - sprintf(buffer, "%s,%s", cpuDebug->GetRegName(0, rt), addressSymbol.c_str()); + snprintf(buffer, sizeof(buffer), "%s,%s", cpuDebug->GetRegName(0, rt), addressSymbol.c_str()); } else { - sprintf(buffer, "%s,0x%08X", cpuDebug->GetRegName(0, rt), immediate); + snprintf(buffer, sizeof(buffer), "%s,0x%08X", cpuDebug->GetRegName(0, rt), immediate); } dest.params = buffer; @@ -864,9 +865,9 @@ bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo &dest, bool addressSymbol = g_symbolMap->GetLabelString(immediate); if (!addressSymbol.empty() && insertSymbols) { - sprintf(buffer, "%s,%s", cpuDebug->GetRegName(0, rt), addressSymbol.c_str()); + snprintf(buffer, sizeof(buffer), "%s,%s", cpuDebug->GetRegName(0, rt), addressSymbol.c_str()); } else { - sprintf(buffer, "%s,0x%08X", cpuDebug->GetRegName(0, rt), immediate); + snprintf(buffer, sizeof(buffer), "%s,0x%08X", cpuDebug->GetRegName(0, rt), immediate); } dest.params = buffer; @@ -1001,9 +1002,9 @@ void DisassemblyData::createLines() } else { char buffer[64]; if (pos == end && b == 0) - strcpy(buffer,"0"); + truncate_cpy(buffer, "0"); else - sprintf(buffer,"0x%02X",b); + snprintf(buffer, sizeof(buffer), "0x%02X", b); if (currentLine.size()+strlen(buffer) >= maxChars) { diff --git a/Core/HLE/HLE.cpp b/Core/HLE/HLE.cpp index cf92c9c472..ce7a04c345 100644 --- a/Core/HLE/HLE.cpp +++ b/Core/HLE/HLE.cpp @@ -223,7 +223,7 @@ const char *GetFuncName(const char *moduleName, u32 nib) return func->name; static char temp[256]; - sprintf(temp,"[UNK: 0x%08x]", nib); + snprintf(temp, sizeof(temp), "[UNK: 0x%08x]", nib); return temp; } diff --git a/Core/HLE/KernelThreadDebugInterface.h b/Core/HLE/KernelThreadDebugInterface.h index 55ead24e65..aa48bdb8bb 100644 --- a/Core/HLE/KernelThreadDebugInterface.h +++ b/Core/HLE/KernelThreadDebugInterface.h @@ -32,11 +32,11 @@ public: u32 GetLR() override { return ctx.r[MIPS_REG_RA]; } void SetPC(u32 _pc) override { ctx.pc = _pc; } - void PrintRegValue(int cat, int index, char *out) override { + void PrintRegValue(int cat, int index, char *out, size_t outSize) override { switch (cat) { - case 0: sprintf(out, "%08X", ctx.r[index]); break; - case 1: sprintf(out, "%f", ctx.f[index]); break; - case 2: sprintf(out, "N/A"); break; + case 0: snprintf(out, outSize, "%08X", ctx.r[index]); break; + case 1: snprintf(out, outSize, "%f", ctx.f[index]); break; + case 2: snprintf(out, outSize, "N/A"); break; } } diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 0127c86271..59db5a6923 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -224,8 +224,8 @@ public: const char *GetName() override { return fullpath.c_str(); } const char *GetTypeName() override { return GetStaticTypeName(); } static const char *GetStaticTypeName() { return "OpenFile"; } - void GetQuickInfo(char *ptr, int size) override { - sprintf(ptr, "Seekpos: %08x", (u32)pspFileSystem.GetSeekPos(handle)); + void GetQuickInfo(char *buf, int bufSize) override { + snprintf(buf, bufSize, "Seekpos: %08x", (u32)pspFileSystem.GetSeekPos(handle)); } static u32 GetMissingErrorCode() { return SCE_KERNEL_ERROR_BADF; } static int GetStaticIDType() { return PPSSPP_KERNEL_TMID_File; } @@ -2767,7 +2767,7 @@ static int __IoIoctl(u32 id, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, if (result == (int)SCE_KERNEL_ERROR_ERRNO_FUNCTION_NOT_SUPPORTED) { char temp[256]; // We want the reported message to include the cmd, so it's unique. - sprintf(temp, "sceIoIoctl(%%s, %08x, %%08x, %%x, %%08x, %%x)", cmd); + snprintf(temp, sizeof(temp), "sceIoIoctl(%%s, %08x, %%08x, %%x, %%08x, %%x)", cmd); Reporting::ReportMessage(temp, f->fullpath.c_str(), indataPtr, inlen, outdataPtr, outlen); ERROR_LOG(SCEIO, "UNIMPL 0=sceIoIoctl id: %08x, cmd %08x, indataPtr %08x, inlen %08x, outdataPtr %08x, outLen %08x", id,cmd,indataPtr,inlen,outdataPtr,outlen); } diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 01bc74979b..f83efc63c1 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -523,7 +523,7 @@ void KernelObjectPool::List() { if (occupied[i]) { char buffer[256]; if (pool[i]) { - pool[i]->GetQuickInfo(buffer, 256); + pool[i]->GetQuickInfo(buffer, sizeof(buffer)); INFO_LOG(SCEKERNEL, "KO %i: %s \"%s\": %s", i + handleOffset, pool[i]->GetTypeName(), pool[i]->GetName(), buffer); } else { strcpy(buffer, "WTF? Zero Pointer"); diff --git a/Core/MIPS/ARM/ArmJit.cpp b/Core/MIPS/ARM/ArmJit.cpp index cf1a1de41d..9873130e21 100644 --- a/Core/MIPS/ARM/ArmJit.cpp +++ b/Core/MIPS/ARM/ArmJit.cpp @@ -62,7 +62,7 @@ void DisassembleArm(const u8 *data, int size) { int reg0 = (inst & 0x0000F000) >> 12; int reg1 = (next & 0x0000F000) >> 12; if (reg0 == reg1) { - sprintf(temp, "%08x MOV32 %s, %04x%04x", (u32)inst, ArmRegName(reg0), hi, low); + snprintf(temp, sizeof(temp), "%08x MOV32 %s, %04x%04x", (u32)inst, ArmRegName(reg0), hi, low); INFO_LOG(JIT, "A: %s", temp); i += 4; continue; diff --git a/Core/MIPS/JitCommon/JitBlockCache.cpp b/Core/MIPS/JitCommon/JitBlockCache.cpp index d1498e4731..a08f430ed4 100644 --- a/Core/MIPS/JitCommon/JitBlockCache.cpp +++ b/Core/MIPS/JitCommon/JitBlockCache.cpp @@ -280,13 +280,13 @@ void JitBlockCache::FinalizeBlock(int block_num, bool block_link) { #if defined USE_OPROFILE && USE_OPROFILE char buf[100]; - sprintf(buf, "EmuCode%x", b.originalAddress); + snprintf(buf, sizeof(buf), "EmuCode%x", b.originalAddress); const u8* blockStart = blocks_[block_num].checkedEntry; op_write_native_code(agent, buf, (uint64_t)blockStart, blockStart, b.normalEntry + b.codeSize - b.checkedEntry); #endif #ifdef USE_VTUNE - sprintf(b.blockName, "EmuCode_0x%08x", b.originalAddress); + snprintf(b.blockName, sizeof(b.blockName), "EmuCode_0x%08x", b.originalAddress); iJIT_Method_Load jmethod = {0}; jmethod.method_id = iJIT_GetNewMethodID(); diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index f39dac8738..dff4bb7f4a 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -930,7 +930,7 @@ skip: } static const char *DefaultFunctionName(char buffer[256], u32 startAddr) { - sprintf(buffer, "z_un_%08x", startAddr); + snprintf(buffer, sizeof(buffer), "z_un_%08x", startAddr); return buffer; } diff --git a/Core/MIPS/MIPSDebugInterface.cpp b/Core/MIPS/MIPSDebugInterface.cpp index ab415a384a..69b3c8e970 100644 --- a/Core/MIPS/MIPSDebugInterface.cpp +++ b/Core/MIPS/MIPSDebugInterface.cpp @@ -58,7 +58,7 @@ public: for (int i = 0; i < 32; i++) { char reg[8]; - sprintf(reg, "r%d", i); + snprintf(reg, sizeof(reg), "r%d", i); if (strcasecmp(str, reg) == 0 || strcasecmp(str, cpu->GetRegName(0, i)) == 0) { @@ -71,7 +71,7 @@ public: return true; } - sprintf(reg, "fi%d", i); + snprintf(reg, sizeof(reg), "fi%d", i); if (strcasecmp(str, reg) == 0) { referenceIndex = REF_INDEX_FPU_INT | i; @@ -88,7 +88,7 @@ public: } char reg[8]; - sprintf(reg, "vi%d", i); + snprintf(reg, sizeof(reg), "vi%d", i); if (strcasecmp(str, reg) == 0) { referenceIndex = REF_INDEX_VFPU_INT | i; @@ -159,7 +159,7 @@ public: return EXPR_TYPE_UINT; } - bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char* error) override { + bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char* error, size_t errorBufSize) override { // We allow, but ignore, bad access. // If we didn't, log/condition statements that reference registers couldn't be configured. uint32_t valid = Memory::ValidSize(address, size); @@ -179,7 +179,7 @@ public: return true; } - sprintf(error, "Unexpected memory access size %d", size); + snprintf(error, errorBufSize, "Unexpected memory access size %d", size); return false; } @@ -264,7 +264,7 @@ const char *MIPSDebugInterface::GetName() return ("R4"); } - +// NOT threadsafe. const char *MIPSDebugInterface::GetRegName(int cat, int index) { static const char *regName[32] = { @@ -279,24 +279,21 @@ const char *MIPSDebugInterface::GetRegName(int cat, int index) }; // really nasty hack so that this function can be called several times on one line of c++. - static int access=0; + static int access = 0; access++; access &= 3; static char temp[4][16]; - if (cat == 0) + if (cat == 0) { return regName[index]; - else if (cat == 1) - { - sprintf(temp[access],"f%i",index); + } else if (cat == 1) { + snprintf(temp[access], sizeof(temp[access]), "f%d", index); return temp[access]; - } - else if (cat == 2) - { - sprintf(temp[access],"v%03x",index); + } else if (cat == 2) { + snprintf(temp[access], sizeof(temp[access]), "v%03x", index); return temp[access]; - } - else + } else { return "???"; + } } diff --git a/Core/MIPS/MIPSDebugInterface.h b/Core/MIPS/MIPSDebugInterface.h index 03324a36c2..36651c2f16 100644 --- a/Core/MIPS/MIPSDebugInterface.h +++ b/Core/MIPS/MIPSDebugInterface.h @@ -64,11 +64,11 @@ public: } const char *GetRegName(int cat, int index) override; - void PrintRegValue(int cat, int index, char *out) override { + void PrintRegValue(int cat, int index, char *out, size_t outSize) override { switch (cat) { - case 0: sprintf(out, "%08X", cpu->r[index]); break; - case 1: sprintf(out, "%f", cpu->f[index]); break; - case 2: sprintf(out, "N/A"); break; + case 0: snprintf(out, outSize, "%08X", cpu->r[index]); break; + case 1: snprintf(out, outSize, "%f", cpu->f[index]); break; + case 2: snprintf(out, outSize, "N/A"); break; } } diff --git a/Core/MIPS/MIPSTables.cpp b/Core/MIPS/MIPSTables.cpp index 996a54daf7..f5b4af34c7 100644 --- a/Core/MIPS/MIPSTables.cpp +++ b/Core/MIPS/MIPSTables.cpp @@ -15,6 +15,8 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include "Common/StringUtils.h" + #include "Core/Core.h" #include "Core/System.h" #include "Core/MemMap.h" @@ -90,7 +92,7 @@ using namespace MIPSComp; // %s/&Jit::\(.\{-}\),/JITFUNC(\1),/g // regregreg instructions -const MIPSInstruction tableImmediate[64] = // xxxxxx ..... ..... ................ +static const MIPSInstruction tableImmediate[64] = // xxxxxx ..... ..... ................ { //0 ENCODING(Spec), @@ -169,7 +171,7 @@ const MIPSInstruction tableImmediate[64] = // xxxxxx ..... ..... ............... INSTR("vflush", JITFUNC(Comp_DoNothing), Dis_Vflush, Int_Vflush, IS_VFPU|VFPU_NO_PREFIX), }; -const MIPSInstruction tableSpecial[64] = // 000000 ..... ..... ..... ..... xxxxxx +static const MIPSInstruction tableSpecial[64] = // 000000 ..... ..... ..... ..... xxxxxx { INSTR("sll", JITFUNC(Comp_ShiftType), Dis_ShiftType, Int_ShiftType, OUT_RD|IN_RT|IN_SA), INVALID, // copu @@ -246,7 +248,7 @@ const MIPSInstruction tableSpecial[64] = // 000000 ..... ..... ..... ..... xxxxx }; // Theoretically should not hit these. -const MIPSInstruction tableSpecial2[64] = // 011100 ..... ..... ..... ..... xxxxxx +static const MIPSInstruction tableSpecial2[64] = // 011100 ..... ..... ..... ..... xxxxxx { INSTR("halt", JITFUNC(Comp_Generic), Dis_Generic, 0, 0), INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, @@ -266,7 +268,7 @@ const MIPSInstruction tableSpecial2[64] = // 011100 ..... ..... ..... ..... xxxx INVALID_X_8, }; -const MIPSInstruction tableSpecial3[64] = // 011111 ..... ..... ..... ..... xxxxxx +static const MIPSInstruction tableSpecial3[64] = // 011111 ..... ..... ..... ..... xxxxxx { INSTR("ext", JITFUNC(Comp_Special3), Dis_Special3, Int_Special3, IN_RS|OUT_RT), INVALID, @@ -296,7 +298,7 @@ const MIPSInstruction tableSpecial3[64] = // 011111 ..... ..... ..... ..... xxxx INVALID, INVALID, INVALID, INVALID, }; -const MIPSInstruction tableRegImm[32] = // 000001 ..... xxxxx ................ +static const MIPSInstruction tableRegImm[32] = // 000001 ..... xxxxx ................ { INSTR("bltz", JITFUNC(Comp_RelBranchRI), Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_IMM16|IN_RS|DELAYSLOT|CONDTYPE_LTZ), INSTR("bgez", JITFUNC(Comp_RelBranchRI), Dis_RelBranch, Int_RelBranchRI, IS_CONDBRANCH|IN_IMM16|IN_RS|DELAYSLOT|CONDTYPE_GEZ), @@ -329,7 +331,7 @@ const MIPSInstruction tableRegImm[32] = // 000001 ..... xxxxx ................ INSTR("synci", JITFUNC(Comp_Generic), Dis_Generic, 0, 0), }; -const MIPSInstruction tableCop2[32] = // 010010 xxxxx ..... ................ +static const MIPSInstruction tableCop2[32] = // 010010 xxxxx ..... ................ { INSTR("mfc2", JITFUNC(Comp_Generic), Dis_Generic, 0, OUT_RT), INVALID, @@ -353,7 +355,7 @@ const MIPSInstruction tableCop2[32] = // 010010 xxxxx ..... ................ INVALID_X_8, }; -const MIPSInstruction tableCop2BC2[4] = // 010010 01000 ...xx ................ +static const MIPSInstruction tableCop2BC2[4] = // 010010 01000 ...xx ................ { INSTR("bvf", JITFUNC(Comp_VBranch), Dis_VBranch, Int_VBranch, IS_CONDBRANCH|IN_IMM16|IN_VFPU_CC|DELAYSLOT|IS_VFPU), INSTR("bvt", JITFUNC(Comp_VBranch), Dis_VBranch, Int_VBranch, IS_CONDBRANCH|IN_IMM16|IN_VFPU_CC|DELAYSLOT|IS_VFPU), @@ -361,7 +363,7 @@ const MIPSInstruction tableCop2BC2[4] = // 010010 01000 ...xx ................ INSTR("bvtl", JITFUNC(Comp_VBranch), Dis_VBranch, Int_VBranch, IS_CONDBRANCH|IN_IMM16|IN_VFPU_CC|DELAYSLOT|LIKELY|IS_VFPU), }; -const MIPSInstruction tableCop0[32] = // 010000 xxxxx ..... ................ +static const MIPSInstruction tableCop0[32] = // 010000 xxxxx ..... ................ { INSTR("mfc0", JITFUNC(Comp_Generic), Dis_Generic, 0, OUT_RT), // unused INVALID, @@ -387,7 +389,7 @@ const MIPSInstruction tableCop0[32] = // 010000 xxxxx ..... ................ }; // we won't encounter these since we only do user mode emulation -const MIPSInstruction tableCop0CO[64] = // 010000 1.... ..... ..... ..... xxxxxx +static const MIPSInstruction tableCop0CO[64] = // 010000 1.... ..... ..... ..... xxxxxx { INVALID, INSTR("tlbr", JITFUNC(Comp_Generic), Dis_Generic, 0, 0), @@ -415,7 +417,7 @@ const MIPSInstruction tableCop0CO[64] = // 010000 1.... ..... ..... ..... xxxxxx INVALID_X_8, }; -const MIPSInstruction tableCop1[32] = // 010001 xxxxx ..... ..... ........... +static const MIPSInstruction tableCop1[32] = // 010001 xxxxx ..... ..... ........... { INSTR("mfc1", JITFUNC(Comp_mxc1), Dis_mxc1, Int_mxc1, IN_FS|OUT_RT|IS_FPU), INVALID, @@ -434,7 +436,7 @@ const MIPSInstruction tableCop1[32] = // 010001 xxxxx ..... ..... ........... INVALID_X_8, }; -const MIPSInstruction tableCop1BC[32] = // 010001 01000 xxxxx ................ +static const MIPSInstruction tableCop1BC[32] = // 010001 01000 xxxxx ................ { INSTR("bc1f", JITFUNC(Comp_FPUBranch), Dis_FPUBranch, Int_FPUBranch, IS_CONDBRANCH|IN_IMM16|IN_FPUFLAG|DELAYSLOT|CONDTYPE_FPUFALSE|IS_FPU), INSTR("bc1t", JITFUNC(Comp_FPUBranch), Dis_FPUBranch, Int_FPUBranch, IS_CONDBRANCH|IN_IMM16|IN_FPUFLAG|DELAYSLOT|CONDTYPE_FPUTRUE|IS_FPU), @@ -447,7 +449,7 @@ const MIPSInstruction tableCop1BC[32] = // 010001 01000 xxxxx ................ INVALID_X_8, }; -const MIPSInstruction tableCop1S[64] = // 010001 10000 ..... ..... ..... xxxxxx +static const MIPSInstruction tableCop1S[64] = // 010001 10000 ..... ..... ..... xxxxxx { INSTR("add.s", JITFUNC(Comp_FPU3op), Dis_FPU3op, Int_FPU3op, OUT_FD|IN_FS|IN_FT|IS_FPU), INSTR("sub.s", JITFUNC(Comp_FPU3op), Dis_FPU3op, Int_FPU3op, OUT_FD|IN_FS|IN_FT|IS_FPU), @@ -495,7 +497,7 @@ const MIPSInstruction tableCop1S[64] = // 010001 10000 ..... ..... ..... xxxxxx INSTR("c.ngt", JITFUNC(Comp_FPUComp), Dis_FPUComp, Int_FPUComp, IN_FS|IN_FT|OUT_FPUFLAG|IS_FPU), }; -const MIPSInstruction tableCop1W[64] = // 010001 10100 ..... ..... ..... xxxxxx +static const MIPSInstruction tableCop1W[64] = // 010001 10100 ..... ..... ..... xxxxxx { INVALID_X_8, //8 @@ -519,7 +521,7 @@ const MIPSInstruction tableCop1W[64] = // 010001 10100 ..... ..... ..... xxxxxx INVALID_X_8, }; -const MIPSInstruction tableVFPU0[8] = // 011000 xxx ....... . ....... . ....... +static const MIPSInstruction tableVFPU0[8] = // 011000 xxx ....... . ....... . ....... { INSTR("vadd", JITFUNC(Comp_VecDo3), Dis_VectorSet3, Int_VecDo3, MIPSInfo(IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX, 2)), INSTR("vsub", JITFUNC(Comp_VecDo3), Dis_VectorSet3, Int_VecDo3, MIPSInfo(IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX, 2)), @@ -530,7 +532,7 @@ const MIPSInstruction tableVFPU0[8] = // 011000 xxx ....... . ....... . ....... INSTR("vdiv", JITFUNC(Comp_VecDo3), Dis_VectorSet3, Int_VecDo3, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), }; -const MIPSInstruction tableVFPU1[8] = // 011001 xxx ....... . ....... . ....... +static const MIPSInstruction tableVFPU1[8] = // 011001 xxx ....... . ....... . ....... { INSTR("vmul", JITFUNC(Comp_VecDo3), Dis_VectorSet3, Int_VecDo3, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), INSTR("vdot", JITFUNC(Comp_VDot), Dis_VectorDot, Int_VDot, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), @@ -542,7 +544,7 @@ const MIPSInstruction tableVFPU1[8] = // 011001 xxx ....... . ....... . ....... INVALID, }; -const MIPSInstruction tableVFPU3[8] = // 011011 xxx ....... . ....... . ....... +static const MIPSInstruction tableVFPU3[8] = // 011011 xxx ....... . ....... . ....... { INSTR("vcmp", JITFUNC(Comp_Vcmp), Dis_Vcmp, Int_Vcmp, IN_OTHER|OUT_VFPU_CC|IS_VFPU|OUT_EAT_PREFIX), INVALID, @@ -554,7 +556,7 @@ const MIPSInstruction tableVFPU3[8] = // 011011 xxx ....... . ....... . ....... INSTR("vslt", JITFUNC(Comp_VecDo3), Dis_VectorSet3, Int_Vslt, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), }; -const MIPSInstruction tableVFPU4Jump[32] = // 110100 xxxxx ..... . ....... . ....... +static const MIPSInstruction tableVFPU4Jump[32] = // 110100 xxxxx ..... . ....... . ....... { ENCODING(VFPU4), ENCODING(VFPU7), @@ -586,7 +588,7 @@ const MIPSInstruction tableVFPU4Jump[32] = // 110100 xxxxx ..... . ....... . ... INSTR("vwbn", JITFUNC(Comp_Generic), Dis_Vwbn, Int_Vwbn, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), }; -const MIPSInstruction tableVFPU7[32] = // 110100 00001 xxxxx . ....... . ....... +static const MIPSInstruction tableVFPU7[32] = // 110100 00001 xxxxx . ....... . ....... { INSTR("vrnds", JITFUNC(Comp_Generic), Dis_Vrnds, Int_Vrnds, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), INSTR("vrndi", JITFUNC(Comp_Generic), Dis_VrndX, Int_VrndX, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), @@ -621,7 +623,7 @@ const MIPSInstruction tableVFPU7[32] = // 110100 00001 xxxxx . ....... . ....... // 110100 00000 10100 0000000000000000 // 110100 00000 10111 0000000000000000 -const MIPSInstruction tableVFPU4[32] = // 110100 00000 xxxxx . ....... . ....... +static const MIPSInstruction tableVFPU4[32] = // 110100 00000 xxxxx . ....... . ....... { INSTR("vmov", JITFUNC(Comp_VV2Op), Dis_VectorSet2, Int_VV2Op, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), INSTR("vabs", JITFUNC(Comp_VV2Op), Dis_VectorSet2, Int_VV2Op, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), @@ -651,7 +653,7 @@ const MIPSInstruction tableVFPU4[32] = // 110100 00000 xxxxx . ....... . ....... INVALID, INVALID, INVALID, }; -MIPSInstruction tableVFPU5[8] = // 110111 xxx ....... ................ +static const MIPSInstruction tableVFPU5[8] = // 110111 xxx ....... ................ { INSTR("vpfxs", JITFUNC(Comp_VPFX), Dis_VPFXST, Int_VPFX, IN_IMM16|OUT_OTHER|IS_VFPU), INSTR("vpfxs", JITFUNC(Comp_VPFX), Dis_VPFXST, Int_VPFX, IN_IMM16|OUT_OTHER|IS_VFPU), @@ -663,7 +665,7 @@ MIPSInstruction tableVFPU5[8] = // 110111 xxx ....... ................ INSTR("vfim.s", JITFUNC(Comp_Vfim), Dis_Viim, Int_Viim, IN_IMM16|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), }; -const MIPSInstruction tableVFPU6[32] = // 111100 xxxxx ..... . ....... . ....... +static const MIPSInstruction tableVFPU6[32] = // 111100 xxxxx ..... . ....... . ....... { //0 INSTR("vmmul", JITFUNC(Comp_Vmmul), Dis_MatrixMult, Int_Vmmul, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), @@ -708,7 +710,7 @@ const MIPSInstruction tableVFPU6[32] = // 111100 xxxxx ..... . ....... . ....... }; // TODO: Should this only be when bit 20 is 0? -const MIPSInstruction tableVFPUMatrixSet1[16] = // 111100 11100 .xxxx . ....... . ....... (rm x is 16) +static const MIPSInstruction tableVFPUMatrixSet1[16] = // 111100 11100 .xxxx . ....... . ....... (rm x is 16) { INSTR("vmmov", JITFUNC(Comp_Vmmov), Dis_MatrixSet2, Int_Vmmov, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), INVALID, @@ -723,7 +725,7 @@ const MIPSInstruction tableVFPUMatrixSet1[16] = // 111100 11100 .xxxx . ....... INVALID_X_8, }; -const MIPSInstruction tableVFPU9[32] = // 110100 00010 xxxxx . ....... . ....... +static const MIPSInstruction tableVFPU9[32] = // 110100 00010 xxxxx . ....... . ....... { INSTR("vsrt1", JITFUNC(Comp_Generic), Dis_Vbfy, Int_Vsrt1, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), INSTR("vsrt2", JITFUNC(Comp_Generic), Dis_Vbfy, Int_Vsrt2, IN_OTHER|OUT_OTHER|IS_VFPU|OUT_EAT_PREFIX), @@ -763,7 +765,7 @@ const MIPSInstruction tableVFPU9[32] = // 110100 00010 xxxxx . ....... . ....... INVALID, INVALID, INVALID, INVALID, }; -const MIPSInstruction tableALLEGREX0[32] = // 011111 ..... ..... ..... xxxxx 100000 - or ending with 011000? +static const MIPSInstruction tableALLEGREX0[32] = // 011111 ..... ..... ..... xxxxx 100000 - or ending with 011000? { INVALID, INVALID, @@ -794,8 +796,7 @@ const MIPSInstruction tableALLEGREX0[32] = // 011111 ..... ..... ..... xxxxx 10 INVALID, }; - -const MIPSInstruction tableEMU[4] = { +static const MIPSInstruction tableEMU[4] = { INSTR("RUNBLOCK", JITFUNC(Comp_RunBlock), Dis_Emuhack, Int_Emuhack, 0xFFFFFFFF), INSTR("RetKrnl", 0, Dis_Emuhack, Int_Emuhack, 0), INSTR("CallRepl", JITFUNC(Comp_ReplacementFunc), Dis_Emuhack, Int_Emuhack, 0), @@ -810,7 +811,7 @@ struct EncodingBitsInfo { u32 mask; }; -const EncodingBitsInfo encodingBits[NumEncodings] = { +static const EncodingBitsInfo encodingBits[NumEncodings] = { EncodingBitsInfo(26, 6), //IMME EncodingBitsInfo(0, 6), //Special EncodingBitsInfo(0, 6), //special2 @@ -840,7 +841,7 @@ const EncodingBitsInfo encodingBits[NumEncodings] = { EncodingBitsInfo(0, 0), //Rese }; -const MIPSInstruction *mipsTables[NumEncodings] = { +static const MIPSInstruction *mipsTables[NumEncodings] = { tableImmediate, tableSpecial, tableSpecial2, @@ -919,7 +920,7 @@ void MIPSCompileOp(MIPSOpcode op, MIPSComp::MIPSFrontendInterface *jit) { void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, bool tabsToSpaces) { if (op == 0) { - sprintf(out,"nop"); + strcpy(out, "nop"); } else { disPC = pc; const MIPSInstruction *instr = MIPSGetInstruction(op); diff --git a/Core/MIPS/MIPSVFPUUtils.cpp b/Core/MIPS/MIPSVFPUUtils.cpp index 90e9a63cbc..e80bb77ac8 100644 --- a/Core/MIPS/MIPSVFPUUtils.cpp +++ b/Core/MIPS/MIPSVFPUUtils.cpp @@ -540,7 +540,7 @@ MatrixOverlapType GetMatrixOverlap(int mtx1, int mtx2, MatrixSize msize) { const char *GetVectorNotation(int reg, VectorSize size) { - static char hej[4][16]; + static char temp[4][16]; static int yo = 0; yo++; yo &= 3; int mtx = (reg>>2)&7; @@ -548,8 +548,7 @@ const char *GetVectorNotation(int reg, VectorSize size) int row = 0; int transpose = (reg>>5)&1; char c; - switch (size) - { + switch (size) { case V_Single: transpose=0; c='S'; row=(reg>>5)&3; break; case V_Pair: c='C'; row=(reg>>5)&2; break; case V_Triple: c='C'; row=(reg>>6)&1; break; @@ -558,15 +557,15 @@ const char *GetVectorNotation(int reg, VectorSize size) } if (transpose && c == 'C') c='R'; if (transpose) - sprintf(hej[yo],"%c%i%i%i",c,mtx,row,col); + snprintf(temp[yo], sizeof(temp[yo]), "%c%i%i%i",c,mtx,row,col); else - sprintf(hej[yo],"%c%i%i%i",c,mtx,col,row); - return hej[yo]; + snprintf(temp[yo], sizeof(temp[yo]), "%c%i%i%i",c,mtx,col,row); + return temp[yo]; } const char *GetMatrixNotation(int reg, MatrixSize size) { - static char hej[4][16]; + static char temp[4][16]; static int yo=0;yo++;yo&=3; int mtx = (reg>>2)&7; int col = reg&3; @@ -582,10 +581,10 @@ const char *GetMatrixNotation(int reg, MatrixSize size) } if (transpose && c=='M') c='E'; if (transpose) - sprintf(hej[yo],"%c%i%i%i",c,mtx,row,col); + snprintf(temp[yo], sizeof(temp[yo]), "%c%i%i%i",c,mtx,row,col); else - sprintf(hej[yo],"%c%i%i%i",c,mtx,col,row); - return hej[yo]; + snprintf(temp[yo], sizeof(temp[yo]), "%c%i%i%i",c,mtx,col,row); + return temp[yo]; } bool GetVFPUCtrlMask(int reg, u32 *mask) { diff --git a/Core/Util/PortManager.cpp b/Core/Util/PortManager.cpp index f9359458cf..1a45900705 100644 --- a/Core/Util/PortManager.cpp +++ b/Core/Util/PortManager.cpp @@ -220,8 +220,8 @@ bool PortManager::Add(const char* protocol, unsigned short port, unsigned short Terminate(); return false; } - sprintf(port_str, "%d", port); - sprintf(intport_str, "%d", intport); + snprintf(port_str, sizeof(port_str), "%d", port); + snprintf(intport_str, sizeof(intport_str), "%d", intport); // Only add new port map if it's not previously created by PPSSPP for current IP auto el_it = std::find_if(m_portList.begin(), m_portList.end(), [port_str, protocol](const std::pair &el) { return el.first == port_str && el.second == protocol; }); @@ -275,7 +275,7 @@ bool PortManager::Remove(const char* protocol, unsigned short port) { Terminate(); return false; } - sprintf(port_str, "%d", port); + snprintf(port_str, sizeof(port_str), "%d", port); int r = UPNP_DeletePortMapping(urls->controlURL, datas->first.servicetype, port_str, protocol, NULL); if (r != 0) { diff --git a/Core/WebServer.cpp b/Core/WebServer.cpp index 5784827021..9db7b0f5fd 100644 --- a/Core/WebServer.cpp +++ b/Core/WebServer.cpp @@ -191,7 +191,7 @@ static void DiscHandler(const http::Request &request, const Path &filename) { s64 len = last - begin + 1; char contentRange[1024]; - sprintf(contentRange, "Content-Range: bytes %lld-%lld/%lld\r\n", begin, last, sz); + snprintf(contentRange, sizeof(contentRange), "Content-Range: bytes %lld-%lld/%lld\r\n", begin, last, sz); request.WriteHttpResponseHeader("1.0", 206, len, "application/octet-stream", contentRange); const size_t CHUNK_SIZE = 16 * 1024; diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index f4e7e7a9fa..bb78b80875 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -184,9 +184,9 @@ void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) { if (shift == 0 && mask == 0xFF) { // Easy peasy. if (writer.Lang().shaderLanguage == HLSL_D3D9) - sprintf(lookupMethod, "index.a"); + snprintf(lookupMethod, sizeof(lookupMethod), "index.a"); else - sprintf(lookupMethod, "index.r"); + snprintf(lookupMethod, sizeof(lookupMethod), "index.r"); formatOK = true; } else { // Deal with this if we find it. @@ -199,9 +199,9 @@ void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) { const char *rgba = "rrrrrrrrggggggggbbbbbbbbaaaaaaaa"; const u8 rgba_shift = shift & 7; if (rgba_shift == 0 && mask == 0xFF) { - sprintf(lookupMethod, "index.%c", rgba[shift]); + snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]); } else { - sprintf(lookupMethod, "mod(index.%c * %f, %d.0)", rgba[shift], 255.99f / (1 << rgba_shift), mask + 1); + snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 255.99f / (1 << rgba_shift), mask + 1); index_multiplier = 1.0f / 256.0f; // Format was OK if there weren't bits from another component. formatOK = mask <= 255 - (1 << rgba_shift); @@ -215,11 +215,11 @@ void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) { const char *rgba = "rrrrggggbbbbaaaa"; const u8 rgba_shift = shift & 3; if (rgba_shift == 0 && mask == 0xF) { - sprintf(lookupMethod, "index.%c", rgba[shift]); + snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]); index_multiplier = 15.0f / 256.0f; } else { // Let's divide and mod to get the right bits. A common case is shift=0, mask=01. - sprintf(lookupMethod, "mod(index.%c * %f, %d.0)", rgba[shift], 15.99f / (1 << rgba_shift), mask + 1); + snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 15.99f / (1 << rgba_shift), mask + 1); index_multiplier = 1.0f / 256.0f; formatOK = mask <= 15 - (1 << rgba_shift); } @@ -234,12 +234,12 @@ void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) { const char *rgba = "rrrrrggggggbbbbb"; const u8 rgba_shift = shifts[shift]; if (rgba_shift == 0 && mask == multipliers[shift]) { - sprintf(lookupMethod, "index.%c", rgba[shift]); + snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]); index_multiplier = multipliers[shift] / 256.0f; } else { // We just need to divide the right component by the right value, and then mod against the mask. // A common case is shift=1, mask=0f. - sprintf(lookupMethod, "mod(index.%c * %f, %d.0)", rgba[shift], ((float)multipliers[shift] + 0.99f) / (1 << rgba_shift), mask + 1); + snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], ((float)multipliers[shift] + 0.99f) / (1 << rgba_shift), mask + 1); index_multiplier = 1.0f / 256.0f; formatOK = mask <= multipliers[shift] - (1 << rgba_shift); } @@ -250,21 +250,21 @@ void GenerateDepalShaderFloat(ShaderWriter &writer, const DepalConfig &config) { case GE_FORMAT_5551: if (config.textureFormat == GE_TFMT_CLUT8 && mask == 0xFF && shift == 0) { // Follow the intent here, and ignore g (and let's not round unnecessarily). - sprintf(lookupMethod, "floor(floor(index.a) * 128.0 + index.b * 64.0)"); + snprintf(lookupMethod, sizeof(lookupMethod), "floor(floor(index.a) * 128.0 + index.b * 64.0)"); index_multiplier = 1.0f / 256.0f; // SOCOM case. #16210 } else if ((mask & (mask + 1)) == 0 && shift < 16) { const char *rgba = "rrrrrgggggbbbbba"; const u8 rgba_shift = shift % 5; if (rgba_shift == 0 && mask == 0x1F) { - sprintf(lookupMethod, "index.%c", rgba[shift]); + snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]); index_multiplier = 31.0f / 256.0f; } else if (shift == 15 && mask == 1) { - sprintf(lookupMethod, "index.%c", rgba[shift]); + snprintf(lookupMethod, sizeof(lookupMethod), "index.%c", rgba[shift]); index_multiplier = 1.0f / 256.0f; } else { // A isn't possible here. - sprintf(lookupMethod, "mod(index.%c * %f, %d.0)", rgba[shift], 31.99f / (1 << rgba_shift), mask + 1); + snprintf(lookupMethod, sizeof(lookupMethod), "mod(index.%c * %f, %d.0)", rgba[shift], 31.99f / (1 << rgba_shift), mask + 1); index_multiplier = 1.0f / 256.0f; formatOK = mask <= 31 - (1 << rgba_shift); } diff --git a/GPU/Common/GPUDebugInterface.cpp b/GPU/Common/GPUDebugInterface.cpp index 49b6725743..c6df47e932 100644 --- a/GPU/Common/GPUDebugInterface.cpp +++ b/GPU/Common/GPUDebugInterface.cpp @@ -516,7 +516,7 @@ public: bool parseSymbol(char *str, uint32_t &symbolValue) override; uint32_t getReferenceValue(uint32_t referenceIndex) override; ExpressionType getReferenceType(uint32_t referenceIndex) override; - bool getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error) override; + bool getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error, size_t errorBufSize) override; private: bool parseFieldReference(const char *ref, const char *field, uint32_t &referenceIndex); @@ -926,7 +926,7 @@ ExpressionType GEExpressionFunctions::getFieldType(GECmdFormat fmt, GECmdField f return EXPR_TYPE_UINT; } -bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error) { +bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error, size_t errorBufSize) { // We allow, but ignore, bad access. // If we didn't, log/condition statements that reference registers couldn't be configured. uint32_t valid = Memory::ValidSize(address, size); @@ -946,7 +946,7 @@ bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t return true; } - sprintf(error, "Unexpected memory access size %d", size); + snprintf(error, errorBufSize, "Unexpected memory access size %d", size); return false; } diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 09c75f64a6..9d0b5dbd94 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -322,9 +322,9 @@ void GameButton::Draw(UIContext &dc) { char discNumInfo[8]; if (ginfo->disc_total > 1) - sprintf(discNumInfo, "-DISC%d", ginfo->disc_number); + snprintf(discNumInfo, sizeof(discNumInfo), "-DISC%d", ginfo->disc_number); else - strcpy(discNumInfo, ""); + discNumInfo[0] = '\0'; dc.Draw()->Flush(); dc.RebindTexture(); @@ -1120,7 +1120,7 @@ void MainScreen::CreateViews() { rightColumn->Add(rightColumnItems); char versionString[256]; - sprintf(versionString, "%s", PPSSPP_GIT_VERSION); + snprintf(versionString, sizeof(versionString), "%s", PPSSPP_GIT_VERSION); rightColumnItems->SetSpacing(0.0f); AnchorLayout *logos = new AnchorLayout(new AnchorLayoutParams(FILL_PARENT, 60.0f, false)); if (System_GetPropertyBool(SYSPROP_APP_GOLD)) { diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index ee637d6890..e8d176b1bc 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #include "Windows/resource.h" #include "Core/MemMap.h" #include "Core/MIPS/JitCommon/JitCommon.h" diff --git a/Windows/Debugger/CtrlDisAsmView.h b/Windows/Debugger/CtrlDisAsmView.h index c67306a76c..eca66393dc 100644 --- a/Windows/Debugger/CtrlDisAsmView.h +++ b/Windows/Debugger/CtrlDisAsmView.h @@ -1,25 +1,22 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #pragma once -////////////////////////////////////////////////////////////////////////// -//CtrlDisAsmView -// CtrlDisAsmView.cpp -////////////////////////////////////////////////////////////////////////// -//This Win32 control is made to be flexible and usable with -//every kind of CPU architechture that has fixed width instruction words. -//Just supply it an instance of a class derived from Debugger, with all methods -//overridden for full functionality. Look at the ppc one for an example. -// -//To add to a dialog box, just draw a User Control in the dialog editor, -//and set classname to "CtrlDisAsmView". you also need to call CtrlDisAsmView::init() -//before opening this dialog, to register the window class. -// -//To get a class instance to be able to access it, just use -// CtrlDisAsmView::getFrom(GetDlgItem(yourdialog, IDC_yourid)). +// CtrlDisAsmView +// +// This Win32 control is made to be flexible and usable with +// every kind of CPU architecture that has fixed width instruction words. +// Just supply it an instance of a class derived from Debugger, with all methods +// overridden for full functionality. Look at the ppc one for an example. +// +// To add to a dialog box, just draw a User Control in the dialog editor, +// and set classname to "CtrlDisAsmView". you also need to call CtrlDisAsmView::init() +// before opening this dialog, to register the window class. +// +// To get a class instance to be able to access it, just use +// CtrlDisAsmView::getFrom(GetDlgItem(yourdialog, IDC_yourid)). #include #include + #include "Common/CommonWindows.h" #include "Common/Log.h" #include "Core/Debugger/DebugInterface.h" diff --git a/Windows/Debugger/CtrlMemView.cpp b/Windows/Debugger/CtrlMemView.cpp index 4890531233..12e4f85d1c 100644 --- a/Windows/Debugger/CtrlMemView.cpp +++ b/Windows/Debugger/CtrlMemView.cpp @@ -1,11 +1,11 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #include #include -#include +#include #include #include + #include "ext/xxhash.h" +#include "Common/StringUtils.h" #include "Core/Config.h" #include "Core/MemMap.h" #include "Core/Reporting.h" @@ -229,7 +229,7 @@ void CtrlMemView::onPaint(WPARAM wParam, LPARAM lParam) { char temp[32]; uint32_t address = windowStart_ + i * rowSize_; - sprintf(temp, "%08X", address); + snprintf(temp, sizeof(temp), "%08X", address); setTextColors(0x600000, standardBG); TextOutA(hdc, addressStartX_, rowY, temp, (int)strlen(temp)); @@ -260,12 +260,12 @@ void CtrlMemView::onPaint(WPARAM wParam, LPARAM lParam) { char c; if (valid) { - sprintf(temp, "%02X ", memory.bytes[j]); + snprintf(temp, sizeof(temp), "%02X ", memory.bytes[j]); c = (char)memory.bytes[j]; if (memory.bytes[j] < 32 || memory.bytes[j] >= 128) c = '.'; } else { - strcpy(temp, "??"); + truncate_cpy(temp, "??"); c = '.'; } @@ -616,7 +616,7 @@ void CtrlMemView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) { case ID_MEMVIEW_COPYADDRESS: { char temp[24]; - sprintf(temp, "0x%08X", curAddress_); + snprintf(temp, sizeof(temp), "0x%08X", curAddress_); W32Util::CopyTextToClipboard(wnd, temp); } break; @@ -959,7 +959,7 @@ void CtrlMemView::drawOffsetScale(HDC hdc) { char temp[64]; for (int i = 0; i < 16; i++) { - sprintf(temp, "%02X", i); + snprintf(temp, sizeof(temp), "%02X", i); TextOutA(hdc, currentX, offsetPositionY_, temp, 2); currentX += 3 * charWidth_; // hex and space } diff --git a/Windows/Debugger/CtrlMemView.h b/Windows/Debugger/CtrlMemView.h index 93096c05ae..3c57ecb2aa 100644 --- a/Windows/Debugger/CtrlMemView.h +++ b/Windows/Debugger/CtrlMemView.h @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #pragma once ////////////////////////////////////////////////////////////////////////// diff --git a/Windows/Debugger/CtrlRegisterList.cpp b/Windows/Debugger/CtrlRegisterList.cpp index 6fcf215b76..7f442650ed 100644 --- a/Windows/Debugger/CtrlRegisterList.cpp +++ b/Windows/Debugger/CtrlRegisterList.cpp @@ -1,6 +1,4 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - -#include +#include #include #include "Common/System/Display.h" @@ -253,12 +251,12 @@ void CtrlRegisterList::onPaint(WPARAM wParam, LPARAM lParam) if (iGetNumRegsInCategory(category)) { char temp[256]; - int temp_len = sprintf(temp,"%s",cpu->GetRegName(category,i)); + int temp_len = snprintf(temp, sizeof(temp), "%s", cpu->GetRegName(category, i)); SetTextColor(hdc,0x600000); TextOutA(hdc,17,rowY1,temp,temp_len); SetTextColor(hdc,0x000000); - cpu->PrintRegValue(category,i,temp); + cpu->PrintRegValue(category, i, temp, sizeof(temp)); if (category == 0 && changedCat0Regs[i]) SetTextColor(hdc, 0x0000FF); else @@ -274,15 +272,15 @@ void CtrlRegisterList::onPaint(WPARAM wParam, LPARAM lParam) { case REGISTER_PC: value = cpu->GetPC(); - len = sprintf(temp,"pc"); + len = snprintf(temp, sizeof(temp), "pc"); break; case REGISTER_HI: value = cpu->GetHi(); - len = sprintf(temp,"hi"); + len = snprintf(temp, sizeof(temp), "hi"); break; case REGISTER_LO: value = cpu->GetLo(); - len = sprintf(temp,"lo"); + len = snprintf(temp, sizeof(temp), "lo"); break; default: temp[0] = '\0'; @@ -292,7 +290,7 @@ void CtrlRegisterList::onPaint(WPARAM wParam, LPARAM lParam) SetTextColor(hdc,0x600000); TextOutA(hdc,17,rowY1,temp,len); - len = sprintf(temp,"%08X",value); + len = snprintf(temp, sizeof(temp), "%08X",value); if (changedCat0Regs[i]) SetTextColor(hdc, 0x0000FF); else diff --git a/Windows/Debugger/CtrlRegisterList.h b/Windows/Debugger/CtrlRegisterList.h index 46d9a13f68..34afb67de0 100644 --- a/Windows/Debugger/CtrlRegisterList.h +++ b/Windows/Debugger/CtrlRegisterList.h @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #pragma once ////////////////////////////////////////////////////////////////////////// diff --git a/Windows/Debugger/Debugger_MemoryDlg.cpp b/Windows/Debugger/Debugger_MemoryDlg.cpp index 5382a69b71..b247459bbf 100644 --- a/Windows/Debugger/Debugger_MemoryDlg.cpp +++ b/Windows/Debugger/Debugger_MemoryDlg.cpp @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #include "Windows/stdafx.h" #include #include diff --git a/Windows/Debugger/Debugger_MemoryDlg.h b/Windows/Debugger/Debugger_MemoryDlg.h index 4e47d37be1..d69643a832 100644 --- a/Windows/Debugger/Debugger_MemoryDlg.h +++ b/Windows/Debugger/Debugger_MemoryDlg.h @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #pragma once #include "Windows/W32Util/DialogManager.h" diff --git a/Windows/Debugger/Debugger_VFPUDlg.cpp b/Windows/Debugger/Debugger_VFPUDlg.cpp index 1a6bc43852..a82f0793e7 100644 --- a/Windows/Debugger/Debugger_VFPUDlg.cpp +++ b/Windows/Debugger/Debugger_VFPUDlg.cpp @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #include "Common/CommonWindows.h" #include #include diff --git a/Windows/Debugger/DumpMemoryWindow.cpp b/Windows/Debugger/DumpMemoryWindow.cpp index 884ba3bccf..fc438a5418 100644 --- a/Windows/Debugger/DumpMemoryWindow.cpp +++ b/Windows/Debugger/DumpMemoryWindow.cpp @@ -152,7 +152,7 @@ bool DumpMemoryWindow::fetchDialogData(HWND hwnd) if (cpu->initExpression(str,exp) == false || cpu->parseExpression(exp,start) == false) { - sprintf(errorMessage,"Invalid address expression \"%s\".",str); + snprintf(errorMessage, sizeof(errorMessage), "Invalid address expression \"%s\".",str); MessageBoxA(hwnd,errorMessage,"Error",MB_OK); return false; } @@ -162,7 +162,7 @@ bool DumpMemoryWindow::fetchDialogData(HWND hwnd) if (cpu->initExpression(str,exp) == false || cpu->parseExpression(exp,size) == false) { - sprintf(errorMessage,"Invalid size expression \"%s\".",str); + snprintf(errorMessage, sizeof(errorMessage), "Invalid size expression \"%s\".",str); MessageBoxA(hwnd,errorMessage,"Error",MB_OK); return false; } @@ -198,12 +198,12 @@ bool DumpMemoryWindow::fetchDialogData(HWND hwnd) if (invalidAddress) { - sprintf(errorMessage,"Invalid address 0x%08X.",start); + snprintf(errorMessage, sizeof(errorMessage), "Invalid address 0x%08X.",start); MessageBoxA(hwnd,errorMessage,"Error",MB_OK); return false; } else if (invalidSize) { - sprintf(errorMessage,"Invalid end address 0x%08X.",start+size); + snprintf(errorMessage, sizeof(errorMessage), "Invalid end address 0x%08X.",start+size); MessageBoxA(hwnd,errorMessage,"Error",MB_OK); return false; } @@ -232,8 +232,7 @@ void DumpMemoryWindow::changeMode(HWND hwnd, Mode newMode) u32 start = 0, size = 0; const char *defaultFileName = ""; - switch (selectedMode) - { + switch (selectedMode) { case MODE_RAM: start = PSP_GetUserMemoryBase(); size = PSP_GetUserMemoryEnd()-start; @@ -251,11 +250,11 @@ void DumpMemoryWindow::changeMode(HWND hwnd, Mode newMode) break; } - sprintf(buffer,"0x%08X",start); + snprintf(buffer, sizeof(buffer), "0x%08X", start); SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),buffer); EnableWindow(GetDlgItem(hwnd,IDC_DUMP_STARTADDRESS),FALSE); - sprintf(buffer,"0x%08X",size); + snprintf(buffer, sizeof(buffer), "0x%08X", size); SetWindowTextA(GetDlgItem(hwnd,IDC_DUMP_SIZE),buffer); EnableWindow(GetDlgItem(hwnd,IDC_DUMP_SIZE),FALSE); diff --git a/Windows/GEDebugger/CtrlDisplayListView.cpp b/Windows/GEDebugger/CtrlDisplayListView.cpp index 4c341c7e4b..25cd47ae34 100644 --- a/Windows/GEDebugger/CtrlDisplayListView.cpp +++ b/Windows/GEDebugger/CtrlDisplayListView.cpp @@ -228,7 +228,7 @@ void CtrlDisplayListView::onPaint(WPARAM wParam, LPARAM lParam) GPUDebugOp op = i < (int)disasm.size() ? disasm[i] : GPUDebugOp(); char addressText[64]; - sprintf(addressText,"%08X %08X",op.pc,op.op); + snprintf(addressText,sizeof(addressText),"%08X %08X",op.pc,op.op); TextOutA(hdc,pixelPositions.addressStart,rowY1+2,addressText,(int)strlen(addressText)); if (address == list.pc) @@ -344,7 +344,7 @@ void CtrlDisplayListView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) case ID_DISASM_COPYADDRESS: { char temp[16]; - sprintf(temp,"%08X",curAddress); + snprintf(temp,sizeof(temp),"%08X",curAddress); W32Util::CopyTextToClipboard(wnd, temp); } break; diff --git a/Windows/MainWindow.cpp b/Windows/MainWindow.cpp index ff2edf3230..7988a76f60 100644 --- a/Windows/MainWindow.cpp +++ b/Windows/MainWindow.cpp @@ -18,12 +18,12 @@ // TODO: Get rid of the internal window. // Tried before but Intel drivers screw up when minimizing, or something ? -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. -// It's improving slowly, though. :) #include "stdafx.h" + +#include "ppsspp_config.h" + #include "Common/CommonWindows.h" #include "Common/OSVersion.h" -#include "ppsspp_config.h" #include #include diff --git a/Windows/W32Util/ShellUtil.cpp b/Windows/W32Util/ShellUtil.cpp index 08e89b9c0b..df770e64d3 100644 --- a/Windows/W32Util/ShellUtil.cpp +++ b/Windows/W32Util/ShellUtil.cpp @@ -1,5 +1,3 @@ -// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003. - #pragma warning(disable:4091) // workaround bug in VS2015 headers #include "Windows/stdafx.h" @@ -59,9 +57,6 @@ namespace W32Util return result; } - //--------------------------------------------------------------------------------------------------- - // function WinBrowseForFileName - //--------------------------------------------------------------------------------------------------- bool BrowseForFileName(bool _bLoad, HWND _hParent, const wchar_t *_pTitle, const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension, std::string &_strFileName) { diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 1a88de933b..e5e557557b 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -384,7 +384,7 @@ void System_ShowKeyboard() { void System_Vibrate(int length_ms) { char temp[32]; - sprintf(temp, "%i", length_ms); + snprintf(temp, sizeof(temp), "%", length_ms); PushCommand("vibrate", temp); }