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

Hardik Chona via libc-commits libc-commits at lists.llvm.org
Fri May 22 11:54:18 PDT 2026


https://github.com/un-pixelated updated https://github.com/llvm/llvm-project/pull/199220

>From 2f75c0770861d400ab624e442c82e9708bbb3043 Mon Sep 17 00:00:00 2001
From: Hardik Chona <iamhardikchona at gmail.com>
Date: Fri, 22 May 2026 14:12:32 +0530
Subject: [PATCH] [libc] implement mkstemp

[fix] stdc -> posix

Fix headers in CMakeLists.txt and clean up testcase file

Changed EXPECTs to ASSERTs

formatting changes

[libc] implement mkstemp

Added new line at the end of files

[libc] implement mkstemp
---
 libc/config/linux/x86_64/entrypoints.txt |   1 +
 libc/include/stdlib.yaml                 |   6 +
 libc/src/stdlib/CMakeLists.txt           |  17 +++
 libc/src/stdlib/mkstemp.cpp              |  86 ++++++++++++++
 libc/src/stdlib/mkstemp.h                |  20 ++++
 libc/test/src/stdlib/CMakeLists.txt      |  18 +++
 libc/test/src/stdlib/mkstemp_test.cpp    | 141 +++++++++++++++++++++++
 7 files changed, 289 insertions(+)
 create mode 100644 libc/src/stdlib/mkstemp.cpp
 create mode 100644 libc/src/stdlib/mkstemp.h
 create mode 100644 libc/test/src/stdlib/mkstemp_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4b551ced82138..3f203f16195d4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -198,6 +198,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.llabs
     libc.src.stdlib.lldiv
     libc.src.stdlib.memalignment
+    libc.src.stdlib.mkstemp
     libc.src.stdlib.qsort
     libc.src.stdlib.qsort_r
     libc.src.stdlib.rand
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index 4c958cd9d28ad..06b57f0968329 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -164,6 +164,12 @@ functions:
     return_type: size_t
     arguments:
       - type: const void *
+  - name: mkstemp
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: char *
   - name: posix_memalign
     standards:
       - posix
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index e79353eb2a581..15fa9457b26c0 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -397,6 +397,23 @@ add_entrypoint_object(
     libc.hdr.types.size_t
 )
 
