News for hackers. Stuff that matters.
Re:Why Does Calloc Exist? by wizeman on Sunday November 13, 2022 @11:38AM (#33584662)
> But less satisfying, it looks like clang optimizes out the entire zero-initialization logic, presumably because it assumes the expression ((size_t *)p)[-1] is undefined behavior.

Yeah, I noticed that is undefined behavior too!

> Makes me wonder what kind of compiler flags muslibc uses to prevent this.

As far as I can see, the only related flags are `-ffreestanding`, `-fno-builtin` and `-fno-strict-aliasing` but I think none of these options prevent the undefined behavior if 1) either the malloc() implementation gets inlined into calloc(), or 2) calloc() gets inlined into the caller, so that the compiler realizes that `p` is not actually an array of `size_t` or compatible integers.

So perhaps musl just avoids undefined behavior by forcing `malloc()` and `calloc()` to not be inlined into the caller? Although I'm not entirely sure that actually completely prevents undefined behavior, I think it would just avoid it assuming how compilers usually/currently work?

Re:Why Does Calloc Exist? by wizeman on Sunday November 13, 2022 @02:19PM (#33586392)
So, I just checked and it seems that the parent pointed to a musl repository that contains old code.

The current code doesn't contain that undefined behavior-triggering expression: https://git.musl-libc.org/cgit/musl/tree/src/malloc/calloc.c