[libcxx-commits] [libcxx] f6af5ef - [SystemZ][z/OS] vasprintf fix libc++

Muiez Ahmed via libcxx-commits libcxx-commits at lists.llvm.org
Thu Mar 18 12:01:16 PDT 2021


Author: Muiez Ahmed
Date: 2021-03-18T15:00:57-04:00
New Revision: f6af5efcec4171080c036ad55a2b4db9fc5c37fa

URL: https://github.com/llvm/llvm-project/commit/f6af5efcec4171080c036ad55a2b4db9fc5c37fa
DIFF: https://github.com/llvm/llvm-project/commit/f6af5efcec4171080c036ad55a2b4db9fc5c37fa.diff

LOG: [SystemZ][z/OS] vasprintf fix libc++

The aim is to use the correct vasprintf implementation for z/OS libc++, where a copy of va_list ap is needed. In particular, it avoids the potential that the initial internal call to vsnprintf will modify ap and the subsequent call to vsnprintf will use that modified ap.

Differential Revision: https://reviews.llvm.org/D97473

Added: 
    

Modified: 
    libcxx/include/__support/ibm/xlocale.h
    libcxx/src/support/win32/support.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__support/ibm/xlocale.h b/libcxx/include/__support/ibm/xlocale.h
index b4d21172bcfa..563b465a8f65 100644
--- a/libcxx/include/__support/ibm/xlocale.h
+++ b/libcxx/include/__support/ibm/xlocale.h
@@ -10,6 +10,7 @@
 #ifndef _LIBCPP_SUPPORT_IBM_XLOCALE_H
 #define _LIBCPP_SUPPORT_IBM_XLOCALE_H
 
+#include <stdarg.h>
 #include <__support/ibm/locale_mgmt_aix.h>
 #include <__support/ibm/locale_mgmt_zos.h>
 
@@ -268,18 +269,19 @@ unsigned long strtoul_l(const char *__nptr, char **__endptr,
 }
 
 static inline
-int vasprintf(char **strp, const char *fmt, va_list ap)
-{
+int vasprintf(char **strp, const char *fmt, va_list ap) {
   const size_t buff_size = 256;
-  int str_size;
-  if ((*strp = (char *)malloc(buff_size)) == NULL)
-  {
+  if ((*strp = (char *)malloc(buff_size)) == NULL) {
     return -1;
   }
-  if ((str_size = vsnprintf(*strp, buff_size, fmt,  ap)) >= buff_size)
-  {
-    if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL)
-    {
+
+  va_list ap_copy;
+  va_copy(ap_copy, ap);
+  int str_size = vsnprintf(*strp, buff_size, fmt,  ap_copy);
+  va_end(ap_copy);
+
+  if ((size_t) str_size >= buff_size) {
+    if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL) {
       return -1;
     }
     str_size = vsnprintf(*strp, str_size + 1, fmt,  ap);

diff  --git a/libcxx/src/support/win32/support.cpp b/libcxx/src/support/win32/support.cpp
index 52453f547926..5890e669a34e 100644
--- a/libcxx/src/support/win32/support.cpp
+++ b/libcxx/src/support/win32/support.cpp
@@ -22,7 +22,10 @@ int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap )
 {
     *sptr = NULL;
     // Query the count required.
-    int count = _vsnprintf( NULL, 0, format, ap );
+    va_list ap_copy;
+    va_copy(ap_copy, ap);
+    int count = _vsnprintf( NULL, 0, format, ap_copy );
+    va_end(ap_copy);
     if (count < 0)
         return count;
     size_t buffer_size = static_cast<size_t>(count) + 1;


        


More information about the libcxx-commits mailing list