]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/utmp-wtmp: silence gcc warning about strncpy truncation
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 7 May 2019 13:10:58 +0000 (15:10 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 7 May 2019 19:05:26 +0000 (21:05 +0200)
Unfortunately the warning must be known, or otherwise the pragma generates a
warning or an error. So let's do a meson check for it.

Is it worth doing this to silence the warning? I think so, because apparently
the warning was already emitted by gcc-8.1, and with the recent push in gcc to
catch more such cases, we'll most likely only get more of those.

meson.build
src/basic/macro.h
src/basic/string-util.h
src/shared/utmp-wtmp.c

index 24ef643d44e92a399cd6e1fb8c2cb3df35de6713..b5d06e422621e5cac2913bbb63102460e9c5470a 100644 (file)
@@ -411,11 +411,14 @@ endif
 
 cpp = ' '.join(cc.cmd_array()) + ' -E'
 
+has_wstringop_truncation = cc.has_argument('-Wstringop-truncation')
+
 #####################################################################
 # compilation result tests
 
 conf.set('_GNU_SOURCE', true)
 conf.set('__SANE_USERSPACE_TYPES__', true)
+conf.set10('HAVE_WSTRINGOP_TRUNCATION', has_wstringop_truncation)
 
 conf.set('SIZEOF_PID_T', cc.sizeof('pid_t', prefix : '#include <sys/types.h>'))
 conf.set('SIZEOF_UID_T', cc.sizeof('uid_t', prefix : '#include <sys/types.h>'))
index 1971e912db4f497e676af561733f98286397e4ad..ae8907db04bc719ecd13c16cbe64d2a570c77dc6 100644 (file)
         _Pragma("GCC diagnostic push");                                 \
         _Pragma("GCC diagnostic ignored \"-Wincompatible-pointer-types\"")
 
+#if HAVE_WSTRINGOP_TRUNCATION
+#  define DISABLE_WARNING_STRINGOP_TRUNCATION                           \
+        _Pragma("GCC diagnostic push");                                 \
+        _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"")
+#else
+#  define DISABLE_WARNING_STRINGOP_TRUNCATION                           \
+        _Pragma("GCC diagnostic push")
+#endif
+
 #define REENABLE_WARNING                                                \
         _Pragma("GCC diagnostic pop")
 
index b23f4c834140978b0e6f953721cf72917ebad5a4..a630856236a9a699be23f14a12d7cdbaadd4ff9d 100644 (file)
@@ -212,6 +212,12 @@ static inline size_t strlen_ptr(const char *s) {
         return strlen(s);
 }
 
+DISABLE_WARNING_STRINGOP_TRUNCATION;
+static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
+        strncpy(buf, src, buf_len);
+}
+REENABLE_WARNING;
+
 /* Like startswith(), but operates on arbitrary memory blocks */
 static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
         size_t n;
index db4811b118b784bbc8339e649232b307ae669d6f..646f449821818efe3292b67cddf44a0d8d9c2876 100644 (file)
@@ -209,7 +209,7 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
         copy_suffix(store.ut_id, sizeof(store.ut_id), id);
 
         if (line)
-                strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
+                strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
 
         r = write_entry_both(&store);
         if (r < 0)