Skip to content

Commit 48bb5b8

Browse files
committed
Chore: use ffStrCopyN instead of strlcpy
1 parent 9bd692d commit 48bb5b8

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

src/detection/cpucache/cpucache_apple.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cpucache.h"
22
#include "common/sysctl.h"
3+
#include "util/stringUtils.h"
34

45
const char* ffDetectCPUCache(FFCPUCacheResult* result)
56
{
@@ -19,35 +20,35 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
1920
{
2021
*pNum = (char) ('0' + i);
2122

22-
strlcpy(pSubkey, "physicalcpu", lenLeft);
23+
ffStrCopyN(pSubkey, "physicalcpu", lenLeft);
2324
uint32_t ncpu = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
2425
if (ncpu <= 0) continue;
2526

26-
strlcpy(pSubkey, "l1icachesize", lenLeft);
27+
ffStrCopyN(pSubkey, "l1icachesize", lenLeft);
2728
uint32_t size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
2829
if (size)
2930
ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_INSTRUCTION)->num = ncpu;
3031

31-
strlcpy(pSubkey, "l1dcachesize", lenLeft);
32+
ffStrCopyN(pSubkey, "l1dcachesize", lenLeft);
3233
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
3334
if (size)
3435
ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_DATA)->num = ncpu;
3536

36-
strlcpy(pSubkey, "l2cachesize", lenLeft);
37+
ffStrCopyN(pSubkey, "l2cachesize", lenLeft);
3738
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
3839
if (size)
3940
{
40-
strlcpy(pSubkey, "cpusperl2", lenLeft);
41+
ffStrCopyN(pSubkey, "cpusperl2", lenLeft);
4142
uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
4243
if (cpuSper)
4344
ffCPUCacheAddItem(result, 2, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper;
4445
}
4546

46-
strlcpy(pSubkey, "l3cachesize", lenLeft);
47+
ffStrCopyN(pSubkey, "l3cachesize", lenLeft);
4748
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
4849
if (size)
4950
{
50-
strlcpy(pSubkey, "cpusperl3", lenLeft);
51+
ffStrCopyN(pSubkey, "cpusperl3", lenLeft);
5152
uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
5253
if (cpuSper)
5354
ffCPUCacheAddItem(result, 3, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper;

src/detection/gpu/gpu_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
460460
{
461461
if (ffStrStartsWith(entry->d_name, "card"))
462462
{
463-
strlcpy(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer));
463+
ffStrCopyN(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer));
464464
drmKey = drmKeyBuffer;
465465
break;
466466
}

src/detection/localip/localip_linux.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
231231
FF_LIST_FOR_EACH(FFLocalIpResult, iface, *results)
232232
{
233233
struct ifreq ifr;
234-
strlcpy(ifr.ifr_name, iface->name.chars, IFNAMSIZ);
234+
ffStrCopyN(ifr.ifr_name, iface->name.chars, IFNAMSIZ);
235235

236236
if (options->showType & FF_LOCALIP_TYPE_MTU_BIT)
237237
{
@@ -248,7 +248,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
248248
iface->speed = (edata.speed_hi << 16) | edata.speed; // ethtool_cmd_speed is not available on Android
249249
#elif __FreeBSD__ || __APPLE__ || __OpenBSD__
250250
struct ifmediareq ifmr = {};
251-
strlcpy(ifmr.ifm_name, iface->name.chars, IFNAMSIZ);
251+
ffStrCopyN(ifmr.ifm_name, iface->name.chars, IFNAMSIZ);
252252
if (ioctl(sockfd, SIOCGIFMEDIA, &ifmr) == 0 && (IFM_TYPE(ifmr.ifm_active) & IFM_ETHER))
253253
{
254254
switch (IFM_SUBTYPE(ifmr.ifm_active))

src/detection/packages/packages_windows.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ static void detectChoco(FF_MAYBE_UNUSED FFPackagesResult* result)
5858
return;
5959

6060
char chocoPath[MAX_PATH + 3];
61-
strcpy(chocoPath, chocoInstall);
62-
strncat(chocoPath, "/lib/*", sizeof(chocoPath) - 1 - strlen(chocoPath));
61+
char* pend = ffStrCopyN(chocoPath, chocoInstall, sizeof(chocoPath));
62+
ffStrCopyN(pend, "/lib/*", sizeof(chocoPath) - (pend - chocoPath));
6363
result->choco = getNumElements(chocoPath, FILE_ATTRIBUTE_DIRECTORY, "choco");
6464
}
6565

src/detection/terminalfont/terminalfont_windows.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "common/properties.h"
55
#include "detection/terminalshell/terminalshell.h"
66
#include "util/windows/unicode.h"
7+
#include "util/stringUtils.h"
78
#include "terminalfont.h"
89

910
#include <shlobj.h>
@@ -97,13 +98,12 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon
9798
if(terminalExe && terminalExe->length > 0 && !ffStrbufEqualS(terminalExe, "Windows Terminal"))
9899
{
99100
char jsonPath[MAX_PATH + 1];
100-
strncpy(jsonPath, terminalExe->chars, ffStrbufLastIndexC(terminalExe, '\\') + 1);
101-
char* pathEnd = jsonPath + strlen(jsonPath);
102-
strncpy(pathEnd, ".portable", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1);
101+
char* pathEnd = ffStrCopyN(jsonPath, terminalExe->chars, ffStrbufLastIndexC(terminalExe, '\\') + 1);
102+
ffStrCopyN(pathEnd, ".portable", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1);
103103

104104
if(ffPathExists(jsonPath, FF_PATHTYPE_ANY))
105105
{
106-
strncpy(pathEnd, "settings\\settings.json", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1);
106+
ffStrCopyN(pathEnd, "settings\\settings.json", sizeof(jsonPath) - (size_t) (pathEnd - jsonPath) - 1);
107107
if(!ffAppendFileBuffer(jsonPath, &json))
108108
error = "Error reading Windows Terminal portable settings JSON file";
109109
}

src/detection/wifi/wifi_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static const char* detectWifiWithIoctls(FFWifiResult* item)
190190
return "socket() failed";
191191

192192
struct iwreq iwr;
193-
strlcpy(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ);
193+
ffStrCopyN(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ);
194194
ffStrbufEnsureFree(&item->conn.ssid, IW_ESSID_MAX_SIZE);
195195
iwr.u.essid.pointer = (caddr_t) item->conn.ssid.chars;
196196
iwr.u.essid.length = IW_ESSID_MAX_SIZE + 1;

src/util/stringUtils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,14 @@ static inline bool ffCharIsDigit(char c)
7979
{
8080
return '0' <= c && c <= '9';
8181
}
82+
83+
static inline char* ffStrCopyN(char* __restrict__ dst, const char* __restrict__ src, size_t nDst)
84+
{
85+
assert(dst != NULL);
86+
if (__builtin_expect(dst == NULL, false)) return dst;
87+
88+
size_t len = strnlen(src, nDst - 1);
89+
memcpy(dst, src, len);
90+
dst[len] = '\0';
91+
return dst + len;
92+
}

0 commit comments

Comments
 (0)