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

Rajveer Singh Bharadwaj via libc-commits libc-commits at lists.llvm.org
Thu Oct 31 04:16:33 PDT 2024


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

>From e0a4fb4d2eb92c11acc77ea43ed7583a8161b111 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, size_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/date_and_time.rst                 |  2 +
 libc/include/llvm-libc-types/rsize_t.h      | 16 ++++++
 libc/newhdrgen/yaml/time.yaml               |  8 +++
 libc/spec/stdc.td                           |  9 +++
 libc/src/time/CMakeLists.txt                | 12 ++++
 libc/src/time/ctime_s.cpp                   | 43 +++++++++++++++
 libc/src/time/ctime_s.h                     | 23 ++++++++
 libc/test/src/time/CMakeLists.txt           | 18 ++++++
 libc/test/src/time/ctime_s_test.cpp         | 61 +++++++++++++++++++++
 14 files changed, 197 insertions(+)
 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 68030f7f1775b5..233af0e2915f73 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -205,6 +205,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 5894b591072ef0..1c6f4aa04ca3f0 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -201,6 +201,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 885827d304efe3..fa3208cdbf13b6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -969,6 +969,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 5c09edf7cfb266..717f07a58587fc 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -901,6 +901,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 3ca14ec03de3c7..378abd3bf93e1c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1026,6 +1026,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/date_and_time.rst b/libc/docs/date_and_time.rst
index b745a3b416f802..e8a78ff9fbdb1d 100644
--- a/libc/docs/date_and_time.rst
+++ b/libc/docs/date_and_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/include/llvm-libc-types/rsize_t.h b/libc/include/llvm-libc-types/rsize_t.h
new file mode 100644
index 00000000000000..703469681753d6
--- /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 <stddef.h>
+
+typedef size_t rsize_t;
+
+#endif // LLVM_LIBC_TYPES_RSIZE_T_H
diff --git a/libc/newhdrgen/yaml/time.yaml b/libc/newhdrgen/yaml/time.yaml
index 69b40bef3160dd..ac0ccdde5f4cf6 100644
--- a/libc/newhdrgen/yaml/time.yaml
+++ b/libc/newhdrgen/yaml/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/spec/stdc.td b/libc/spec/stdc.td
index ea032ba5f66e71..d4acceb42c9077 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1630,6 +1630,15 @@ def StdC : StandardSpec<"stdc"> {
                   ArgSpec<CharPtr>,
               ]
           >,
+          FunctionSpec<
+              "ctime_s",
+              RetValSpec<IntType>,
+              [
+                  ArgSpec<CharPtr>,
+                  ArgSpec<SizeTType>,
+                  ArgSpec<ConstTimeTTypePtr>,
+              ]
+          >,
           FunctionSpec<
               "clock",
               RetValSpec<ClockT>,
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index b3318e7ca87fa5..c2fc04ace137f5 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..1bfb720a1f0506
--- /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)) {
+  if (t_ptr == nullptr || buffer == nullptr ||
+      *t_ptr > cpp::numeric_limits<int32_t>::max()) {
+    if (buffer != nullptr)
+      buffer[0] = '\0';
+    return EINVAL;
+  }
+
+  if (buffer_size < TimeConstants::ASCTIME_MAX_BYTES) {
+    buffer[0] = '\0';
+    return ERANGE;
+  }
+
+  if (time_utils::asctime(time_utils::localtime(t_ptr), buffer, buffer_size) ==
+      nullptr) {
+    buffer[0] = '\0';
+    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..91b956bffe8be7
--- /dev/null
+++ b/libc/src/time/ctime_s.h
@@ -0,0 +1,23 @@
+//===-- 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
+
+#include "hdr/types/time_t.h"
+#include "include/llvm-libc-types/rsize_t.h"
+#include "src/__support/macros/config.h"
+#include <stddef.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 bba01f063fed27..7c27be0f23200a 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -66,6 +66,24 @@ 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
+  CXX_STANDARD
+    20
+  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..cf1f8b26e2b936
--- /dev/null
+++ b/libc/test/src/time/ctime_s_test.cpp
@@ -0,0 +1,61 @@
+//===-- 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;
+  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, ValidUnixTimestamp0) {
+  char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
+  time_t t;
+  int result;
+  // 1970-01-01 00:00:00. Test with a valid buffer size.
+  t = 0;
+  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];
+  time_t t;
+  int result;
+  // 2038-01-19 03:14:07. Test with a valid buffer size.
+  t = 2147483647;
+  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;
+  int result;
+  t = 2147483648;
+  result = LIBC_NAMESPACE::ctime_s(buffer, sizeof(buffer), &t);
+  ASSERT_EQ(EINVAL, result);
+}



More information about the libc-commits mailing list