From cc8b44d85fece6bad8de40fd261a76e8bc3327a8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 25 May 2019 13:26:08 +0200 Subject: [PATCH] Use memcpy instead of strncpy in cases where we want to use only parts of strings. GCC 8.1 and higher warns about using strncpy when it detects possible truncations already at compile time. Signed-off-by: DL6ER --- api.c | 2 +- args.c | 2 +- shmem.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api.c b/api.c index b1585ba5..79980352 100644 --- a/api.c +++ b/api.c @@ -972,7 +972,7 @@ void getVersion(const int *sock) // Extract first 7 characters of the hash char hash[8]; - strncpy(hash, commit, 7); hash[7] = 0; + memcpy(hash, commit, 7); hash[7] = 0; if(strlen(tag) > 1) { if(istelnet[*sock]) diff --git a/args.c b/args.c index eb03049e..a2eb42dd 100644 --- a/args.c +++ b/args.c @@ -62,7 +62,7 @@ void parse_args(int argc, char* argv[]) const char * commit = GIT_HASH; char hash[8]; // Extract first 7 characters of the hash - strncpy(hash, commit, 7); hash[7] = 0; + memcpy(hash, commit, 7); hash[7] = 0; printf("vDev-%s\n", hash); } exit(EXIT_SUCCESS); diff --git a/shmem.c b/shmem.c index 51731d5d..ef8c8a29 100644 --- a/shmem.c +++ b/shmem.c @@ -86,8 +86,8 @@ size_t addstr(const char *str) counters->strings_MAX = shm_strings.size; // Copy the C string pointed by str into the shared string buffer - strncpy(&((char*)shm_strings.ptr)[shmSettings->next_str_pos], str, len); - ((char*)shm_strings.ptr)[shmSettings->next_str_pos + len] = '\0'; + // the length (len+1) ensures that we copy the terminator as well + strncpy(&((char*)shm_strings.ptr)[shmSettings->next_str_pos], str, len + 1); // Increment string length counter shmSettings->next_str_pos += len+1;