diff mbox series

[1/2] tools/thermal: tmon: fix build warning caused by misusing strncpy()

Message ID 20221031132110.65847-1-wangxiongfeng2@huawei.com (mailing list archive)
State New, archived
Delegated to: Daniel Lezcano
Headers show
Series [1/2] tools/thermal: tmon: fix build warning caused by misusing strncpy() | expand

Commit Message

Xiongfeng Wang Oct. 31, 2022, 1:21 p.m. UTC
Building tool tmon emits the following warning.

tmon.c: In function ‘main’:
tmon.c:233:4: warning: ‘strncpy’ specified bound 20 equals destination size [-Wstringop-truncation]
  233 |    strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the third parameter 'count' of strncpy() equals destination size, we
can not guarantee the dest string is NULL terminated. The dest string
'ctrl_cdev' will be used as the input of strstr(). Let's minus the copy
count by one and force the dest string NULL terminated.

Fixes: 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem")
Signed-off-by: Xiongfeng Wang <wangxiongfeng2@huawei.com>
---
 tools/thermal/tmon/tmon.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/tools/thermal/tmon/tmon.c b/tools/thermal/tmon/tmon.c
index 7eb3216a27f4..537a7a47f401 100644
--- a/tools/thermal/tmon/tmon.c
+++ b/tools/thermal/tmon/tmon.c
@@ -230,7 +230,8 @@  int main(int argc, char **argv)
 		switch (c) {
 		case 'c':
 			no_control = 0;
-			strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
+			strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE - 1);
+			ctrl_cdev[CDEV_NAME_SIZE - 1] = '\0';
 			break;
 		case 'd':
 			start_daemon_mode();