diff --git a/.github/setenv.sh b/.github/setenv.sh index 2755afc..c4a70bd 100644 --- a/.github/setenv.sh +++ b/.github/setenv.sh @@ -36,6 +36,7 @@ function c4_show_info() echo "ARM=$ARM" echo "LIBCXX=$LIBCXX" echo "VERBOSE_MAKEFILES=$VERBOSE_MAKEFILES" + set -x which cmake cmake --version case "$CXX_" in @@ -52,7 +53,8 @@ function c4_show_info() *) ;; esac - set -x + pwd + ls git branch git rev-parse HEAD git tag || echo diff --git a/changelog/current.md b/changelog/current.md index 10cdc1f..1e41a2c 100644 --- a/changelog/current.md +++ b/changelog/current.md @@ -1,8 +1,9 @@ -- Fix [rapidyaml#445](https://github.com/biojppm/biojppm/pull/445): Amalgamate: fix include of ``. +- Amalgamate: fix include of `` (see [rapidyaml#445](https://github.com/biojppm/biojppm/pull/445)). - Add `C4_MINGW` ([PR#139](https://github.com/biojppm/c4core/pull/139)) - Annotate `c4::handle_error()` with `[[noreturn]]` ([PR#137](https://github.com/biojppm/c4core/pull/137)) - Add `bool from_chars(csubstr s, fmt::overflow_checked_ *wrapper)`. There was already a function receiving `&wrapper`, but `*wrapper` was missing for use with generic code. +- Ensure `posix_memalign()` is never called with bad alignment values ([PR#138](https://github.com/biojppm/c4core/pull/138)) - Update fast_float to v6.1.1 ([PR#136](https://github.com/biojppm/c4core/pull/136)) diff --git a/src/c4/memory_resource.cpp b/src/c4/memory_resource.cpp index ead3edd..891c373 100644 --- a/src/c4/memory_resource.cpp +++ b/src/c4/memory_resource.cpp @@ -38,37 +38,34 @@ void afree_impl(void *ptr) void* aalloc_impl(size_t size, size_t alignment) { + // alignment must be nonzero and a power of 2 + C4_CHECK(alignment > 0 && (alignment & (alignment - 1u)) == 0); + // NOTE: alignment needs to be sized in multiples of sizeof(void*) + if(C4_UNLIKELY(alignment < sizeof(void*))) + alignment = sizeof(void*); + static_assert((sizeof(void*) & (sizeof(void*)-1u)) == 0, "sizeof(void*) must be a power of 2"); + C4_CHECK(((alignment & (sizeof(void*) - 1u))) == 0u); void *mem; #if defined(C4_WIN) || defined(C4_XBOX) mem = ::_aligned_malloc(size, alignment); C4_CHECK(mem != nullptr || size == 0); -#elif defined(C4_ARM) || defined(C4_ANDROID) - // https://stackoverflow.com/questions/53614538/undefined-reference-to-posix-memalign-in-arm-gcc - // https://electronics.stackexchange.com/questions/467382/e2-studio-undefined-reference-to-posix-memalign/467753 - mem = memalign(alignment, size); - C4_CHECK(mem != nullptr || size == 0); #elif defined(C4_POSIX) || defined(C4_IOS) || defined(C4_MACOS) - // NOTE: alignment needs to be sized in multiples of sizeof(void*) - size_t amult = alignment; - if(C4_UNLIKELY(alignment < sizeof(void*))) - { - amult = sizeof(void*); - } - int ret = ::posix_memalign(&mem, amult, size); + int ret = ::posix_memalign(&mem, alignment, size); if(C4_UNLIKELY(ret)) { - if(ret == EINVAL) - { - C4_ERROR("The alignment argument %zu was not a power of two, " - "or was not a multiple of sizeof(void*)", alignment); - } - else if(ret == ENOMEM) + C4_ASSERT(ret != EINVAL); // this was already handled above + if(ret == ENOMEM) { C4_ERROR("There was insufficient memory to fulfill the " "allocation request of %zu bytes (alignment=%lu)", size, size); } return nullptr; } +#elif defined(C4_ARM) || defined(C4_ANDROID) + // https://stackoverflow.com/questions/53614538/undefined-reference-to-posix-memalign-in-arm-gcc + // https://electronics.stackexchange.com/questions/467382/e2-studio-undefined-reference-to-posix-memalign/467753 + mem = memalign(alignment, size); + C4_CHECK(mem != nullptr || size == 0); #else (void)size; (void)alignment;