From 21fe73c8e7843dc3b84d14629ab088851cb66b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 26 May 2017 19:04:08 +0200 Subject: [PATCH] Switch strncpy to truncate_cpy in a bunch of places. --- Common/ArmCPUDetect.cpp | 6 +++--- Common/MemArenaAndroid.cpp | 3 ++- Core/Debugger/SymbolMap.cpp | 9 ++++----- Core/Dialog/PSPDialog.cpp | 3 ++- Core/Dialog/PSPMsgDialog.cpp | 3 ++- Core/Dialog/SavedataParam.cpp | 4 ++-- Core/HLE/sceIo.cpp | 4 ++-- Windows/Debugger/CtrlDisAsmView.cpp | 3 ++- ext/native/gfx_es2/gpu_features.cpp | 6 +++--- 9 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Common/ArmCPUDetect.cpp b/Common/ArmCPUDetect.cpp index cb4992b466..8aa3fd3a50 100644 --- a/Common/ArmCPUDetect.cpp +++ b/Common/ArmCPUDetect.cpp @@ -219,7 +219,7 @@ void CPUInfo::Detect() strcpy(brand_string, "Unknown"); num_cores = 1; #endif - strncpy(cpu_string, brand_string, sizeof(cpu_string)); + truncate_cpy(cpu_string, brand_string); // Hardcode this for now bSwp = true; bHalf = true; @@ -237,8 +237,8 @@ void CPUInfo::Detect() bFP = false; bASIMD = false; #else // PPSSPP_PLATFORM(LINUX) - strncpy(cpu_string, GetCPUString().c_str(), sizeof(cpu_string)); - strncpy(brand_string, GetCPUBrandString().c_str(), sizeof(brand_string)); + truncate_cpy(cpu_string, GetCPUString().c_str()); + truncate_cpy(brand_string, GetCPUBrandString().c_str()); bSwp = CheckCPUFeature("swp"); bHalf = CheckCPUFeature("half"); diff --git a/Common/MemArenaAndroid.cpp b/Common/MemArenaAndroid.cpp index a320d1d243..0ff75317cf 100644 --- a/Common/MemArenaAndroid.cpp +++ b/Common/MemArenaAndroid.cpp @@ -21,6 +21,7 @@ #include "MemoryUtil.h" #include "MemArena.h" +#include "StringUtils.h" #include #include @@ -49,7 +50,7 @@ static int ashmem_create_region(const char *name, size_t size) { int ret; if (name) { char buf[ASHMEM_NAME_LEN]; - strncpy(buf, name, sizeof(buf)); + truncate_cpy(buf, name); ret = ioctl(fd, ASHMEM_SET_NAME, buf); if (ret < 0) goto error; diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index 1cadac5323..b36e035340 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -34,6 +34,7 @@ #include "zlib.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" +#include "Common/StringUtils.h" #include "Core/MemMap.h" #include "Core/Debugger/SymbolMap.h" @@ -427,8 +428,7 @@ void SymbolMap::AddModule(const char *name, u32 address, u32 size) { } ModuleEntry mod; - strncpy(mod.name, name, ARRAY_SIZE(mod.name)); - mod.name[ARRAY_SIZE(mod.name) - 1] = '\0'; + truncate_cpy(mod.name, name); mod.start = address; mod.size = size; mod.index = (int)modules.size() + 1; @@ -772,8 +772,7 @@ void SymbolMap::AddLabel(const char* name, u32 address, int moduleIndex) { LabelEntry label; label.addr = relAddress; label.module = moduleIndex; - strncpy(label.name, name, 128); - label.name[127] = 0; + truncate_cpy(label.name, name); labels[symbolKey] = label; if (IsModuleActive(moduleIndex)) { @@ -791,7 +790,7 @@ void SymbolMap::SetLabelName(const char* name, u32 address) { auto symbolKey = std::make_pair(labelInfo->second.module, labelInfo->second.addr); auto label = labels.find(symbolKey); if (label != labels.end()) { - strncpy(label->second.name, name, 128); + truncate_cpy(label->second.name, name); label->second.name[127] = 0; // Refresh the active item if it exists. diff --git a/Core/Dialog/PSPDialog.cpp b/Core/Dialog/PSPDialog.cpp index 54c467dbd5..b6f00f86a3 100644 --- a/Core/Dialog/PSPDialog.cpp +++ b/Core/Dialog/PSPDialog.cpp @@ -18,6 +18,7 @@ #include "i18n/i18n.h" #include "Common/ChunkFile.h" +#include "Common/StringUtils.h" #include "Core/CoreTiming.h" #include "Core/HLE/sceCtrl.h" #include "Core/Util/PPGeDraw.h" @@ -198,7 +199,7 @@ void PSPDialog::DisplayButtons(int flags, const char *caption) char safeCaption[65] = {0}; if (caption != NULL && *caption != '\0') { useCaption = true; - strncpy(safeCaption, caption, 64); + truncate_cpy(safeCaption, caption); } I18NCategory *di = GetI18NCategory("Dialog"); diff --git a/Core/Dialog/PSPMsgDialog.cpp b/Core/Dialog/PSPMsgDialog.cpp index 3b5456c155..b50dee2a9f 100755 --- a/Core/Dialog/PSPMsgDialog.cpp +++ b/Core/Dialog/PSPMsgDialog.cpp @@ -21,6 +21,7 @@ #include "Core/MemMapHelpers.h" #include "Core/Reporting.h" #include "Common/ChunkFile.h" +#include "Common/StringUtils.h" #include "i18n/i18n.h" #include "util/text/utf8.h" @@ -130,7 +131,7 @@ int PSPMsgDialog::Init(unsigned int paramAddr) { if (flag & DS_ERRORMSG) { snprintf(msgText, 512, "Error code: %08x", messageDialog.errorNum); } else { - strncpy(msgText, messageDialog.string, 512); + truncate_cpy(msgText, messageDialog.string); } ChangeStatusInit(MSG_INIT_DELAY_US); diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 3e762bd183..235029f4cc 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -18,6 +18,7 @@ #include "i18n/i18n.h" #include "base/logging.h" #include "Common/ChunkFile.h" +#include "Common/StringUtils.h" #include "Core/Config.h" #include "Core/Host.h" #include "Core/Reporting.h" @@ -55,8 +56,7 @@ namespace void SetStringFromSFO(ParamSFOData &sfoFile, const char *name, char *str, int strLength) { std::string value = sfoFile.GetValueString(name); - strncpy(str, value.c_str(), strLength - 1); - str[strLength - 1] = 0; + truncate_cpy(str, value.c_str()); } bool ReadPSPFile(std::string filename, u8 **data, s64 dataSize, s64 *readSize) diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index 26f83d1ac7..396ff46602 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -22,6 +22,7 @@ #include "thread/threadutil.h" #include "profiler/profiler.h" +#include "Common/StringUtils.h" #include "Core/Core.h" #include "Core/Config.h" #include "Core/Debugger/Breakpoints.h" @@ -2184,8 +2185,7 @@ static u32 sceIoDread(int id, u32 dirent_addr) { PSPFileInfo &info = dir->listing[dir->index]; __IoGetStat(&entry->d_stat, info); - strncpy(entry->d_name, info.name.c_str(), 256); - entry->d_name[255] = '\0'; + truncate_cpy(entry->d_name, info.name.c_str()); bool isFAT = false; IFileSystem *sys = pspFileSystem.GetSystemFromFilename(dir->name); diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index b0afc5f9a5..06248c4768 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -10,6 +10,7 @@ #include "Core/MIPS/MIPSAsm.h" #include "Core/MIPS/MIPSAnalyst.h" #include "Core/Config.h" +#include "Common/StringUtils.h" #include "Windows/Debugger/CtrlDisAsmView.h" #include "Windows/Debugger/Debugger_MemoryDlg.h" #include "Windows/Debugger/DebuggerShared.h" @@ -970,7 +971,7 @@ void CtrlDisAsmView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) { char name[256]; std::string newname; - strncpy_s(name, g_symbolMap->GetLabelString(funcBegin).c_str(),_TRUNCATE); + truncate_cpy(name, g_symbolMap->GetLabelString(funcBegin).c_str()); if (InputBox_GetString(MainWindow::GetHInstance(), MainWindow::GetHWND(), L"New function name", name, newname)) { g_symbolMap->SetLabelName(newname.c_str(),funcBegin); u32 funcSize = g_symbolMap->GetFunctionSize(curAddress); diff --git a/ext/native/gfx_es2/gpu_features.cpp b/ext/native/gfx_es2/gpu_features.cpp index d2bdf49179..1f90acbb24 100644 --- a/ext/native/gfx_es2/gpu_features.cpp +++ b/ext/native/gfx_es2/gpu_features.cpp @@ -143,9 +143,9 @@ void CheckGLExtensions() { // Start by assuming we're at 2.0. int parsed[2] = {2, 0}; { // Grab the version and attempt to parse. - char buffer[64] = { 0 }; + char buffer[128] = { 0 }; if (versionStr) { - strncpy(buffer, versionStr, 63); + strncpy(buffer, versionStr, sizeof(buffer) - 1); } int len = (int)strlen(buffer); @@ -159,7 +159,7 @@ void CheckGLExtensions() { break; } } - if (beforeDot && buffer[i] == '.' && lastDigit) { + if (beforeDot && buffer[i] == '.' && lastDigit) { parsed[0] = lastDigit; beforeDot = false; }