Skip to content

Commit

Permalink
Use the safe strlcpy() when present
Browse files Browse the repository at this point in the history
The current macro, which uses strncpy() and then forces a valid
termination, triggers warnings under GCC 10. It can also cause an
out-of-bounds write if size is 0.

strlcpy() was added in glibc 2.38 (July 2023).

Signed-off-by: Loren M. Lang <[email protected]>
Signed-off-by: Aaron Conole <[email protected]>
  • Loading branch information
penguin359 authored and apconole committed Aug 16, 2024
1 parent d24a1df commit 96106e8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ AC_CHECK_FUNCS([strcasecmp])
AC_CHECK_FUNCS([strchr])
AC_CHECK_FUNCS([strdup])
AC_CHECK_FUNCS([strerror])
AC_CHECK_FUNCS([strlcpy])
AC_CHECK_FUNCS([strncasecmp])
AC_CHECK_FUNCS([strpbrk])
AC_CHECK_FUNCS([strrchr])
Expand Down
5 changes: 5 additions & 0 deletions include/lldp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ typedef __u64 u64;
})

/* Use strncpy with N-1 and ensure the string is terminated. */
#ifdef HAVE_STRLCPY
#define STRNCPY_TERMINATED(DEST, SRC, N) \
(void)strlcpy(DEST, SRC, N)
#else
#define STRNCPY_TERMINATED(DEST, SRC, N) \
do { \
strncpy (DEST, SRC, N - 1); \
DEST[N - 1] = '\0'; \
} while (false)
#endif

/*
* Organizationally Unique Identifier (OUI)
Expand Down

0 comments on commit 96106e8

Please sign in to comment.