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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Nov 14 11:28:03 PST 2024


================
@@ -0,0 +1,76 @@
+//===-- 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);
+  ASSERT_EQ('\0', buffer[0]);
+
+  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;
+  int result;
+
+  t = 0;
+  result = LIBC_NAMESPACE::ctime_s(buffer, 0, &t);
----------------
nickdesaulniers wrote:

```suggestion
  time_t t = 0;
  int result = LIBC_NAMESPACE::ctime_s(buffer, 0, &t);
```

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


More information about the libc-commits mailing list