Skip to content

Commit

Permalink
fix: GCC8 and later gives warnings for strncpy and strncat
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomghuang committed Nov 24, 2019
1 parent fef63d8 commit 1c1bb23
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/arg_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/arg_dstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

0 comments on commit 1c1bb23

Please sign in to comment.