diff options
author | tnn <tnn@pkgsrc.org> | 2007-07-20 00:11:25 +0000 |
---|---|---|
committer | tnn <tnn@pkgsrc.org> | 2007-07-20 00:11:25 +0000 |
commit | 943cff783d82116898116332a860ffd8116b6232 (patch) | |
tree | d5b62057d50fdf68ab20867c383a3649212e78b7 /pkgtools/libnbcompat | |
parent | 4d407c5bcb278ff22961cc530c68ed5a10127aad (diff) | |
download | pkgsrc-943cff783d82116898116332a860ffd8116b6232.tar.gz |
Handle trailing NUL correctly.
Diffstat (limited to 'pkgtools/libnbcompat')
-rw-r--r-- | pkgtools/libnbcompat/files/snprintf.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/pkgtools/libnbcompat/files/snprintf.c b/pkgtools/libnbcompat/files/snprintf.c index b44d0175f98..f81189261be 100644 --- a/pkgtools/libnbcompat/files/snprintf.c +++ b/pkgtools/libnbcompat/files/snprintf.c @@ -1,4 +1,4 @@ -/* $NetBSD: snprintf.c,v 1.4 2007/07/19 22:06:43 tnn Exp $ */ +/* $NetBSD: snprintf.c,v 1.5 2007/07/20 00:11:25 tnn Exp $ */ /*- * Copyright (c) 2007 The NetBSD Foundation, Inc. @@ -45,6 +45,8 @@ #include <stdarg.h> #endif +#define MIN(a, b) ((a)<(b)?(a):(b)) + int snprintf(char *str, size_t size, const char *format, ...) { @@ -83,10 +85,11 @@ vsnprintf(char *str, size_t size, const char *format, va_list ap) } vsprintf(p, format, ap); - - memcpy(str, p, len < size ? len : size); - if (len < size) - str[len] = 0; + + if (size > 0) { + memcpy(str, p, MIN(len, size)); + str[MIN(len, size - 1)] = 0; + } if (p != buf) free(p); |