From a5d39acc92b3866ed291318a0810a84659d749cf Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Thu, 5 Dec 2024 14:00:13 +0100 Subject: [PATCH] Replaced some strnlen by memcpy --- src/templates_front/templator_front.org | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 35c7f26..96fa4a3 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -840,8 +840,10 @@ trexio_string_of_error (const trexio_exit_code error) void trexio_string_of_error_f (const trexio_exit_code error, char result[<>]) { - strncpy(result, trexio_string_of_error(error), <>); - result[<>-1] = '\0'; + const char* pSrc = trexio_string_of_error(error); + size_t sizeCp = strnlen(pSrc, <>); + memcpy(result, pSrc, sizeCp); + result[sizeCp] = '\0'; } #+end_src @@ -1291,9 +1293,11 @@ trexio_open(const char* file_name, const char mode, /* Data for the parent type */ - strncpy(result->file_name, file_name, TREXIO_MAX_FILENAME_LENGTH); - result->file_name[TREXIO_MAX_FILENAME_LENGTH-1] = '\0'; - if (result->file_name[TREXIO_MAX_FILENAME_LENGTH-2] != '\0') { + // See https://stackoverflow.com/a/50198398/4151327 + size_t lenSrc = strnlen(file_name, TREXIO_MAX_FILENAME_LENGTH-1); + if (lenSrc < TREXIO_MAX_FILENAME_LENGTH) { + memcpy(result->file_name, file_name, lenSrc+1); + } else { if (rc_open != NULL) *rc_open = TREXIO_INVALID_ARG_1; free(result); return NULL;