[PATCH] D67236: [MC] Fix undefined behavior in MCInstPrinter::formatHex

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 5 13:47:35 PDT 2019


JDevlieghere updated this revision to Diff 218976.
JDevlieghere edited the summary of this revision.
JDevlieghere added a comment.
Herald added a subscriber: mgorny.

- Add test
- Fix description to say INT64_MIN instead of UINT64_MIN


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67236/new/

https://reviews.llvm.org/D67236

Files:
  llvm/lib/MC/MCInstPrinter.cpp
  llvm/unittests/MC/CMakeLists.txt
  llvm/unittests/MC/MCInstPrinter.cpp


Index: llvm/unittests/MC/MCInstPrinter.cpp
===================================================================
--- /dev/null
+++ llvm/unittests/MC/MCInstPrinter.cpp
@@ -0,0 +1,30 @@
+//===- llvm/unittest/MC/MCInstPrinter.cpp ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+TEST(MCInstPrinter, formatHex) {
+  llvm::InitializeAllTargetInfos();
+  llvm::InitializeAllTargetMCs();
+
+  std::string TripleName = "x86_64-apple-darwin18.0.0";
+  std::string ErrorStr;
+
+  const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
+  ASSERT_TRUE(TheTarget);
+
+  std::unique_ptr<MCRegisterInfo> MRI = TheTarget->createMCRegInfo();
+  ASSERT_TRUE(MRI);
+
+  std::unique_ptr<MCAsmInfo> MAI = TheTarget->createMCAsmInfo(*MRI, TripleName);
+  ASSERT_TRUE(MAI);
+
+  std::unique_ptr<const MCInstrInfo> MII = TheTarget->createMCInstrInfo();
+  ASSERT_TRUE(MII);
+
+  std::unique_ptr<MCInstPrinter> Printer = ObjectTarget->createMCInstPrinter(
+      TripleName, MAI->getAssemblerDialect(), *MAI, *MII, *MRI);
+}
Index: llvm/unittests/MC/CMakeLists.txt
===================================================================
--- llvm/unittests/MC/CMakeLists.txt
+++ llvm/unittests/MC/CMakeLists.txt
@@ -8,6 +8,7 @@
 add_llvm_unittest(MCTests
   Disassembler.cpp
   DwarfLineTables.cpp
+  MCInstPrinter.cpp
   StringTableBuilderTest.cpp
   TargetRegistry.cpp
   )
Index: llvm/lib/MC/MCInstPrinter.cpp
===================================================================
--- llvm/lib/MC/MCInstPrinter.cpp
+++ llvm/lib/MC/MCInstPrinter.cpp
@@ -83,15 +83,19 @@
 }
 
 format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
-  switch(PrintHexStyle) {
+  switch (PrintHexStyle) {
   case HexStyle::C:
-    if (Value < 0)
+    if (Value == std::numeric_limits<int64_t>::min())
+      return format("0x%" PRIx64, Value);
+    else if (Value < 0)
       return format("-0x%" PRIx64, -Value);
     else
       return format("0x%" PRIx64, Value);
   case HexStyle::Asm:
-    if (Value < 0) {
-      if (needsLeadingZero((uint64_t)(-Value)))
+    if (Value == std::numeric_limits<int64_t>::min())
+      return format("%" PRIx64 "h", Value);
+    else if (Value < 0) {
+      if (needsLeadingZero(-(uint64_t)(Value)))
         return format("-0%" PRIx64 "h", -Value);
       else
         return format("-%" PRIx64 "h", -Value);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67236.218976.patch
Type: text/x-patch
Size: 2622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190905/519d8a45/attachment.bin>


More information about the llvm-commits mailing list