From 6c947eeccf61ab9f5893e054c26d8d3967ba12a8 Mon Sep 17 00:00:00 2001 From: "Loren M. Lang" Date: Fri, 5 Jul 2024 19:45:06 -0700 Subject: [PATCH] Make macro safer when strlcpy() is not available Macro would previously trigger an out-of-bound write if passed a size of zero. Also, the size was not protected against a possible operator precedence issue. Signed-off-by: Loren M. Lang Signed-off-by: Aaron Conole --- include/lldp.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/lldp.h b/include/lldp.h index 03f5c7e..8e36141 100644 --- a/include/lldp.h +++ b/include/lldp.h @@ -58,8 +58,10 @@ typedef __u64 u64; #else #define STRNCPY_TERMINATED(DEST, SRC, N) \ do { \ - strncpy (DEST, SRC, N - 1); \ - DEST[N - 1] = '\0'; \ + if((N) > 0) { \ + strncpy (DEST, SRC, (N) - 1); \ + DEST[(N) - 1] = '\0'; \ + } \ } while (false) #endif