Skip to content

Commit 9939029

Browse files
committed
Chore: don't use strncpy in favor of strlcpy
strncpy doesn't require target to be NUL terminated
1 parent 8f79d6a commit 9939029

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

src/detection/cpucache/cpucache_apple.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
1010
// macOS provides the global system cache line size
1111
uint32_t lineSize = (uint32_t) ffSysctlGetInt("hw.cachelinesize", 0);
1212

13-
char sysctlKey[256] = "hw.perflevelN.";
13+
char sysctlKey[128] = "hw.perflevelN.";
1414
char* pNum = sysctlKey + strlen("hw.perflevel");
1515
char* pSubkey = sysctlKey + strlen("hw.perflevelN.");
1616
const size_t lenLeft = sizeof(sysctlKey) - strlen("hw.perflevelN.");
@@ -19,35 +19,35 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
1919
{
2020
*pNum = (char) ('0' + i);
2121

22-
strncpy(pSubkey, "physicalcpu", lenLeft);
22+
strlcpy(pSubkey, "physicalcpu", lenLeft);
2323
uint32_t ncpu = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
2424
if (ncpu <= 0) continue;
2525

26-
strncpy(pSubkey, "l1icachesize", lenLeft);
26+
strlcpy(pSubkey, "l1icachesize", lenLeft);
2727
uint32_t size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
2828
if (size)
2929
ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_INSTRUCTION)->num = ncpu;
3030

31-
strncpy(pSubkey, "l1dcachesize", lenLeft);
31+
strlcpy(pSubkey, "l1dcachesize", lenLeft);
3232
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
3333
if (size)
3434
ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_DATA)->num = ncpu;
3535

36-
strncpy(pSubkey, "l2cachesize", lenLeft);
36+
strlcpy(pSubkey, "l2cachesize", lenLeft);
3737
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
3838
if (size)
3939
{
40-
strncpy(pSubkey, "cpusperl2", lenLeft);
40+
strlcpy(pSubkey, "cpusperl2", lenLeft);
4141
uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
4242
if (cpuSper)
4343
ffCPUCacheAddItem(result, 2, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper;
4444
}
4545

46-
strncpy(pSubkey, "l3cachesize", lenLeft);
46+
strlcpy(pSubkey, "l3cachesize", lenLeft);
4747
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
4848
if (size)
4949
{
50-
strncpy(pSubkey, "cpusperl3", lenLeft);
50+
strlcpy(pSubkey, "cpusperl3", lenLeft);
5151
uint32_t cpuSper = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
5252
if (cpuSper)
5353
ffCPUCacheAddItem(result, 3, size, lineSize, FF_CPU_CACHE_TYPE_UNIFIED)->num = ncpu / cpuSper;

src/detection/displayserver/linux/wayland/kde-output.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static void waylandKdeNameListener(void* data, FF_MAYBE_UNUSED struct kde_output
112112
{
113113
WaylandDisplay* display = data;
114114
display->type = ffdsGetDisplayType(name);
115+
// As display->id is used as an internal identifier, we don't need it to be NUL terminated
115116
strncpy((char*) &display->id, name, sizeof(display->id));
116117
ffStrbufAppendS(&display->name, name);
117118
}

src/detection/gpu/gpu_linux.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
460460
{
461461
if (ffStrStartsWith(entry->d_name, "card"))
462462
{
463-
strncpy(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer) - 1);
464-
drmKeyBuffer[sizeof(drmKeyBuffer) - 1] = '\0';
463+
strlcpy(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer));
465464
drmKey = drmKeyBuffer;
466465
break;
467466
}

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-
strncpy(ifr.ifr_name, iface->name.chars, IFNAMSIZ - 1);
234+
strlcpy(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-
strncpy(ifmr.ifm_name, iface->name.chars, IFNAMSIZ - 1);
251+
strlcpy(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/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-
strncpy(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ);
193+
strlcpy(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;

0 commit comments

Comments
 (0)