Skip to content

Commit

Permalink
Replaced some strnlen by memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
scemama committed Dec 5, 2024
1 parent 770b136 commit a5d39ac
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/templates_front/templator_front.org
Original file line number Diff line number Diff line change
Expand Up @@ -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[<<MAX_STRING_LENGTH()>>])
{
strncpy(result, trexio_string_of_error(error), <<MAX_STRING_LENGTH()>>);
result[<<MAX_STRING_LENGTH()>>-1] = '\0';
const char* pSrc = trexio_string_of_error(error);
size_t sizeCp = strnlen(pSrc, <<MAX_STRING_LENGTH()>>);
memcpy(result, pSrc, sizeCp);
result[sizeCp] = '\0';
}
#+end_src

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a5d39ac

Please sign in to comment.