[libc-commits] [libc] [libc] Implement vasprintf and asprintf (PR #98824)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Wed Jul 31 13:26:31 PDT 2024
================
@@ -0,0 +1,83 @@
+//===-- Unittests for asprintf--------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdio/asprintf.h"
+#include "src/stdio/sprintf.h"
+#include "src/string/memset.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcASPrintfTest, SimpleNoConv) {
+ char *buff = nullptr;
+ int written;
+ written =
+ LIBC_NAMESPACE::asprintf(&buff, "A simple string with no conversions.");
+ EXPECT_EQ(written, 36);
+ ASSERT_STREQ(buff, "A simple string with no conversions.");
+ free(buff);
+}
+
+TEST(LlvmLibcASPrintfTest, PercentConv) {
+ char *buff = nullptr;
+ int written;
+
+ written = LIBC_NAMESPACE::asprintf(&buff, "%%");
+ EXPECT_EQ(written, 1);
+ ASSERT_STREQ(buff, "%");
+
----------------
michaelrj-google wrote:
you need to `free` buff after every call, not just the last one. `asprintf` never frees the pointer its passed.
https://github.com/llvm/llvm-project/pull/98824
More information about the libc-commits
mailing list