[libc-commits] [libc] [libc] Add `ctime_s` (PR #110676)

Rajveer Singh Bharadwaj via libc-commits libc-commits at lists.llvm.org
Tue Jan 7 02:49:12 PST 2025


https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/110676

>From fd7a9723bdaaca5848bb434e518b796d13e6b651 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Tue, 1 Oct 2024 19:09:11 +0530
Subject: [PATCH] [libc] Add `ctime_s`

Resolves #110548

Definition:

```c++
int ctime_s(char *buffer, rsize_t buffer_size, const time_t *t_ptr);
```
---
 libc/config/baremetal/arm/entrypoints.txt   |  1 +
 libc/config/baremetal/riscv/entrypoints.txt |  1 +
 libc/config/linux/aarch64/entrypoints.txt   |  1 +
 libc/config/linux/riscv/entrypoints.txt     |  1 +
 libc/config/linux/x86_64/entrypoints.txt    |  1 +
 libc/docs/headers/time.rst                  |  2 +
 libc/hdr/types/rsize_t.h                    | 23 ++++++++
 libc/include/llvm-libc-types/rsize_t.h      | 16 +++++
 libc/include/time.yaml                      |  8 +++
 libc/src/time/CMakeLists.txt                | 12 ++++
 libc/src/time/ctime_s.cpp                   | 43 ++++++++++++++
 libc/src/time/ctime_s.h                     | 24 ++++++++
 libc/test/src/time/CMakeLists.txt           | 16 +++++
 libc/test/src/time/ctime_s_test.cpp         | 65 +++++++++++++++++++++
 14 files changed, 214 insertions(+)
 create mode 100644 libc/hdr/types/rsize_t.h
 create mode 100644 libc/include/llvm-libc-types/rsize_t.h
 create mode 100644 libc/src/time/ctime_s.cpp
 create mode 100644 libc/src/time/ctime_s.h
 create mode 100644 libc/test/src/time/ctime_s_test.cpp

diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 694cd7b1993ca2..fa609faa8cd85f 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -206,6 +206,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.asctime_r
     libc.src.time.ctime
     libc.src.time.ctime_r
+    libc.src.time.ctime_s
     libc.src.time.difftime
     libc.src.time.gmtime
     libc.src.time.gmtime_r
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 6dc5df830eb000..68a348ef881a1b 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -202,6 +202,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.asctime_r
     libc.src.time.ctime
     libc.src.time.ctime_r
+    libc.src.time.ctime_s
     libc.src.time.difftime
     libc.src.time.gmtime
     libc.src.time.gmtime_r
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 00f0c6a8bfb8e4..21edf9a155a624 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1010,6 +1010,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.asctime_r
     libc.src.time.ctime
     libc.src.time.ctime_r
+    libc.src.time.ctime_s
     libc.src.time.clock
     libc.src.time.clock_gettime
     libc.src.time.difftime
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 49a8d61b938027..5fcbf8aa181258 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -941,6 +941,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.asctime_r
     libc.src.time.ctime
     libc.src.time.ctime_r
+    libc.src.time.ctime_s
     libc.src.time.clock
     libc.src.time.clock_gettime
     libc.src.time.difftime
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7e549607716c02..d9e7242acdf480 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1095,6 +1095,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.time.asctime_r
     libc.src.time.ctime
     libc.src.time.ctime_r
+    libc.src.time.ctime_s
     libc.src.time.clock
     libc.src.time.clock_gettime
     libc.src.time.difftime
diff --git a/libc/docs/headers/time.rst b/libc/docs/headers/time.rst
index de82d80a2bec48..ea5ee515a9bba8 100644
--- a/libc/docs/headers/time.rst
+++ b/libc/docs/headers/time.rst
@@ -59,6 +59,8 @@ Implementation Status
 +---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
 | ctime_r             | |check| | |check| |         |     |check|     |         |         |         |         |         |         |         |         |
 +---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
+| ctime_s             | |check| | |check| |         |     |check|     |         |         |         |         |         |         |         |         |
++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
 | clock               | |check| | |check| |         |     |check|     |         |         |         |         |         |         |         |         |
 +---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+
 | clock_getcpuclockid |         |         |         |                 |         |         |         |         |         |         |         |         |
diff --git a/libc/hdr/types/rsize_t.h b/libc/hdr/types/rsize_t.h
new file mode 100644
index 00000000000000..6b441b495ad418
--- /dev/null
+++ b/libc/hdr/types/rsize_t.h
@@ -0,0 +1,23 @@
+//===-- Proxy for rsize_t -------------------------------------------------===//
+//
+// 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_HDR_TYPES_RSIZE_T_H
+#define LLVM_LIBC_HDR_TYPES_RSIZE_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/rsize_t.h"
+
+#else
+
+#define __need_rsize_t
+#include <stddef.h>
+#undef __need_rsize_t
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_RSIZE_T_H
diff --git a/libc/include/llvm-libc-types/rsize_t.h b/libc/include/llvm-libc-types/rsize_t.h
new file mode 100644
index 00000000000000..c0ef39e452fb92
--- /dev/null
+++ b/libc/include/llvm-libc-types/rsize_t.h
@@ -0,0 +1,16 @@
+//===-- Definition of rsize_t type ----------------------------------------===//
+//
+// 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_TYPES_RSIZE_T_H
+#define LLVM_LIBC_TYPES_RSIZE_T_H
+
+#include "size_t.h"
+
+typedef size_t rsize_t;
+
+#endif // LLVM_LIBC_TYPES_RSIZE_T_H
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index 3f745e5ee33868..6d9c2624f3e160 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -37,6 +37,14 @@ functions:
     arguments:
       - type: const time_t *
       - type: char *
+  - name: ctime_s
+    standard:
+      - stdc
+    return_type: int
+    arguments:
+      - type: char *
+      - type: size_t
+      - type: const time_t *
   - name: clock
     standard:
       - stdc
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index ae835dcc742742..6c1d071695f25b 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -60,6 +60,18 @@ add_entrypoint_object(
     libc.include.time
 )
 
+add_entrypoint_object(
+  ctime_s
+  SRCS
+    ctime_s.cpp
+  HDRS
+    ctime_s.h
+  DEPENDS
+    .time_utils
+    libc.hdr.types.time_t
+    libc.include.time
+)
+
 add_entrypoint_object(
   difftime
   SRCS
diff --git a/libc/src/time/ctime_s.cpp b/libc/src/time/ctime_s.cpp
new file mode 100644
index 00000000000000..27fd682a3af9b1
--- /dev/null
+++ b/libc/src/time/ctime_s.cpp
@@ -0,0 +1,43 @@
+//===-- Implementation of ctime_s function --------------------------------===//
+//
+// 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 "ctime_s.h"
+#include "hdr/errno_macros.h"
+#include "src/__support/CPP/limits.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+LLVM_LIBC_FUNCTION(int, ctime_s,
+                   (char *buffer, rsize_t buffer_size, const time_t *t_ptr)) {
+  // TODO (https://github.com/llvm/llvm-project/issues/115907): invoke
+  // constraint handler
+  if (buffer == nullptr || t_ptr == nullptr)
+    return EINVAL;
+
+  if (buffer_size < TimeConstants::ASCTIME_MAX_BYTES ||
+      buffer_size > RSIZE_MAX) {
+    buffer[0] = '\0';
+    return ERANGE;
+  }
+
+  if (*t_ptr > cpp::numeric_limits<int32_t>::max())
+    return EINVAL;
+
+  if (time_utils::asctime(time_utils::localtime(t_ptr), buffer, buffer_size) ==
+      nullptr)
+    return EINVAL;
+
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/ctime_s.h b/libc/src/time/ctime_s.h
new file mode 100644
index 00000000000000..7c01eab083ae0c
--- /dev/null
+++ b/libc/src/time/ctime_s.h
@@ -0,0 +1,24 @@
+//===-- Implementation header of ctime_s ------------------------*- 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_TIME_CTIME_S_H
+#define LLVM_LIBC_SRC_TIME_CTIME_S_H
+
+#define __STDC_WANT_LIB_EXT1__ 1
+
+#include "hdr/types/rsize_t.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int ctime_s(char *buffer, rsize_t buffer_size, const time_t *t_ptr);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_CTIME_S_H
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index da3903f3e0e494..b1cc9920cb014e 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -66,6 +66,22 @@ add_libc_unittest(
     libc.src.time.time_utils
 )
 
+add_libc_unittest(
+  ctime_s_test
+  SUITE
+    libc_time_unittests
+  SRCS
+    ctime_s_test.cpp
+  HDRS
+    TmHelper.h
+    TmMatcher.h
+  DEPENDS
+    libc.include.time
+    libc.hdr.types.time_t
+    libc.src.time.ctime_s
+    libc.src.time.time_utils
+)
+
 add_libc_test(
   clock_gettime_test
   SUITE
diff --git a/libc/test/src/time/ctime_s_test.cpp b/libc/test/src/time/ctime_s_test.cpp
new file mode 100644
index 00000000000000..be8d174413a13e
--- /dev/null
+++ b/libc/test/src/time/ctime_s_test.cpp
@@ -0,0 +1,65 @@
+//===-- Unittests for ctime_s ---------------------------------------------===//
+//
+// 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 "src/errno/libc_errno.h"
+#include "src/time/ctime_s.h"
+#include "src/time/time_utils.h"
+#include "test/UnitTest/Test.h"
+#include "test/src/time/TmHelper.h"
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+TEST(LlvmLibcCtimeS, Nullptr) {
+  int result = LIBC_NAMESPACE::ctime_s(nullptr, 0, nullptr);
+  ASSERT_EQ(EINVAL, result);
+
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), nullptr);
+  ASSERT_EQ(EINVAL, result);
+
+  time_t t;
+  result = LIBC_NAMESPACE::ctime_s(nullptr, 0, &t);
+  ASSERT_EQ(EINVAL, result);
+}
+
+TEST(LlvmLibcCtimeS, InvalidBufferSize) {
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+
+  time_t t = 0;
+  int result = LIBC_NAMESPACE::ctime_s(buffer, 0, &t);
+  ASSERT_EQ(ERANGE, result);
+
+  result = LIBC_NAMESPACE::ctime_s(buffer, RSIZE_MAX + 1, &t);
+  ASSERT_EQ(ERANGE, result);
+}
+
+TEST(LlvmLibcCtimeS, ValidUnixTimestamp0) {
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  // 1970-01-01 00:00:00. Test with a valid buffer size.
+  time_t t = 0;
+  int result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+  ASSERT_STREQ("Thu Jan  1 00:00:00 1970\n", buffer);
+  ASSERT_EQ(0, result);
+}
+
+TEST(LlvmLibcCtimeS, ValidUnixTimestamp32Int) {
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  // 2038-01-19 03:14:07. Test with a valid buffer size.
+  time_t t = 2147483647;
+  int result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+  ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", buffer);
+  ASSERT_EQ(0, result);
+}
+
+TEST(LlvmLibcCtimeS, InvalidArgument) {
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  time_t t = 2147483648;
+  int result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+  ASSERT_EQ(EINVAL, result);
+}



More information about the libc-commits mailing list