[libc-commits] [libc] [libc][search] implement posix `lfind` function (PR #114692)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Nov 4 09:44:04 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);
----------------
nickdesaulniers wrote:

Usually with interfaces that accept the number of members and a size, there's a potential for `*nmemb * size` to overflow, which should be checked by the library function.  We should do that here.

We seem to have a `mul_overflow` helper fn in __support/memory_size.h, perhaps that should be moved to __support/math_extras.h with the others, then used here?

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


More information about the libc-commits mailing list