[libc-commits] [libc] [libc] implement mkstemp (PR #199220)

Hardik Chona via libc-commits libc-commits at lists.llvm.org
Sat Jun 6 21:10:10 PDT 2026


================
@@ -0,0 +1,182 @@
+//===-- Unittests for mkstemp ---------------------------------------------===//
+//
+// 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 "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/signal_macros.h"
+#include "src/stdlib/mkstemp.h"
+#include "src/string/strdup.h"
+#include "src/unistd/access.h"
+#include "src/unistd/close.h"
+#include "src/unistd/read.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcMkstempTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcMkstempTest, ValidTemplate) {
+  char *tmpl = LIBC_NAMESPACE::strdup(libc_make_test_file_path("tmp_XXXXXX"));
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+  ::free(tmpl);
----------------
un-pixelated wrote:

Looking at how other file such as `strdup_test.cpp` have implemented the free calls, I chose to follow that pattern and use the system `::free()`.

When I use the namespaced version of free (`LIBC_NAMESPACE::free()`), with the inclusion of `"src/stdlib/free.h"` in the header file and in the CMakeLists.txt DEPENDS `libc.src.stdlib.free` I get this build error:

```
root at bd736d3d8d9f:/llvm# ninja -j5 -C build libc.test.src.stdlib.mkstemp_test.__unit__
ninja: Entering directory `build'
[1/2] Linking CXX executable libc/test/src/stdlib/libc.test.src.stdlib.mkstemp_test.__unit__.__build__
FAILED: libc/test/src/stdlib/libc.test.src.stdlib.mkstemp_test.__unit__.__build__ 
: && /usr/bin/clang++ -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -Xclang -fno-pch-timestamp -g -fpie -ffreestanding -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -Wall -Wextra -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wno-c99-extensions -Wno-gnu-imaginary-constant -Wno-pedantic -Wstrict-prototypes -Wextra-semi -Wnewline-eof -Wnonportable-system-include-path -Wthread-safety libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o -o libc/test/src/stdlib/libc.test.src.stdlib.mkstemp_test.__unit__.__build__  libc/src/string/CMakeFiles/libc.src.string.strlen.__internal__.dir/./strlen.cpp.o  libc/src/__support/OSUtil/linux/CMakeFiles/libc.src.__support.OSUtil.linux.linux_util.dir/./exit.cpp.o  libc/src/__support/OSUtil/linux/CMakeFiles/libc.src.__support.OSUtil.linux.linux_util.dir/./fcntl.cpp.o  libc/src/stdlib/CMakeFiles/libc.src.stdlib.mkstemp.__internal__.dir/./mkstemp.cpp.o  libc/src/errno/CMakeFiles/libc.src.errno.errno.__internal__.dir/./libc_errno.cpp.o  libc/src/string/CMakeFiles/libc.src.string.strdup.__internal__.dir/./strdup.cpp.o  libc/src/unistd/linux/CMakeFiles/libc.src.unistd.linux.access.__internal__.dir/./access.cpp.o  libc/src/unistd/linux/CMakeFiles/libc.src.unistd.linux.close.__internal__.dir/./close.cpp.o  libc/src/unistd/linux/CMakeFiles/libc.src.unistd.linux.read.__internal__.dir/./read.cpp.o  libc/src/unistd/linux/CMakeFiles/libc.src.unistd.linux.unlink.__internal__.dir/./unlink.cpp.o  libc/src/unistd/linux/CMakeFiles/libc.src.unistd.linux.write.__internal__.dir/./write.cpp.o  libc/src/__support/StringUtil/CMakeFiles/libc.src.__support.StringUtil.error_to_string.dir/./error_to_string.cpp.o  libc/test/UnitTest/libLibcDeathTestExecutors.unit.a  libc/test/UnitTest/libLibcTest.unit.a && :
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o: in function `LlvmLibcMkstempTest_ValidTemplate::Run()':
/llvm/libc/test/src/stdlib/mkstemp_test.cpp:38:(.text+0xd4): undefined reference to `__llvm_libc_23_0_0_git::free(void*)'
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o: in function `LlvmLibcMkstempTest_TemplateModifiedInPlace::Run()':
/llvm/libc/test/src/stdlib/mkstemp_test.cpp:58:(.text+0x30c): undefined reference to `__llvm_libc_23_0_0_git::free(void*)'
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o: in function `LlvmLibcMkstempTest_AllCharactersInCharset::Run()':
/llvm/libc/test/src/stdlib/mkstemp_test.cpp:86:(.text+0x5d4): undefined reference to `__llvm_libc_23_0_0_git::free(void*)'
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o: in function `LlvmLibcMkstempTest_FileOpenedCorrectly::Run()':
/llvm/libc/test/src/stdlib/mkstemp_test.cpp:100:(.text+0x7a4): undefined reference to `__llvm_libc_23_0_0_git::free(void*)'
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o: in function `LlvmLibcMkstempTest_Uniqueness::Run()':
/llvm/libc/test/src/stdlib/mkstemp_test.cpp:121:(.text+0xa34): undefined reference to `__llvm_libc_23_0_0_git::free(void*)'
/usr/bin/ld: libc/test/src/stdlib/CMakeFiles/libc.test.src.stdlib.mkstemp_test.__unit__.__build__.dir/mkstemp_test.cpp.o:/llvm/libc/test/src/stdlib/mkstemp_test.cpp:122: more undefined references to `__llvm_libc_23_0_0_git::free(void*)' follow
/usr/bin/ld: libc/test/src/stdlib/libc.test.src.stdlib.mkstemp_test.__unit__.__build__: hidden symbol `_ZN22__llvm_libc_23_0_0_git4freeEPv' isn't defined
/usr/bin/ld: final link failed: bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
```

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


More information about the libc-commits mailing list