[libc-commits] [libc] [libc][search] implement posix `lfind` function (PR #114692)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 4 11:30:22 PST 2024
================
@@ -0,0 +1,29 @@
+//===-- Implementation of lfind -------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/search/lfind.h"
+#include "src/__support/CPP/cstddef.h" // cpp::byte
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(void *, lfind,
+ (void *key, void *base, size_t *nmemb, size_t size,
+ int (*compar)(void *, void *))) {
+ cpp::byte *next = reinterpret_cast<cpp::byte *>(base);
+ cpp::byte *end = next + (*nmemb * size);
----------------
duncpro wrote:
The [standard](https://pubs.opengroup.org/onlinepubs/9799919799/functions/lfind.html) says we should return `nullptr` if an "error" occurs.
I'll modify this to use `mul_overflow` to calculate the byte length, if it overflows, I'll return `nullptr` indicating an error.
Then in a subsequent PR, we can consider moving `mul_overflow` into `__support/math_extras.h`.
That way we can update all the `#include`s at once.
https://github.com/llvm/llvm-project/pull/114692
More information about the libc-commits
mailing list