[PATCH] libbacktrace: fix UBSAN issues

Ian Lance Taylor iant@golang.org
Thu Nov 11 19:21:21 GMT 2021


On Thu, Nov 11, 2021 at 7:39 AM Martin Liška <mliska@suse.cz> wrote:
>
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>
> Ready to be installed?
> Thanks,
> Martin
>
> Fix issues mentioned in the PR.
>
>         PR libbacktrace/103167
>
> libbacktrace/ChangeLog:
>
>         * elf.c (elf_uncompress_lzma_block): Cast to unsigned int.
>         (elf_uncompress_lzma): Likewise.
>         * xztest.c (test_samples): memcpy only if v > 0.
>
> Co-Authored-By: Andrew Pinski <apinski@marvell.com>
> ---
>   libbacktrace/elf.c    | 8 ++++----
>   libbacktrace/xztest.c | 2 +-
>   2 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/libbacktrace/elf.c b/libbacktrace/elf.c
> index 79d56146fc6..e69ac41c88b 100644
> --- a/libbacktrace/elf.c
> +++ b/libbacktrace/elf.c
> @@ -3175,7 +3175,7 @@ elf_uncompress_lzma_block (const unsigned char *compressed,
>     stream_crc = (compressed[off]
>                 | (compressed[off + 1] << 8)
>                 | (compressed[off + 2] << 16)
> -               | (compressed[off + 3] << 24));
> +               | ((unsigned)(compressed[off + 3]) << 24));

Thanks, but this kind of thing looks strange and is therefore likely
to break again in the future.  I suggest instead

  stream_crc = ((uint32_t) compressed[off]
                         | ((uint32_t) compressed[off + 1] << 8)
                         | ((uint32_t) compressed[off + 2] << 16)
                         | ((uint32_t) compressed[off + 3] << 24));

Same for the similar cases elsewhere.

Ian


More information about the Gcc-patches mailing list