From 993902916db978cacf05b7a76f8975589a54ca82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Oct 2024 16:16:04 +0800 Subject: [PATCH] Chore: don't use strncpy in favor of strlcpy strncpy doesn't require target to be NUL terminated --- src/detection/cpucache/cpucache_apple.c | 16 ++++++++-------- .../displayserver/linux/wayland/kde-output.c | 1 + src/detection/gpu/gpu_linux.c | 3 +-- src/detection/localip/localip_linux.c | 4 ++-- src/detection/wifi/wifi_linux.c | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/detection/cpucache/cpucache_apple.c b/src/detection/cpucache/cpucache_apple.c index 261e46551..d4d305642 100644 --- a/src/detection/cpucache/cpucache_apple.c +++ b/src/detection/cpucache/cpucache_apple.c @@ -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; diff --git a/src/detection/displayserver/linux/wayland/kde-output.c b/src/detection/displayserver/linux/wayland/kde-output.c index ab8a9eb93..1c4576dd4 100644 --- a/src/detection/displayserver/linux/wayland/kde-output.c +++ b/src/detection/displayserver/linux/wayland/kde-output.c @@ -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); } diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 6fe350d56..0eb206d89 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -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; } diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 243091ddf..1c1475564 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; - 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)) diff --git a/src/detection/wifi/wifi_linux.c b/src/detection/wifi/wifi_linux.c index 2e6b90f20..1c73c6611 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; - 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;