google / bloaty

Bloaty: a size profiler for binaries
Apache License 2.0
4.71k stars 340 forks source link

CheckAdd integer overflow when comparing arm32 elf files #314

Open PetervdPerk-NXP opened 2 years ago

PetervdPerk-NXP commented 2 years ago

I'm comparing the following files using this command with master bloaty

nxp_fmuk66-v3_default.zip nxp_fmuk66-v3_default_upstream.zip

bloaty -d compileunits nxp_fmuk66-v3_default.elf -- nxp_fmuk66-v3_default_upstream.elf

Which gives me the following runtime error bloaty: integer overflow

I've added a debug print before the overlow and it seems it goes wrong when checking for an overflow with a negative accum (-4, 6). Maybe this is because of an earlier error but it seems that checker function doesn't allow to overflow from negative to postive numbers

CheckedAdd: -4 6
bloaty: integer overflow

A workaround for me would be changing the function into this, but my guess that there's a more fundamental problem

  // Original version
  bool safe = *accum < 0
                  ? (val >= std::numeric_limits<int64_t>::max() - *accum)
                  : (val <= std::numeric_limits<int64_t>::max() - *accum);
  // Version that allows for overflow from negative to positive
  bool safe = *accum < 0
                  ? (val <= std::numeric_limits<int64_t>::max() + *accum)
                  : (val <= std::numeric_limits<int64_t>::max() - *accum);
EgorDuplensky commented 1 year ago

Probably duplicate of https://github.com/google/bloaty/issues/208