I agree, the caller's behavior is UB. I'll send them (freetype2) a patch. That said, do we want to avoid internal UB here anyway? - As mentioned earlier, glibc avoids the UB (and the lock). - llvm-libc does the same starting with https://github.com/llvm/llvm-project/commit/53c251b - uclibc avoids the UB but still locks: https://github.com/gittup/uClibc/blob/9dbf00b/libc/stdio/fread.c#L25 - FreeBSD avoids the UB but still locks: https://svnweb.freebsd.org/base/head/lib/libc/stdio/fread.c?view=markup#l76 - Android (bionic) avoids the UB but still locks: https://cs.android.com/android/platform/superproject/+/master:bionic/libc/stdio/stdio.cpp;l=1099;drc=4aa8f499f21ebf84101de34d68682d5388667001 Does this persuade? On Fri, Feb 24, 2023 at 10:13 AM NRK wrote: > On Fri, Feb 24, 2023 at 09:42:00AM -0500, Tamir Duberstein wrote: > > We could take the lock and still avoid UB with an early return. > > As Jens has pointed out, the UB in this case is the caller calling fread > with NULL - not in musl. > > And on a sidenote, I've always found - especially for the various mem* > functions - accepting 0 size but not accepting NULL arg (when n is 0) to > be a poor choice. A lot of the value that accepting 0 size provides is > diminished by not accepting NULL. > > And this affects more than just libc, too. Compilers like gcc/clang will > see a call like `memcmp(p, q, 0)` and will ""determine"" `p` and `q` are > non-null (which can lead to deleting any subsequent null-checks on those > pointers). > > But anyways, that was just a small rant. > > As things currently are, *even if* musl deal with the NULL pointer - any > caller calling fread with NULL is still in danger from compilers and > needs to fix it on their side. > > - NRK >