[syslinux] [PATCH] Make com32 printf obey width-restriction on %s

Arne Georg Gleditsch argggh at dolphinics.no
Mon Apr 16 00:30:18 PDT 2007


Maurice Massar wrote:
 > The field width specifies the minimum field width:
 > "In no case does a [..] small field width cause truncation of a field;"
 >
 > If you want to restrict the length, specify a precision, like:
 > "%.10s", which will print a most 10 characters.

D'oh.  You're right, of course.  I'd still recommend using strnlen,
though, since the presence of a precision specifier means the string
might not be null-terminated.  I'm appending that part of the patch.

-- 
								Arne.

diff -ru syslinux-3.36.orig/com32/lib/Makefile syslinux-3.36/com32/lib/Makefile
--- syslinux-3.36.orig/com32/lib/Makefile	2007-02-10 21:47:07.000000000 +0100
+++ syslinux-3.36/com32/lib/Makefile	2007-04-12 12:06:07.000000000 +0200
@@ -11,7 +11,7 @@
  	perror.o printf.o puts.o qsort.o realloc.o seed48.o snprintf.o	\
  	sprintf.o srand48.o sscanf.o stack.o strcasecmp.o strcat.o	\
  	strchr.o strcmp.o strcpy.o strdup.o strerror.o strlen.o		\
-	strncasecmp.o strncat.o strncmp.o strncpy.o strndup.o		\
+	strncasecmp.o strncat.o strncmp.o strncpy.o strndup.o strnlen.o	\
  	strntoimax.o strntoumax.o strrchr.o strsep.o strspn.o strstr.o	\
  	strtoimax.o strtok.o strtol.o strtoll.o strtoul.o strtoull.o	\
  	strtoumax.o vfprintf.o vprintf.o vsnprintf.o vsprintf.o		\
diff -ru syslinux-3.36.orig/com32/lib/strnlen.c syslinux-3.36/com32/lib/strnlen.c
--- syslinux-3.36.orig/com32/lib/strnlen.c	1970-01-01 00:00:00.000000000 +0100
+++ syslinux-3.36/com32/lib/strnlen.c	2007-04-12 12:05:41.000000000 +0200
@@ -0,0 +1,14 @@
+/*
+ * strnlen.c
+ */
+
+#include <string.h>
+
+size_t strnlen(const char *s, size_t maxlen)
+{
+  const char *ss = s;
+  while ( *ss && maxlen ) {
+    ss++; maxlen--;
+  }
+  return ss-s;
+}
diff -ru syslinux-3.36.orig/com32/lib/vsnprintf.c syslinux-3.36/com32/lib/vsnprintf.c
--- syslinux-3.36.orig/com32/lib/vsnprintf.c	2007-02-10 21:47:08.000000000 +0100
+++ syslinux-3.36/com32/lib/vsnprintf.c	2007-04-16 09:17:20.000000000 +0200
@@ -362,7 +362,7 @@
  	case 's':		/* String */
  	  sarg = va_arg(ap, const char *);
  	  sarg = sarg ? sarg : "(null)";
-	  slen = strlen(sarg);
+	  slen = prec != -1 ? strnlen(sarg, prec) : strlen(sarg);
  	  goto is_string;

  	is_string:




More information about the Syslinux mailing list