[PATCH] D72369: DWARFDebugLine.cpp: Format NULL as "(null)" to avoid UB

Hubert Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 9 06:56:19 PST 2020


hubert.reinterpretcast updated this revision to Diff 237072.
hubert.reinterpretcast added a comment.

DWARFDebugLine.cpp: Format unknown line number standard opcodes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D72369

Files:
  llvm/include/llvm/BinaryFormat/Dwarf.h
  llvm/lib/BinaryFormat/Dwarf.cpp
  llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
  llvm/test/tools/llvm-dwarfdump/X86/debug-line.s


Index: llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
===================================================================
--- llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
+++ llvm/test/tools/llvm-dwarfdump/X86/debug-line.s
@@ -30,7 +30,7 @@
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
 # CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_isa] = 1
-# CHECK-NEXT: standard_opcode_lengths[(null)] = 0
+# CHECK-NEXT: standard_opcode_lengths[DW_LNS_unknown_d] = 0
 # CHECK-NEXT: include_directories[  0] = "dir1/dir2"
 # CHECK-NEXT: file_names[  0]:
 # CHECK-NEXT:            name: "file1.c"
Index: llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
@@ -16,6 +16,7 @@
 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -114,8 +115,9 @@
      << format("     opcode_base: %u\n", OpcodeBase);
 
   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
-    OS << format("standard_opcode_lengths[%s] = %u\n",
-                 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
+    OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
+                  static_cast<dwarf::LineNumberOps>(I + 1),
+                  StandardOpcodeLengths[I]);
 
   if (!IncludeDirectories.empty()) {
     // DWARF v5 starts directory indexes at 0.
Index: llvm/lib/BinaryFormat/Dwarf.cpp
===================================================================
--- llvm/lib/BinaryFormat/Dwarf.cpp
+++ llvm/lib/BinaryFormat/Dwarf.cpp
@@ -757,3 +757,4 @@
 constexpr char llvm::dwarf::EnumTraits<Form>::Type[];
 constexpr char llvm::dwarf::EnumTraits<Index>::Type[];
 constexpr char llvm::dwarf::EnumTraits<Tag>::Type[];
+constexpr char llvm::dwarf::EnumTraits<LineNumberOps>::Type[];
Index: llvm/include/llvm/BinaryFormat/Dwarf.h
===================================================================
--- llvm/include/llvm/BinaryFormat/Dwarf.h
+++ llvm/include/llvm/BinaryFormat/Dwarf.h
@@ -654,6 +654,11 @@
   static constexpr char Type[4] = "TAG";
   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
 };
+
+template <> struct EnumTraits<LineNumberOps> : public std::true_type {
+  static constexpr char Type[4] = "LNS";
+  static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
+};
 } // End of namespace dwarf
 
 /// Dwarf constants format_provider


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72369.237072.patch
Type: text/x-patch
Size: 2720 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200109/1436ff4b/attachment.bin>


More information about the llvm-commits mailing list