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

via libc-commits libc-commits at lists.llvm.org
Mon Nov 4 10:57:14 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 *))) {
----------------
duncpro wrote:

The [standard](https://pubs.opengroup.org/onlinepubs/9799919799/functions/lfind.html) does not explicitly mentioned this case. However it does specify the behavior of the function if an "error" occurs.
> Both functions shall return a null pointer in case of error.

However it does not specify what constitutes an error.

I think we could consider this case an "error", return early (with null pointer like the standard says), and that way we avoid the undefined behavior of dereferencing null pointer when it is given as an argument.

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


More information about the libc-commits mailing list