Arne Georg Gleditsch
2007-Apr-12 10:59 UTC
[syslinux] [PATCH] Make com32 printf obey width-restriction on %s
Hi, The following patch is against 3.36. It lets the width-modifier to the %s conversion specifier restrict the length of the formatted string as well as expand it. In addition, it adds the strnlen function, which only exists as a prototype today. -- Arne. --- syslinux-3.36/com32/lib/Makefile.orig 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 \ --- syslinux-3.36/com32/lib/vsnprintf.c.orig 2007-02-10 21:47:08.000000000 +0100 +++ syslinux-3.36/com32/lib/vsnprintf.c 2007-04-12 12:08:54.000000000 +0200 @@ -362,7 +362,7 @@ case 's': /* String */ sarg = va_arg(ap, const char *); sarg = sarg ? sarg : "(null)"; - slen = strlen(sarg); + slen = width ? strnlen(sarg, width) : strlen(sarg); goto is_string; is_string: --- syslinux-3.36/com32/lib/strnlen.c.orig 2007-04-12 12:07:18.000000000 +0200 +++ 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; +}
Maurice Massar
2007-Apr-13 15:18 UTC
[syslinux] [PATCH] Make com32 printf obey width-restriction on %s
hi, On Thu, Apr 12, 2007 at 12:59:46PM +0200, Arne Georg Gleditsch wrote:> The following patch is against 3.36. It lets the width-modifier to the > %s conversion specifier restrict the length of the formatted string as > well as expand it.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. see: man 3 printf http://www.die.net/doc/linux/man/man3/printf.3.html cu maurice
Arne Georg Gleditsch
2007-Apr-16 07:30 UTC
[syslinux] [PATCH] Make com32 printf obey width-restriction on %s
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: