diff --git a/src/detection/cpucache/cpucache_apple.c b/src/detection/cpucache/cpucache_apple.c index d4d305642..b0e0c4ff8 100644 --- a/src/detection/cpucache/cpucache_apple.c +++ b/src/detection/cpucache/cpucache_apple.c @@ -1,5 +1,6 @@ #include "cpucache.h" #include "common/sysctl.h" +#include "util/stringUtils.h" const char* ffDetectCPUCache(FFCPUCacheResult* result) { @@ -19,35 +20,35 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result) { *pNum = (char) ('0' + i); - strlcpy(pSubkey, "physicalcpu", lenLeft); + ffStrCopyN(pSubkey, "physicalcpu", lenLeft); uint32_t ncpu = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (ncpu <= 0) continue; - strlcpy(pSubkey, "l1icachesize", lenLeft); + ffStrCopyN(pSubkey, "l1icachesize", lenLeft); uint32_t size = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (size) ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_INSTRUCTION)->num = ncpu; - strlcpy(pSubkey, "l1dcachesize", lenLeft); + ffStrCopyN(pSubkey, "l1dcachesize", lenLeft); size = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (size) ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_DATA)->num = ncpu; - strlcpy(pSubkey, "l2cachesize", lenLeft); + ffStrCopyN(pSubkey, "l2cachesize", lenLeft); size = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (size) { - strlcpy(pSubkey, "cpusperl2", lenLeft); + ffStrCopyN(pSubkey, "cpusperl2", lenLeft); uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (cpuSper) ffCPUCacheAddItem(result, 2, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper; } - strlcpy(pSubkey, "l3cachesize", lenLeft); + ffStrCopyN(pSubkey, "l3cachesize", lenLeft); size = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (size) { - strlcpy(pSubkey, "cpusperl3", lenLeft); + ffStrCopyN(pSubkey, "cpusperl3", lenLeft); uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0); if (cpuSper) ffCPUCacheAddItem(result, 3, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper; diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 0eb206d89..096395896 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -460,7 +460,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf { if (ffStrStartsWith(entry->d_name, "card")) { - strlcpy(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer)); + ffStrCopyN(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer)); drmKey = drmKeyBuffer; break; } diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 1c1475564..fa512fd13 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -231,7 +231,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) FF_LIST_FOR_EACH(FFLocalIpResult, iface, *results) { struct ifreq ifr; - strlcpy(ifr.ifr_name, iface->name.chars, IFNAMSIZ); + ffStrCopyN(ifr.ifr_name, iface->name.chars, IFNAMSIZ); if (options->showType & FF_LOCALIP_TYPE_MTU_BIT) { @@ -248,7 +248,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) iface->speed = (edata.speed_hi << 16) | edata.speed; // ethtool_cmd_speed is not available on Android #elif __FreeBSD__ || __APPLE__ || __OpenBSD__ struct ifmediareq ifmr = {}; - strlcpy(ifmr.ifm_name, iface->name.chars, IFNAMSIZ); + ffStrCopyN(ifmr.ifm_name, iface->name.chars, IFNAMSIZ); if (ioctl(sockfd, SIOCGIFMEDIA, &ifmr) == 0 && (IFM_TYPE(ifmr.ifm_active) & IFM_ETHER)) { switch (IFM_SUBTYPE(ifmr.ifm_active)) diff --git a/src/detection/packages/packages_windows.c b/src/detection/packages/packages_windows.c index 3d613f89c..103f7a6a1 100644 --- a/src/detection/packages/packages_windows.c +++ b/src/detection/packages/packages_windows.c @@ -58,8 +58,8 @@ static void detectChoco(FF_MAYBE_UNUSED FFPackagesResult* result) return; char chocoPath[MAX_PATH + 3]; - strcpy(chocoPath, chocoInstall); - strncat(chocoPath, "/lib/*", sizeof(chocoPath) - 1 - strlen(chocoPath)); + char* pend = ffStrCopyN(chocoPath, chocoInstall, sizeof(chocoPath)); + ffStrCopyN(pend, "/lib/*", sizeof(chocoPath) - (pend - chocoPath)); result->choco = getNumElements(chocoPath, FILE_ATTRIBUTE_DIRECTORY, "choco"); } diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index cf82da291..46b529ec5 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -4,6 +4,7 @@ #include "common/properties.h" #include "detection/terminalshell/terminalshell.h" #include "util/windows/unicode.h" +#include "util/stringUtils.h" #include "terminalfont.h" #include @@ -97,13 +98,12 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon if(terminalExe && terminalExe->length > 0 && !ffStrbufEqualS(terminalExe, "Windows Terminal")) { char jsonPath[MAX_PATH + 1]; - strncpy(jsonPath, terminalExe->chars, ffStrbufLastIndexC(terminalExe, '\\') + 1); - char* pathEnd = jsonPath + strlen(jsonPath); - strncpy(pathEnd, ".portable", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); + char* pathEnd = ffStrCopyN(jsonPath, terminalExe->chars, ffStrbufLastIndexC(terminalExe, '\\') + 1); + ffStrCopyN(pathEnd, ".portable", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); if(ffPathExists(jsonPath, FF_PATHTYPE_ANY)) { - strncpy(pathEnd, "settings\\settings.json", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); + ffStrCopyN(pathEnd, "settings\\settings.json", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1); if(!ffAppendFileBuffer(jsonPath, &json)) error = "Error reading Windows Terminal portable settings JSON file"; } diff --git a/src/detection/wifi/wifi_linux.c b/src/detection/wifi/wifi_linux.c index 1c73c6611..732c5afc8 100644 --- a/src/detection/wifi/wifi_linux.c +++ b/src/detection/wifi/wifi_linux.c @@ -190,7 +190,7 @@ static const char* detectWifiWithIoctls(FFWifiResult* item) return "socket() failed"; struct iwreq iwr; - strlcpy(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ); + ffStrCopyN(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ); ffStrbufEnsureFree(&item->conn.ssid, IW_ESSID_MAX_SIZE); iwr.u.essid.pointer = (caddr_t) item->conn.ssid.chars; iwr.u.essid.length = IW_ESSID_MAX_SIZE + 1; diff --git a/src/util/stringUtils.h b/src/util/stringUtils.h index a7dea3fbe..6c641517b 100644 --- a/src/util/stringUtils.h +++ b/src/util/stringUtils.h @@ -79,3 +79,14 @@ static inline bool ffCharIsDigit(char c) { return '0' <= c && c <= '9'; } + +static inline char* ffStrCopyN(char* __restrict__ dst, const char* __restrict__ src, size_t nDst) +{ + assert(dst != NULL); + if (__builtin_expect(dst == NULL, false)) return dst; + + size_t len = strnlen(src, nDst - 1); + memcpy(dst, src, len); + dst[len] = '\0'; + return dst + len; +}