[libcxx-commits] [libcxx] [libc++] Replace __resize_default_init with resize_and_overwrite (PR #157121)

Nico Weber via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 16 08:52:31 PDT 2025


================
@@ -34,20 +34,19 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 0) string vformat_string(const ch
 
   va_list apcopy;
   va_copy(apcopy, ap);
-  int ret = ::vsnprintf(buf.data(), buf.size(), msg, apcopy);
+  int size = ::vsnprintf(buf.data(), buf.size(), msg, apcopy);
   va_end(apcopy);
 
   string result;
-  if (static_cast<size_t>(ret) < buf.size()) {
-    result.assign(buf.data(), static_cast<size_t>(ret));
+  if (static_cast<size_t>(size) < buf.size()) {
+    result.assign(buf.data(), static_cast<size_t>(size));
   } else {
     // we did not provide a long enough buffer on our first attempt. The
     // return value is the number of bytes (excluding the null byte) that are
     // needed for formatting.
-    size_t size_with_null = static_cast<size_t>(ret) + 1;
-    result.__resize_default_init(size_with_null - 1);
-    ret = ::vsnprintf(&result[0], size_with_null, msg, ap);
-    _LIBCPP_ASSERT_INTERNAL(static_cast<size_t>(ret) == (size_with_null - 1), "TODO");
+    result.resize_and_overwrite(size, [&](char* res, size_t n) { return ::vsnprintf(res, n, msg, ap); });
----------------
nico wrote:

resize_and_overwrite is c++23-only but this here isn't, is it?

We build in c++20 mode and are now getting:

```
[4057/177031] CXX android_clang_arm/obj/buildtools/third_party/libc++/libc++/filesystem_error.o
../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF android_clang_arm/obj/buildtools/third_party/libc++/libc++/filesystem_error....(too long)
In file included from ../../third_party/libc++/src/src/filesystem/filesystem_error.cpp:15:
../../third_party/libc++/src/src/filesystem/format_string.h:47:12: error: no member named 'resize_and_overwrite' in 'std::string'
   47 |     result.resize_and_overwrite(size, [&](char* res, size_t n) { return ::vsnprintf(res, n, msg, ap); });
      |     ~~~~~~ ^
1 error generated.
```

Are we holding something wrong?

https://github.com/llvm/llvm-project/pull/157121


More information about the libcxx-commits mailing list