Skip to content

Minor fixes to posix_memalign()

This MR changes the alignment check in posix_memalign() slightly. The spec states that alignment must be a "power of two multiple of sizeof(void *)", which I interpreted to mean "a-power-of-two times sizeof(void *)" when I originally wrote the code.

Both C11 and Rust's standard library seem to state that valid alignment requirements are always strictly powers of two. posix_memalign() is now slightly stricter to enforce this. There are two further implications to this change:

  • The new check is simpler and therefore, presumably, slightly faster
  • On a hypothetical platform with non-power-of-two pointer size, posix_memalign() will now never succeed (it would previously follow the happy path when called with an alignment of, say, 4*sizeof(void *)).

In addition, the function now also sets memptr to NULL upon errors. The old behaviour of leaving it untouched on error is also permitted by POSIX, but setting it to NULL may prevent some cases of reading from an uninitialized memptr in user code with inadequate error checks.

Merge request reports