[libc-commits] [libc] [libc][search] implement POSIX `lsearch` function (PR #116870)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Tue Nov 19 14:13:55 PST 2024
================
@@ -0,0 +1,79 @@
+//===-- Unittests for lsearch ---------------------------------------------===//
+//
+// 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/lsearch.h"
+#include "test/UnitTest/Test.h"
+
+int compar(const void *a, const void *b) {
+ return *reinterpret_cast<const int *>(a) != *reinterpret_cast<const int *>(b);
+}
+
+TEST(LlvmLibcLsearchTest, SearchHead) {
+ int list[4] = {1, 2, 3, 4};
+ size_t len = 3;
+ int key = 1;
+ void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
+
+ ASSERT_EQ(static_cast<int *>(ret), &list[0]);
+ ASSERT_EQ(len, static_cast<size_t>(3));
+ ASSERT_EQ(list[1], 2);
+ ASSERT_EQ(list[2], 3);
+ ASSERT_EQ(list[3], 4);
+ ASSERT_EQ(list[3], 4);
+}
+
+TEST(LlvmLibcLsearchTest, SearchMiddle) {
+ int list[4] = {1, 2, 3, 4};
+ size_t len = 3;
+ int key = 2;
+ void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
+ ASSERT_EQ(static_cast<int *>(ret), &list[1]);
+ ASSERT_EQ(len, static_cast<size_t>(3));
+ ASSERT_EQ(list[0], 1);
+ ASSERT_EQ(list[1], 2);
+ ASSERT_EQ(list[2], 3);
+ ASSERT_EQ(list[3], 4);
+}
+
+TEST(LlvmLibcLsearchTest, SearchTail) {
+ int list[4] = {1, 2, 3, 4};
+ size_t len = 3;
+ int key = 3;
+ void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
+ ASSERT_EQ(static_cast<int *>(ret), &list[2]);
+ ASSERT_EQ(len, static_cast<size_t>(3));
+ ASSERT_EQ(list[0], 1);
+ ASSERT_EQ(list[1], 2);
+ ASSERT_EQ(list[2], 3);
+ ASSERT_EQ(list[3], 4);
+}
+
+TEST(LlvmLibcLsearchTest, SearchNonExistent) {
+ int list[4] = {1, 2, 3, 4};
+ size_t len = 3;
----------------
nickdesaulniers wrote:
```sh
$ cat foo.c
#include <search.h>
int compar(const void *a, const void *b) {
return *(int*)a != *(int*)b;
}
int main () {
int list [4] = {1, 2, 3, 4};
size_t len = 4;
int key = 5;
void *ret = lsearch(&key, list, &len, sizeof(int), compar);
return list[3] == 4;
}
$ gcc -O2 foo.c -fsanitize=address
$ ./a.out
$ echo $?
1
```
Did asan miss an OOB write? Or is glibc's `lsearch` doing something more sophisticated?
https://github.com/llvm/llvm-project/pull/116870
More information about the libc-commits
mailing list