From 812da4190f8ac2f548f3bea09b85164b78d33347 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 4 May 2014 23:37:24 +0200 Subject: mprintf: allow %.s with data not being zero terminated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the precision is indeed shorter than the string, don't strlen() to find the end because that's not how the precision operator works. I also added a unit test for curl_msnprintf to make sure this works and that the fix doesn't a few other basic use cases. I found a POSIX compliance problem that I marked TODO in the unit test, and I figure we need to add more tests in the future. Reported-by: Török Edwin --- lib/mprintf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/mprintf.c') diff --git a/lib/mprintf.c b/lib/mprintf.c index cc6042a3c..23070a764 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1999 - 2013, Daniel Stenberg, , et al. + * Copyright (C) 1999 - 2014, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -804,11 +804,11 @@ static int dprintf_formatf( len = 0; } } + else if(prec != -1) + len = (size_t)prec; else len = strlen(str); - if(prec != -1 && (size_t) prec < len) - len = (size_t)prec; width -= (long)len; if(p->flags & FLAGS_ALT) @@ -818,7 +818,7 @@ static int dprintf_formatf( while(width-- > 0) OUTCHAR(' '); - while(len-- > 0) + while((len-- > 0) && *str) OUTCHAR(*str++); if(p->flags&FLAGS_LEFT) while(width-- > 0) -- cgit v1.2.1