Skip to contentSkip to conversation

tools/thermal/tmon: Fix string truncation warning in optarg copy

PATCH Applied Florian Fainelli 3 participants 3 messages Sep 29 – Oct 14 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/thermal/tmon: Fix string truncation warning in optarg copy

GCC warns about possible string truncation with -Wstringop-truncation:

tmon.c: In function ‘main’:
tmon.c:233:25: warning: ‘__builtin_strncpy’ specified bound 20 equals
destination size [-Wstringop-truncation]

    233 |                         strncpy(ctrl_cdev, optarg,
	CDEV_NAME_SIZE);
        |

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replace strncpy() with snprintf() to guarantee NUL-termination and silence the compiler warning.

Fixes 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem")
---
 tools/thermal/tmon/tmon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/thermal/tmon/tmon.c +1 -1
index 7eb3216a27f4..0c8deee4652e 100644
@@ -230,7 +230,7 @@ int main(int argc, char **argv)
 		switch (c) {
 		case 'c':
 			no_control = 0;
-			strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
+			snprintf(ctrl_cdev, sizeof(ctrl_cdev), "%s", optarg);
 			break;
 		case 'd':
 			start_daemon_mode();
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Florian Fainelli

GCC warns about possible string truncation with -Wstringop-truncation:

tmon.c: In function ‘main’:
tmon.c:233:25: warning: ‘__builtin_strncpy’ specified bound 20 equals
destination size [-Wstringop-truncation]

     233 |                         strncpy(ctrl_cdev, optarg,
	CDEV_NAME_SIZE);
         |
Quoted 8 lines

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replace strncpy() with snprintf() to guarantee NUL-termination and silence the compiler warning.

Fixes 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem")

---

Applied, thanks

#2 lore raw
In reply to Florian Fainelli
The following commit has been merged into the thermal/linux-next branch of thermal:

The following commit has been merged into the thermal/linux-next branch of thermal:

Commit-ID:     602da762f7a7c494a95cb204ac005965b214e556
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//602da762f7a7c494a95cb204ac005965b214e556
Author:        Florian Fainelli <[email protected]>
AuthorDate:    Tue, 29 Sep 2026 13:19:51 -07:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Wed, 14 Oct 2026 09:45:40 +02:00

tools/thermal/tmon: Fix string truncation warning in optarg copy

GCC warns about possible string truncation with -Wstringop-truncation:

tmon.c: In function ‘main’:
tmon.c:233:25: warning: ‘__builtin_strncpy’ specified bound 20 equals
destination size [-Wstringop-truncation]

    233 |                         strncpy(ctrl_cdev, optarg,
	CDEV_NAME_SIZE);
        |

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replace strncpy() with snprintf() to guarantee NUL-termination and silence the compiler warning.

Fixes 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem")
---
 tools/thermal/tmon/tmon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/thermal/tmon/tmon.c +1 -1
index ef67cd1..70a2ec5 100644
@@ -230,7 +230,7 @@ int main(int argc, char **argv)
 		switch (c) {
 		case 'c':
 			no_control = 0;
-			strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
+			snprintf(ctrl_cdev, sizeof(ctrl_cdev), "%s", optarg);
 			break;
 		case 'd':
 			start_daemon_mode();