ianlancetaylor / libbacktrace

A C library that may be linked into a C/C++ program to produce symbolic backtraces
Other
944 stars 220 forks source link

Fix a bug in the zlib decompressor #87

Closed rui314 closed 2 years ago

rui314 commented 2 years ago

libbacktrace occasionally fails to decompress compressed debug info even though the sections contain valid zlib streams. The cause of the issue is an off-by-one error.

If a zlib data block is a plain data (uncompressed data), the next two bytes contain the size of the block. These two bytes value is byte- aligned, so if we read-ahead more than 8 bits, we need to unread it.

So, the correct condition to determine whether or not we need to unread a byte is bits >= 8 and not bits > 8. Due to this error, if the last read bits happened to end at a byte boundary, the next byte would be skipped. That caused the decompression failure.

This bug was originally reported against the mold linker. https://github.com/rui314/mold/issues/402

ianlancetaylor commented 2 years ago

Thanks very much for analyzing and fixing this bug.

The master sources for libbacktrace are in the GCC tree, so I took your patch and committed it there. I've then mirrored it back here.

rui314 commented 2 years ago

FYI, I implemented a workaround to mold so that it doesn't trigger this bug. https://github.com/rui314/mold/commit/ba6347996bf2aecab39188dd0b549960c5ba86d9