From 4c77c2c9ca67d9210c882a432b58bf552b59174a Mon Sep 17 00:00:00 2001 From: Danny Milosavljevic Date: Tue, 27 Aug 2019 04:04:56 +0200 Subject: [PATCH] Fix variable argument list traversal for doubles. * include/stdarg.h (va_align): New macro. (va_arg8): New macro. * lib/stdio/vfprintf.c (vfprintf): Use it. * lib/stdio/vsnprintf.c (vsnprintf): Use it. --- include/stdarg.h | 2 ++ lib/stdio/vfprintf.c | 2 +- lib/stdio/vsnprintf.c | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/stdarg.h b/include/stdarg.h index 34061d06..08c5ded5 100644 --- a/include/stdarg.h +++ b/include/stdarg.h @@ -34,6 +34,8 @@ typedef char *va_list; #define va_start(ap, last) (void)((ap) = (char*)(&(last) + 1)) #define va_arg(ap, type) (type)(((long*)((ap) = ((ap) + sizeof (void*))))[-1]) +#define va_align(ap, alignment) ((((unsigned long) (unsigned char*) ap) + (alignment) - 1) &~ (alignment - 1)) +#define va_arg8(ap, type) (type)(((double*)((ap) = (va_align((ap), 8) + sizeof(double))))[-1]) #define va_end(ap) (void)((ap) = 0) #define va_copy(dest, src) dest = src diff --git a/lib/stdio/vfprintf.c b/lib/stdio/vfprintf.c index 979ad8df..00ac4917 100644 --- a/lib/stdio/vfprintf.c +++ b/lib/stdio/vfprintf.c @@ -188,7 +188,7 @@ vfprintf (FILE * f, char const *format, va_list ap) case 'g': case 'G': { - double d = va_arg (ap, double); + double d = va_arg8 (ap, double); char *s = dtoab (d, 10, 1); if (c == 'E' || c == 'G') strupr (s); diff --git a/lib/stdio/vsnprintf.c b/lib/stdio/vsnprintf.c index a8d9624d..b1ed2e0a 100644 --- a/lib/stdio/vsnprintf.c +++ b/lib/stdio/vsnprintf.c @@ -204,7 +204,7 @@ vsnprintf (char *str, size_t size, char const *format, va_list ap) case 'g': case 'G': { - double d = va_arg (ap, double); + double d = va_arg8 (ap, double); char *s = dtoab (d, 10, 1); if (c == 'E' || c == 'G') strupr (s);