Fix GCC -Wstringop-truncation warning

The code in testcase memberin_extend_c would end up without a
terminating nul if passed a 50 byte string, and then make_upper()
would run off the end of the buffer.

Fix by using strncat() (which always nul terminates the result, and
also doesn't zero-fill the tail of the buffer if the result is
shorter).
This commit is contained in:
Olly Betts 2021-04-19 18:09:24 +12:00
parent 5ec65fde0a
commit 9a82261e4a
1 changed files with 2 additions and 1 deletions

View File

@ -32,7 +32,8 @@ char *Person_name_get(Person *p) {
} }
void Person_name_set(Person *p, char *val) { void Person_name_set(Person *p, char *val) {
strncpy(p->name,val,50); p->name[0] = '\0';
strncat(p->name, val, sizeof(p->name) - 1);
make_upper(p->name); make_upper(p->name);
} }
%} %}