From f7d0f970f2231f7f938de66fb58ea764444b92df Mon Sep 17 00:00:00 2001 From: "Loren M. Lang" Date: Sat, 6 Jul 2024 00:55:43 -0700 Subject: [PATCH] Disable invalid warning about use of strncpy() GCC 10+ is producing a warning that strncpy() may truncate without valid nul termination, however, the code is easily checked that nul termination is added immediately afterwards. Signed-off-by: Loren M. Lang Signed-off-by: Aaron Conole --- include/lldp.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/lldp.h b/include/lldp.h index 8e36141..439c49a 100644 --- a/include/lldp.h +++ b/include/lldp.h @@ -55,6 +55,17 @@ typedef __u64 u64; #ifdef HAVE_STRLCPY #define STRNCPY_TERMINATED(DEST, SRC, N) \ (void)strlcpy(DEST, SRC, N) +#elif __GNUC__ >= 10 +#define STRNCPY_TERMINATED(DEST, SRC, N) \ + do { \ + if((N) > 0) { \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wstringop-truncation\"") \ + strncpy (DEST, SRC, (N) - 1); \ + _Pragma("GCC diagnostic pop") \ + DEST[(N) - 1] = '\0'; \ + } \ + } while (false) #else #define STRNCPY_TERMINATED(DEST, SRC, N) \ do { \