Skip to content

Commit bc83638

Browse files
committed
nilfs-tune: fix gcc8 warnings at user_string() and group_string()
GCC 8 emits -Wstringop-truncation warnings at user_string() and group_string(). This suppresses these warnings by explicitly writing a null character to the last character in buffers, even if it is not computationally necessary. Signed-off-by: Ryusuke Konishi <[email protected]>
1 parent 9c03b44 commit bc83638

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

sbin/nilfs-tune/nilfs-tune.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,12 @@ static const char *user_string(uid_t uid)
260260
static char buf[LOGIN_NAME_MAX + 8];
261261
struct passwd *pwd;
262262

263-
strcpy(buf, "user ");
264-
265263
pwd = getpwuid(uid);
266-
if (pwd)
267-
strncpy(tmp, pwd->pw_name, sizeof(tmp));
268-
else
269-
strcpy(tmp, "unknown");
270-
strcat(buf, tmp);
264+
strncpy(tmp, pwd ? pwd->pw_name : "unknown", sizeof(tmp));
265+
tmp[sizeof(tmp) - 1] = '\0';
266+
267+
snprintf(buf, sizeof(buf), "user %s", tmp);
268+
buf[sizeof(buf) - 1] = '\0';
271269
return buf;
272270
}
273271

@@ -277,14 +275,12 @@ static const char *group_string(gid_t gid)
277275
static char buf[LOGIN_NAME_MAX + 8];
278276
struct group *grp;
279277

280-
strcpy(buf, "group ");
281-
282278
grp = getgrgid(gid);
283-
if (grp)
284-
strncpy(tmp, grp->gr_name, sizeof(tmp));
285-
else
286-
strcpy(tmp, "unknown");
287-
strcat(buf, tmp);
279+
strncpy(tmp, grp ? grp->gr_name : "unknown", sizeof(tmp));
280+
tmp[sizeof(tmp) - 1] = '\0';
281+
282+
snprintf(buf, sizeof(buf), "group %s", tmp);
283+
buf[sizeof(buf) - 1] = '\0';
288284
return buf;
289285
}
290286

0 commit comments

Comments
 (0)