+add_entrypoint_object(
+  mkstemp
+  SRCS
+    mkstemp.cpp
+  HDRS
+    mkstemp.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fcntl_macros
+    libc.src.__support.OSUtil.linux.syscall_wrappers.getrandom
+    libc.src.__support.OSUtil.linux.syscall_wrappers.open
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
+)
+
 add_entrypoint_object(
   mbtowc
   SRCS
diff --git a/libc/src/stdlib/mkstemp.cpp b/libc/src/stdlib/mkstemp.cpp
new file mode 100644
index 0000000000000..5200863046b78
--- /dev/null
+++ b/libc/src/stdlib/mkstemp.cpp
@@ -0,0 +1,86 @@
+//===-- Implementation of 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 "src/stdlib/mkstemp.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/open.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mkstemp, (char *tmpl)) {
+  LIBC_CRASH_ON_NULLPTR(tmpl);
+
+  // keep walking until we find the null terminator
+  int len = 0;
+  char *walk = tmpl;
+  while (*walk != '\0') {
+    walk++;
+    len++;
+  }
+
+  // invalid string length check
+  if (len < 6) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  walk--;
+
+  // when we find it, backtrack six positions
+  int counter = 0;
+  while (*walk == 'X') {
+    counter++;
+    if (counter == 6)
+      break;
+    walk--;
+  }
+
+  // if there aren't six Xs, throw error
+  if (counter != 6) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  char *suffix = tmpl + len - 6;
+
+  const char charset[] = "qwertyuiopasdfghjklzxcvbnm"
+                         "QWERTYUIOPASDFGHJKLZXCVBNM"
+                         "1234567890";
+
+  uint8_t rand_bytes[6];
+
+  while (true) {
+    auto ret = linux_syscalls::getrandom(rand_bytes, 6, 0);
+    if (!ret.has_value()) {
+      libc_errno = ret.error();
+      return -1;
+    }
+
+    for (int i = 0; i < 6; i++) {
+      suffix[i] = charset[rand_bytes[i] % (26 + 26 + 10)];
+    }
+
+    auto fd = linux_syscalls::open(tmpl, O_RDWR | O_CREAT | O_EXCL, 0600);
+    if (!fd.has_value()) {
+      if (fd.error() == EEXIST)
+        continue;
+      libc_errno = fd.error();
+      return -1;
+    }
+
+    return fd.value();
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/mkstemp.h b/libc/src/stdlib/mkstemp.h
new file mode 100644
index 0000000000000..294f23d9d290a
--- /dev/null
+++ b/libc/src/stdlib/mkstemp.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for mkstemp -----------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_MKSTEMP_H
+#define LLVM_LIBC_SRC_STDLIB_MKSTEMP_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int mkstemp(char *tmpl);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDLIB_MKSTEMP_H
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index a7b7c9269fcee..4b5ae2ce2a01a 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -388,6 +388,24 @@ add_libc_test(
     libc.src.stdlib.memalignment
 )
 
+add_libc_test(
+  mkstemp_test
+  SUITE
+    libc-stdlib-tests
+  SRCS
+    mkstemp_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fcntl_macros
+    libc.src.stdlib.mkstemp
+    libc.src.unistd.access
+    libc.src.unistd.close
+    libc.src.unistd.read
+    libc.src.unistd.unlink
+    libc.src.unistd.write
+    libc.test.UnitTest.ErrnoCheckingTest
+)
+
 add_libc_test(
   mbtowc_test
   SUITE
diff --git a/libc/test/src/stdlib/mkstemp_test.cpp b/libc/test/src/stdlib/mkstemp_test.cpp
new file mode 100644
index 0000000000000..d69f634b1730d
--- /dev/null
+++ b/libc/test/src/stdlib/mkstemp_test.cpp
@@ -0,0 +1,141 @@
+//===-- 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/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"
+
+TEST(LlvmLibcMkstempTest, ValidTemplate) {
+  char tmpl[] = "tmp_XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+TEST(LlvmLibcMkstempTest, TemplateModifiedInPlace) {
+  char tmpl[] = "tmp_XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  bool modified = false;
+  for (int i = 4; i < 10; i++)
+    if (tmpl[i] != 'X') {
+      modified = true;
+      break;
+    }
+  EXPECT_TRUE(modified);
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+TEST(LlvmLibcMkstempTest, FileExists) {
+  char tmpl[] = "tmp_XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  EXPECT_EQ(LIBC_NAMESPACE::access(tmpl, F_OK), 0);
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+TEST(LlvmLibcMkstempTest, FdIsWritable) {
+  char tmpl[] = "tmp_XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  const char msg[] = "hello";
+  EXPECT_EQ(LIBC_NAMESPACE::write(fd, msg, 5), static_cast<ssize_t>(5));
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+TEST(LlvmLibcMkstempTest, Uniqueness) {
+  char tmpl1[] = "tmp_XXXXXX";
+  char tmpl2[] = "tmp_XXXXXX";
+  int fd1 = LIBC_NAMESPACE::mkstemp(tmpl1);
+  int fd2 = LIBC_NAMESPACE::mkstemp(tmpl2);
+  ASSERT_GE(fd1, 0);
+  ASSERT_GE(fd2, 0);
+  bool different = false;
+  for (int i = 0; i < 10; i++)
+    if (tmpl1[i] != tmpl2[i]) {
+      different = true;
+      break;
+    }
+  EXPECT_TRUE(different);
+  LIBC_NAMESPACE::close(fd1);
+  LIBC_NAMESPACE::close(fd2);
+  LIBC_NAMESPACE::unlink(tmpl1);
+  LIBC_NAMESPACE::unlink(tmpl2);
+}
+
+TEST(LlvmLibcMkstempTest, FileIsEmpty) {
+  char tmpl[] = "tmp_XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  // read should return 0 on empty file
+  char buf[1];
+  EXPECT_EQ(LIBC_NAMESPACE::read(fd, buf, 1), static_cast<ssize_t>(0));
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+TEST(LlvmLibcMkstempTest, SixXsNoPrefix) {
+  char tmpl[] = "XXXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  ASSERT_GE(fd, 0);
+  LIBC_NAMESPACE::close(fd);
+  LIBC_NAMESPACE::unlink(tmpl);
+}
+
+#if defined(LIBC_ADD_NULL_CHECKS)
+TEST(LlvmLibcMkstempTest, NullPointer) {
+  EXPECT_DEATH([] { LIBC_NAMESPACE::mkstemp(nullptr); }, WITH_SIGNAL(-1));
+}
+#endif
+
+TEST(LlvmLibcMkstempTest, TemplateTooShort) {
+  char tmpl[] = "XXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  EXPECT_EQ(fd, -1);
+  ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST(LlvmLibcMkstempTest, DoesNotEndInXs) {
+  char tmpl[] = "tmp_XXXXXY";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  EXPECT_EQ(fd, -1);
+  ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST(LlvmLibcMkstempTest, XsInMiddleNotEnd) {
+  char tmpl[] = "XXXXXXtmp";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  EXPECT_EQ(fd, -1);
+  ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST(LlvmLibcMkstempTest, FiveXsAtEnd) {
+  char tmpl[] = "tmp_XXXXX";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  EXPECT_EQ(fd, -1);
+  ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST(LlvmLibcMkstempTest, EmptyString) {
+  char tmpl[] = "";
+  int fd = LIBC_NAMESPACE::mkstemp(tmpl);
+  EXPECT_EQ(fd, -1);
+  ASSERT_ERRNO_EQ(EINVAL);
+}
\ No newline at end of file



More information about the libc-commits mailing list