C99 states that vsnprintf() "returns the number of characters that would have been printed if the n were unlimited"; but according to the Linux manual page "glibc until 2.0.6 would return -1 when the output was truncated" -- so we have to handle both cases ... --- src/ngircd/conn.c | 13 +++++++++++-- src/portab/portabtest.c | 12 ++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-)
diff --git a/src/ngircd/conn.c b/src/ngircd/conn.c index 3f447c6..fef568e 100644 --- a/src/ngircd/conn.c +++ b/src/ngircd/conn.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors. + * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -984,6 +984,7 @@ va_dcl size_t len; bool ok; va_list ap; + int r;
assert( Idx > NONE ); assert( Format != NULL ); @@ -993,7 +994,8 @@ va_dcl #else va_start( ap ); #endif - if (vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 ) { + r = vsnprintf(buffer, COMMAND_LEN - 2, Format, ap); + if (r >= COMMAND_LEN - 2 || r == -1) { /* * The string that should be written to the socket is longer * than the allowed size of COMMAND_LEN bytes (including both @@ -1014,6 +1016,13 @@ va_dcl * an other server only routing the message!), so the only * option left is to shorten the string and to hope that the * result is still somewhat useful ... + * + * Note: + * C99 states that vsnprintf() "returns the number of characters + * that would have been printed if the n were unlimited"; but + * according to the Linux manual page "glibc until 2.0.6 would + * return -1 when the output was truncated" -- so we have to + * handle both cases ... * -alex- */
diff --git a/src/portab/portabtest.c b/src/portab/portabtest.c index 9e6bd22..6c6f9e6 100644 --- a/src/portab/portabtest.c +++ b/src/portab/portabtest.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors. + * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -139,14 +139,22 @@ va_dcl { char str[5]; va_list ap; + int r;
#ifdef PROTOTYPES va_start(ap, Format); #else va_start(ap); #endif - if (vsnprintf(str, sizeof(str), Format, ap) != Len) + r = vsnprintf(str, sizeof(str), Format, ap); + if (r != Len && r != -1) { + /* C99 states that vsnprintf() "returns the number of characters + * that would have been printed if the n were unlimited"; but + * according to the Linux manual page "glibc until 2.0.6 would + * return -1 when the output was truncated" -- so we have to + * handle both cases ... */ Panic("vsnprintf return code"); + } va_end(ap);
if (str[4] != '\0')