]> granicus.if.org Git - llvm/commit
[MC] Fix undefined behavior in MCInstPrinter::formatHex
authorJonas Devlieghere <jonas@devlieghere.com>
Fri, 6 Sep 2019 01:13:32 +0000 (01:13 +0000)
committerJonas Devlieghere <jonas@devlieghere.com>
Fri, 6 Sep 2019 01:13:32 +0000 (01:13 +0000)
commite9377b922768942632b2cbc66e36606456aeff12
tree00c7a1b61b7e9fe7e16ed6bde29d1acd48488715
parent5453d346a5cfb7804f72f383354565fa2b523e48
[MC] Fix undefined behavior in MCInstPrinter::formatHex

Passing INT64_MIN to MCInstPrinter::formatHex triggers undefined
behavior because the negation of -9223372036854775808 cannot be
represented in type 'int64_t' (aka 'long long'). This patch puts a
workaround in place to just print the hex value directly.

A possible alternative involves using a small helper functions that uses
(implementation) defined conversions to achieve the desirable value:

  static int64_t helper(int64_t V) {
    auto U = static_cast<uint64_t>(V);
    return V < 0 ? -U : U;
  }

The underlying problem is that MCInstPrinter::formatHex(int64_t) returns
a format_object<int64_t> and should really return a
format_object<uint64_t>. However, that's not possible because formatImm
needs to be able to print both as decimal (where a signed is required)
and hex (where we'd prefer to always have an unsigned).

  format_object<int64_t> formatImm(int64_t Value) const {
    return PrintImmHex ? formatHex(Value) : formatDec(Value);
  }

Differential revision: https://reviews.llvm.org/D67236

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@371159 91177308-0d34-0410-b5e6-96231b3b80d8
lib/MC/MCInstPrinter.cpp
unittests/MC/CMakeLists.txt
unittests/MC/MCInstPrinter.cpp [new file with mode: 0644]