mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Unaligned memory access in resampler #499

Closed u3shit closed 2 years ago

u3shit commented 2 years ago

After updating miniaudio (after years) to current master (0.11.9), and fixed the api breaks, I noticed that running an ubsan (with clang 8) build creates a bunch of errors about unaligned access (external file, because it's big). miniaudio.log Otherwise it works for me in amd64 debug build, but technically it's an undefined behavior and it might break with optimizations or on CPU architectures that doesn't support unaligned accesses.

After some digging around it looks like at line 49162 (inside ma_linear_resampler_get_heap_layout) before initializing LPF, pHeapLayout->sizeInBytes will be 4, but on 64 bit architectures it should be aligned to 8 bytes. Manually adding a pHeapLayout->sizeInBytes = ma_align_64(pHeapLayout->sizeInBytes); before fixes the problem for me and all ubsan warnings disappear.

mackron commented 2 years ago

Thanks for this, and good find. I think I've fixed that in the dev branch. Are you able to give that try?

u3shit commented 2 years ago

Yup, checked, the ubsan warnings are gone.

mackron commented 2 years ago

Thanks for confirming that for me. I'll get this out soon.