[libcxx-commits] [PATCH] D97473: [SystemZ][z/OS] vasprintf fix libc++

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 1 10:41:44 PST 2021


ldionne requested changes to this revision.
ldionne added a comment.
This revision now requires changes to proceed.

We have the same issue in `__libcpp_vasprintf` in `libcxx/src/support/win32/locale_win32.cpp` - we should fix both in one go.



================
Comment at: libcxx/include/__support/ibm/xlocale.h:256-270
+  int str_size = -1;
+  va_list ap2;
+  va_copy(ap2, ap);
+  int count = vsnprintf(0, 0, fmt, ap2);
+  va_end(ap2);
+  if (count < 0) return -1;
+  const size_t buf_size = count + 1;
----------------
Is there a reason for calling `vsnprintf` twice unconditionally now instead of doing the same thing as before, but providing a copy of the `va_list` for the first call?

In other words, why not do:

```
static inline
int vasprintf(char **strp, const char *fmt, va_list ap)
{
  const size_t buff_size = 256;
  if ((*strp = (char *)malloc(buff_size)) == NULL)
  {
    return -1;
  }

  va_list ap_copy;
  va_copy(ap_copy, ap);
  int str_size = vsnprintf(*strp, buff_size, fmt,  ap_copy);
  va_end(ap_copy);

  if (str_size >= buff_size)
  {
    if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL)
    {
      return -1;
    }
    str_size = vsnprintf(*strp, str_size + 1, fmt,  ap);
  }
  return str_size;
}
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97473/new/

https://reviews.llvm.org/D97473



More information about the libcxx-commits mailing list