mackron / dr_libs

Audio decoding libraries for C/C++, each in a single source file.
Other
1.24k stars 205 forks source link

dr_flac: remove unaligned 32 bit loads when parsing vorbis comments #223

Closed G4Vi closed 2 years ago

G4Vi commented 2 years ago

Using an emscripten debug build with -s SAFE_HEAP=1 https://emscripten.org/docs/porting/Debugging.html#compiler-settings it got an alignment fault on https://github.com/mackron/dr_libs/blob/c5e53558165585ae624a7d6c7908e698609093c8/dr_flac.h#L6514 when calling drflac_open_with_metadata.

Looking at the spec for METADATA_BLOCK_VORBIS_COMMENT, it appears there aren't any alignment requirements, so the uint32's may not be 4 byte aligned. This may need to be also be fixed in other metadata blocks.

This PR adds a function to read a little endian 32 bit integer from a potentially unaligned ptr and replaces the uint32 reading for METADATA_BLOCK_VORBIS_COMMENT with it. This fix should work on big and little endian.

Edit: Also added fix to drflac_next_vorbis_comment as I ran into the same issue there. Edit2: Fixed DRFLAC_METADATA_BLOCK_TYPE_PICTURE

mackron commented 2 years ago

Thanks for this. Merged. I just renamed a few functions, but otherwise all good.