Chore: use ffStrCopyN instead of strlcpy

This commit is contained in:
李通洲
2024-10-17 22:29:38 +08:00
parent 9bd692dffb
commit 48bb5b8746
7 changed files with 29 additions and 17 deletions
+8 -7
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+2 -2
View File
@@ -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))
+2 -2
View File
@@ -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");
}
@@ -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 <shlobj.h>
@@ -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";
}
+1 -1
View File
@@ -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;
+11
View File
@@ -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;
}