summaryrefslogtreecommitdiff
path: root/src/shared/utmp-wtmp.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-05-07 14:15:46 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-05-07 14:15:46 +0200
commitf1d553e9dfd56f95b7564dd20a0b56e6a0d6492c (patch)
tree99d52173eb5be6459a8c5394489793303b502815 /src/shared/utmp-wtmp.c
parentc98b3545008d8e984ab456dcf79787418fcbfe13 (diff)
downloadsystemd-f1d553e9dfd56f95b7564dd20a0b56e6a0d6492c.tar.gz
systemd-f1d553e9dfd56f95b7564dd20a0b56e6a0d6492c.tar.bz2
systemd-f1d553e9dfd56f95b7564dd20a0b56e6a0d6492c.zip
shared/utmp-wtmp: avoid gcc warning about strncpy truncation
The fact that strncpy does the truncation is the whole point here, and gcc shouldn't warn about this. We can avoid the warning and simplify the whole procedure by directly copying the interesting part.
Diffstat (limited to 'src/shared/utmp-wtmp.c')
-rw-r--r--src/shared/utmp-wtmp.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
index 4b134b6c8a..db4811b118 100644
--- a/src/shared/utmp-wtmp.c
+++ b/src/shared/utmp-wtmp.c
@@ -183,16 +183,14 @@ int utmp_put_reboot(usec_t t) {
return write_entry_both(&store);
}
-_pure_ static const char *sanitize_id(const char *id) {
+static void copy_suffix(char *buf, size_t buf_size, const char *src) {
size_t l;
- assert(id);
- l = strlen(id);
-
- if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
- return id;
-
- return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
+ l = strlen(src);
+ if (l < buf_size)
+ strncpy(buf, src, buf_size);
+ else
+ memcpy(buf, src + l - buf_size, buf_size);
}
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
@@ -207,8 +205,8 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
init_timestamp(&store, 0);
- /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
- strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
+ /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
+ copy_suffix(store.ut_id, sizeof(store.ut_id), id);
if (line)
strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
@@ -244,8 +242,8 @@ int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
setutxent();
- /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
- strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
+ /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
+ copy_suffix(store.ut_id, sizeof(store.ut_id), id);
found = getutxid(&lookup);
if (!found)