From 49a4b976f52c6118ab5fb8a4aca052d52b31e238 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 9 May 2024 16:17:55 +0200 Subject: Proper strncpy null termination --- src/main.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 16470b3..4e62fff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -876,10 +876,19 @@ static bool try_card_has_valid_plane(const char *card_path) { return false; } +static void string_copy(char *dst, const char *src, int len) { + int src_len = strlen(src); + int min_len = src_len; + if(len - 1 < min_len) + min_len = len - 1; + memcpy(dst, src, min_len); + dst[min_len] = '\0'; +} + /* output should be >= 128 bytes */ static bool gsr_get_valid_card_path(gsr_egl *egl, char *output) { if(egl->dri_card_path) { - strncpy(output, egl->dri_card_path, 127); + string_copy(output, egl->dri_card_path, 127); return try_card_has_valid_plane(output); } -- cgit v1.2.3