[libc-commits] [libc] [libc] Move from alias(X) to asm(X) for aliasing (PR #89333)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Fri Apr 19 15:14:01 PDT 2024


nickdesaulniers wrote:

```diff
diff --git a/libc/src/stdlib/bsearch.h b/libc/src/stdlib/bsearch.h
index 1de7e051ff6c..cf3188c3adc7 100644
--- a/libc/src/stdlib/bsearch.h
+++ b/libc/src/stdlib/bsearch.h
@@ -9,13 +9,9 @@
 #ifndef LLVM_LIBC_SRC_STDLIB_BSEARCH_H
 #define LLVM_LIBC_SRC_STDLIB_BSEARCH_H
 
-#include <stdlib.h>
+#include <stddef.h> // size_t
```

Is perhaps the more interesting change.  We should do that change regardless, because we only need `stddef.h` for the declaration of `size_t`.  BUT! if we retain it:
```
/android0/llvm-project/libc/src/stdlib/bsearch.cpp: In function ‘void* bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*))’:
/android0/llvm-project/libc/src/stdlib/bsearch.cpp:19:11: error: ‘nonnull’ argument ‘key’ compared to NULL [-Werror=nonnull-compare]
   19 |   if (key == nullptr || array == nullptr || array_size == 0 || elem_size == 0)
      |       ~~~~^~~~~~~~~~
/android0/llvm-project/libc/src/stdlib/bsearch.cpp:19:31: error: ‘nonnull’ argument ‘array’ compared to NULL [-Werror=nonnull-compare]
   19 |   if (key == nullptr || array == nullptr || array_size == 0 || elem_size == 0)
      |                         ~~~~~~^~~~~~~~~~
```

IME there's some very wacky behavior wrt. attributes on redeclarations; there's always this ambiguity between if the redeclarations don't match in terms of attributes, are the attributes retained or not. IME, it's a case by case basis whether that's an error or not, and when not, whether they're retained.

So though we redeclare bsearch without parameter attributes, the declaration in glibc MUST have them on the parameters, so we now get a diagnostic that the comparisons on function parameters (that are guaranteed by the standard to not be nullptr else UB).

So we perhaps should also declare our bsearch to have non-null pointer parameters and remove these checks (maybe, unless we have some hardening mode).

---

Also, I didn't test building the whole tree; even small individual function unit tests require full conversion of the codebase.

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


More information about the libc-commits mailing list