From accb6d2ef4a4632176083e892c5990541f1f9d9a Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Mon, 12 Sep 2016 21:55:25 +0300 Subject: [PATCH] Workaround 'va_list used before va_start' cppcheck error in cord Note that -D CPPCHECK should be passed to cppcheck to activate this workaround. * cord/cordprnt.c (CORD_vsprintf) [CPPCHECK]: Force to use va_copy and va_end. * cord/cordprnt.c (CORD_vsprintf): Set res to -1 if invalid format specifier (instead of immediate return -1); call va_end at a single place. --- cord/cordprnt.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/cord/cordprnt.c b/cord/cordprnt.c index 8f91cb8..d3a85da 100644 --- a/cord/cordprnt.c +++ b/cord/cordprnt.c @@ -272,16 +272,17 @@ int CORD_vsprintf(CORD * out, CORD format, va_list args) register char * buf; va_list vsprintf_args; int max_size = 0; - int res; -# ifdef __va_copy + int res = 0; + +# if defined(CPPCHECK) + va_copy(vsprintf_args, args); +# elif defined(__va_copy) __va_copy(vsprintf_args, args); -# else -# if defined(__GNUC__) && !defined(__DJGPP__) \ +# elif defined(__GNUC__) && !defined(__DJGPP__) \ && !defined(__EMX__) /* and probably in other cases */ - va_copy(vsprintf_args, args); -# else - vsprintf_args = args; -# endif + va_copy(vsprintf_args, args); +# else + vsprintf_args = args; # endif if (width == VARIABLE) width = va_arg(args, int); if (prec == VARIABLE) prec = va_arg(args, int); @@ -324,15 +325,11 @@ int CORD_vsprintf(CORD * out, CORD format, va_list args) (void) va_arg(args, double); break; default: -# if defined(__va_copy) \ - || (defined(__GNUC__) && !defined(__DJGPP__) \ - && !defined(__EMX__)) - va_end(vsprintf_args); -# endif - return(-1); + res = -1; } - res = vsprintf(buf, conv_spec, vsprintf_args); -# if defined(__va_copy) \ + if (0 == res) + res = vsprintf(buf, conv_spec, vsprintf_args); +# if defined(CPPCHECK) || defined(__va_copy) \ || (defined(__GNUC__) && !defined(__DJGPP__) \ && !defined(__EMX__)) va_end(vsprintf_args); -- 2.7.4