From 1c1bb23b305c8cf349328fc0cacd7beb7a575ff4 Mon Sep 17 00:00:00 2001 From: "Tom G. Huang" Date: Sun, 24 Nov 2019 11:39:35 -0800 Subject: [PATCH] fix: GCC8 and later gives warnings for strncpy and strncat When we use strncpy and strncat, we have allocated one more byte for the terminal NULL character. However, the GCC after version 8 will report warnings (-Wstringop-truncation) even when we use strncpy and strncat correctly. Replace strncpy and strncat with memcpy to avoid these warnings. --- src/arg_cmd.c | 12 ++++++------ src/arg_dstr.c | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/arg_cmd.c b/src/arg_cmd.c index 6fcd5db..1141b57 100644 --- a/src/arg_cmd.c +++ b/src/arg_cmd.c @@ -61,7 +61,7 @@ void arg_set_module_name(const char* name) { #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) strncpy_s(s_module_name, slen + 1, name, slen); #else - strncpy(s_module_name, name, slen); + memcpy(s_module_name, name, slen); #endif } @@ -81,7 +81,7 @@ void arg_set_module_version(int major, int minor, int patch, const char* tag) { #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) strncpy_s(s_mod_ver_tag, slen_tag + 1, tag, slen_tag); #else - strncpy(s_mod_ver_tag, tag, slen_tag); + memcpy(s_mod_ver_tag, tag, slen_tag); #endif ds = arg_dstr_create(); @@ -98,7 +98,7 @@ void arg_set_module_version(int major, int minor, int patch, const char* tag) { #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) strncpy_s(s_mod_ver, slen_ds + 1, arg_dstr_cstr(ds), slen_ds); #else - strncpy(s_mod_ver, arg_dstr_cstr(ds), slen_ds); + memcpy(s_mod_ver, arg_dstr_cstr(ds), slen_ds); #endif arg_dstr_destroy(ds); @@ -153,8 +153,8 @@ void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description strncpy_s(cmd_info->name, ARG_CMD_NAME_LEN, name, strlen(name)); strncpy_s(cmd_info->description, ARG_CMD_DESCRIPTION_LEN, description, strlen(description)); #else - strncpy(cmd_info->name, name, strlen(name)); - strncpy(cmd_info->description, description, strlen(description)); + memcpy(cmd_info->name, name, strlen(name)); + memcpy(cmd_info->description, description, strlen(description)); #endif cmd_info->proc = proc; @@ -166,7 +166,7 @@ void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) strncpy_s((char*)k, slen_name + 1, name, slen_name); #else - strncpy((char*)k, name, slen_name); + memcpy((char*)k, name, slen_name); #endif arg_hashtable_insert(s_hashtable, k, cmd_info); diff --git a/src/arg_dstr.c b/src/arg_dstr.c index 76e7a92..884db66 100644 --- a/src/arg_dstr.c +++ b/src/arg_dstr.c @@ -174,12 +174,12 @@ char* arg_dstr_cstr(arg_dstr_t ds) /* Interpreter whose result to return. */ void arg_dstr_cat(arg_dstr_t ds, const char* str) { setup_append_buf(ds, (int)strlen(str) + 1); - strncat(ds->data, str, strlen(str)); + memcpy(ds->data + strlen(ds->data), str, strlen(str)); } void arg_dstr_catc(arg_dstr_t ds, char c) { setup_append_buf(ds, 2); - strncat(ds->data, &c, 1); + memcpy(ds->data + strlen(ds->data), &c, 1); } /* @@ -296,6 +296,7 @@ static void setup_append_buf(arg_dstr_t ds, int new_space) { total_space *= 2; } newbuf = (char*)xmalloc((unsigned)total_space); + memset(newbuf, 0, total_space); strcpy(newbuf, ds->data); if (ds->append_data != NULL) { xfree(ds->append_data);