Skip to content

bug(stl): fl::aligned_alloc ignores alignment on ARM/AVR/ESP8266 (returns plain malloc) #4817

Closed

Description

@zackees

Found while doing #4792 (part of #4801); flagged by CodeRabbit on #4816.

fl::aligned_alloc(alignment, size) (src/fl/stl/cstdlib.cpp.hpp:22) ignores alignment on FL_IS_AVR, FL_IS_ESP8266, FL_IS_ARM, FL_IS_APOLLO3 and FL_IS_CI13XX, and returns plain malloc (8-byte aligned on ARM). That avoids newlib's posix_memalign link error, but it means any caller that needs more than malloc's alignment silently gets less. Examples are DMA/cache-line buffers and over-aligned types. On Teensy 4 / Cortex-M7, arm_dcache_delete on an under-aligned buffer can discard neighbouring heap data.

Proposed fix

On these targets, over-allocate by alignment - 1 + sizeof(void*), align the returned pointer, and stash the original pointer immediately before it. fl::aligned_free then reads it back. This needs no posix_memalign.

Acceptance

  • Unit test: aligned_alloc(32/64, n) returns aligned pointers and aligned_free releases them (host plus an ARM board build).
  • Audit existing callers that relied on the old behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coretype: bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions