Skip to content

Commit

Permalink
Replace dangerous sprintf() and strcat() with snprintf() and strlcat()
Browse files Browse the repository at this point in the history
This patch replaces the old sprintf() and strcat() APIs with the safer
snprintf() and the OpenBSD strlcat().

Also, minor cleanup to configure and defs.h, no need for all that HAVE
define bonanza anymore.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
  • Loading branch information
troglobit committed Jul 19, 2015
1 parent 7ea5796 commit 0750ea0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
10 changes: 4 additions & 6 deletions configure
Expand Up @@ -190,32 +190,30 @@ done
case $OS in
Linux)
heading
echo "INCLUDES = -Iinclude" >> $CFG
echo "DEFS += -DRAW_OUTPUT_IS_RAW -DIOCTL_OK_ON_RAW_SOCKET" >> $CFG
echo "EXTRA_OBJS = libite/strlcpy.o libite/pidfile.o libite/strtonum.o" >> $CFG
echo "INCLUDES = -Iinclude" >> $CFG
echo "DEFS += -DRAW_OUTPUT_IS_RAW -DIOCTL_OK_ON_RAW_SOCKET" >> $CFG
echo "EXTRA_OBJS = libite/strlcpy.o libite/strlcat.o" >> $CFG
echo "EXTRA_OBJS += libite/pidfile.o libite/strtonum.o" >> $CFG
echo "EXTRA_LIBS =" >> $CFG
;;

FreeBSD)
heading
echo "INCLUDES =" >> $CFG
echo "DEFS += -DHAVE_STRTONUM -DHAVE_STRLCPY" >> $CFG
echo "EXTRA_OBJS = libite/pidfile.o" >> $CFG
echo "EXTRA_LIBS =" >> $CFG
;;

NetBSD)
heading
echo "INCLUDES =" >> $CFG
echo "DEFS += -DHAVE_STRTONUM -DHAVE_STRLCPY -DHAVE_PIDFILE" >> $CFG
echo "EXTRA_OBJS =" >> $CFG
echo "EXTRA_LIBS = -lutil" >> $CFG
;;

OpenBSD)
heading
echo "INCLUDES =" >> $CFG
echo "DEFS += -DHAVE_STRTONUM -DHAVE_STRLCPY -DHAVE_PIDFILE" >> $CFG
echo "EXTRA_OBJS =" >> $CFG
echo "EXTRA_LIBS = -lutil" >> $CFG
;;
Expand Down
8 changes: 4 additions & 4 deletions debug.c
Expand Up @@ -467,15 +467,15 @@ void dump_frame(char *desc, void *dump, size_t len)

while (i < length) {
if (!(i % 16))
sprintf(buf, "%03X: ", i);
snprintf(buf, sizeof(buf), "%03X: ", i);

sprintf(tmp, "%02X ", data[i++]);
strcat(buf, tmp);
snprintf(tmp, sizeof(tmp), "%02X ", data[i++]);
strlcat(buf, tmp, sizeof(buf));

if (i > 0 && !(i % 16))
logit(LOG_DEBUG, 0, "%s", buf);
else if (i > 0 && !(i % 8))
strcat(buf, ":: ");
strlcat(buf, ":: ", sizeof(buf));
}
logit(LOG_DEBUG, 0, "%s", buf);
}
Expand Down
25 changes: 7 additions & 18 deletions defs.h
Expand Up @@ -84,23 +84,9 @@
#include <netinet/ip_mroute.h>
#endif /* __linux__ */

/* If using any of the *BSD or BSD compatible C libraries */
#if defined(HAVE_STRLCPY)
# include <string.h>
#endif
#if defined(HAVE_STRTONUM)
# include <stdlib.h>
#endif
#if defined(HAVE_PIDFILE)
# if defined(OpenBSD) || defined(NetBSD)
# include <util.h>
# else
# include <libutil.h>
# endif
#endif

/* For platforms with none of the *BSD/OpenBSD APIs we use libite. it
* has other goodies we want as well, so we always include it. */
/* If using any of the BSD distributions of UNIX the configure script
* links with -lutil, but on Linux we link with -lite. All required
* APIs are forward declared in lite.h, so we can use it everywhere. */
#include "libite/lite.h"

#include <strings.h>
Expand Down Expand Up @@ -176,7 +162,10 @@ typedef void (*ihfunc_t) (int, fd_set *);

/* Versions of Solaris older than 2.6 don't have routing sockets. */
/* XXX TODO: check FreeBSD version and add all other platforms */
#if defined(__linux__) || (defined(SunOS) && SunOS >=56) || defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (IRIX) || defined (__bsdi__) || defined(NetBSD) || defined(OpenBSD)
#if defined(__linux__) || (defined(SunOS) && SunOS >=56) || \
defined (IRIX) || defined (__bsdi__) || \
defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || \
defined(NetBSD) || defined(OpenBSD)
#define HAVE_ROUTING_SOCKETS 1
#endif

Expand Down

0 comments on commit 0750ea0

Please sign in to comment.