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@xxxxxxxxx> --- lib/chip-info.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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; } -- 2.45.2