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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Fri Jan 10 10:15:06 PST 2025


================
@@ -0,0 +1,45 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#define __STDC_WANT_LIB_EXT1__ 1
+
+#include "ctime_s.h"
+#include "hdr/errno_macros.h"
+#include "hdr/types/errno_t.h"
+#include "hdr/types/rsize_t.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 {
+
+LLVM_LIBC_FUNCTION(errno_t, 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 < time_constants::ASCTIME_MAX_BYTES ||
+      buffer_size > RSIZE_MAX) {
----------------
nickdesaulniers wrote:

Hmm...so I went to test this with GCC and found that:

```
/android0/llvm-project/libc/src/time/ctime_s.cpp:30:21: error: ‘RSIZE_MAX’ was not declared in this scope; did you mean ‘SIZE_MAX’?
   30 |       buffer_size > RSIZE_MAX) {
      |                     ^~~~~~~~~
      |                     SIZE_MAX
```
Looking into it, I found that clang's stdint provides `RSIZE_MAX` (example: /usr/lib/llvm-16/lib/clang/16/include/stdint.h), but GCC does not (example: /usr/lib/gcc/x86_64-linux-gnu/14/include/stdint.h).

Grepping gcc's sources for `__STDC_LIB_EXT1__` turns up no hits (one hit in the test suite that looks irrelevant).  So looking into why there's seemingly no support for Annex K turned up this mailing list thread: https://gcc.gnu.org/pipermail/gcc/2019-December/231068.html

Which links to this paper documenting in explicit detail glibc developers experience with trying to implement Annex K: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm

95f1de3de5957 added support for RSIZE_MAX and rsize_t without mentioning _who_ was interested in Annex K support.

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2336.pdf looks like a more recent rebuttal to n1967.

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


More information about the libc-commits mailing list