diff mbox series

[libgpiod,3/4] lib: chip-info strings termination

Message ID 275f51e932498959d57ebc5bf16adc90bb7b5967.1721039339.git.ikerpedrosam@gmail.com
State New
Headers show
Series Fix issues detected by static analyzer | expand

Commit Message

Iker Pedrosa July 17, 2024, 11:36 a.m. UTC
strncpy() truncates the destination buffer if it isn't large enough to
hold the copy. Thus, let's terminate the strings with a NULL character
at the end.

Signed-off-by: Iker Pedrosa <ikerpedrosam@gmail.com>
---
 lib/chip-info.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/lib/chip-info.c b/lib/chip-info.c
index 87fd9e7..2f2523e 100644
--- a/lib/chip-info.c
+++ b/lib/chip-info.c
@@ -57,7 +57,8 @@  gpiod_chip_info_from_uapi(struct gpiochip_info *uapi_info)
 	 * GPIO device must have a name - don't bother checking this field. In
 	 * the worst case (would have to be a weird kernel bug) it'll be empty.
 	 */
-	strncpy(info->name, uapi_info->name, sizeof(info->name));
+	strncpy(info->name, uapi_info->name, sizeof(info->name) - 1);
+	info->name[sizeof(info->name) - 1] = '\0';
 
 	/*
 	 * The kernel sets the label of a GPIO device to "unknown" if it
@@ -66,8 +67,10 @@  gpiod_chip_info_from_uapi(struct gpiochip_info *uapi_info)
 	 */
 	if (uapi_info->label[0] == '\0')
 		strncpy(info->label, "unknown", sizeof(info->label));
-	else
-		strncpy(info->label, uapi_info->label, sizeof(info->label));
+	else {
+		strncpy(info->label, uapi_info->label, sizeof(info->label) - 1);
+		info->label[sizeof(info->label) - 1] = '\0';
+	}
 
 	return info;
 }