Chore: don't use strncpy in favor of strlcpy

strncpy doesn't require target to be NUL terminated
This commit is contained in:
李通洲
2024-10-17 16:16:04 +08:00
parent 8f79d6ab1d
commit 993902916d
5 changed files with 13 additions and 13 deletions
+8 -8
View File
@@ -10,7 +10,7 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
// macOS provides the global system cache line size
uint32_t lineSize = (uint32_t) ffSysctlGetInt("hw.cachelinesize", 0);
char sysctlKey[256] = "hw.perflevelN.";
char sysctlKey[128] = "hw.perflevelN.";
char* pNum = sysctlKey + strlen("hw.perflevel");
char* pSubkey = sysctlKey + strlen("hw.perflevelN.");
const size_t lenLeft = sizeof(sysctlKey) - strlen("hw.perflevelN.");
@@ -19,35 +19,35 @@ const char* ffDetectCPUCache(FFCPUCacheResult* result)
{
*pNum = (char) ('0' + i);
strncpy(pSubkey, "physicalcpu", lenLeft);
strlcpy(pSubkey, "physicalcpu", lenLeft);
uint32_t ncpu = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
if (ncpu <= 0) continue;
strncpy(pSubkey, "l1icachesize", lenLeft);
strlcpy(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;
strncpy(pSubkey, "l1dcachesize", lenLeft);
strlcpy(pSubkey, "l1dcachesize", lenLeft);
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
if (size)
ffCPUCacheAddItem(result, 1, size, lineSize, FF_CPU_CACHE_TYPE_DATA)->num = ncpu;
strncpy(pSubkey, "l2cachesize", lenLeft);
strlcpy(pSubkey, "l2cachesize", lenLeft);
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
if (size)
{
strncpy(pSubkey, "cpusperl2", lenLeft);
strlcpy(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;
}
strncpy(pSubkey, "l3cachesize", lenLeft);
strlcpy(pSubkey, "l3cachesize", lenLeft);
size = (uint32_t) ffSysctlGetInt(sysctlKey, 0);
if (size)
{
strncpy(pSubkey, "cpusperl3", lenLeft);
strlcpy(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;
@@ -112,6 +112,7 @@ static void waylandKdeNameListener(void* data, FF_MAYBE_UNUSED struct kde_output
{
WaylandDisplay* display = data;
display->type = ffdsGetDisplayType(name);
// As display->id is used as an internal identifier, we don't need it to be NUL terminated
strncpy((char*) &display->id, name, sizeof(display->id));
ffStrbufAppendS(&display->name, name);
}
+1 -2
View File
@@ -460,8 +460,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
{
if (ffStrStartsWith(entry->d_name, "card"))
{
strncpy(drmKeyBuffer, entry->d_name, sizeof(drmKeyBuffer) - 1);
drmKeyBuffer[sizeof(drmKeyBuffer) - 1] = '\0';
strlcpy(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;
strncpy(ifr.ifr_name, iface->name.chars, IFNAMSIZ - 1);
strlcpy(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 = {};
strncpy(ifmr.ifm_name, iface->name.chars, IFNAMSIZ - 1);
strlcpy(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))
+1 -1
View File
@@ -190,7 +190,7 @@ static const char* detectWifiWithIoctls(FFWifiResult* item)
return "socket() failed";
struct iwreq iwr;
strncpy(iwr.ifr_name, item->inf.description.chars, IFNAMSIZ);
strlcpy(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;