Skip to content

Commit 63b428e

Browse files
DWARFDebugLine.cpp: Format unknown line number standard opcodes
Summary: This patch implements `formatv()` formatting for `dwarf::LineNumberOps` and makes use of it for the `llvm-dwarfdump --debug-line` dump. Previously, unknown line number standard opcodes would lead to undefined behaviour. The code would attempt to format the data pointer of an empty `StringRef` (a null pointer) using `%s`. According to the description for `format()`, use of that interface carries the "risk of `printf`". Passing a null pointer in place of an array to a C library function results in undefined behaviour. Reviewers: jhenderson, daltenty, stevewan Reviewed By: jhenderson Subscribers: aprantl, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72369
1 parent e429f24 commit 63b428e

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ template <> struct EnumTraits<Tag> : public std::true_type {
654654
static constexpr char Type[4] = "TAG";
655655
static constexpr StringRef (*StringFn)(unsigned) = &TagString;
656656
};
657+
658+
template <> struct EnumTraits<LineNumberOps> : public std::true_type {
659+
static constexpr char Type[4] = "LNS";
660+
static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
661+
};
657662
} // End of namespace dwarf
658663

659664
/// Dwarf constants format_provider

llvm/lib/BinaryFormat/Dwarf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,4 @@ constexpr char llvm::dwarf::EnumTraits<Attribute>::Type[];
757757
constexpr char llvm::dwarf::EnumTraits<Form>::Type[];
758758
constexpr char llvm::dwarf::EnumTraits<Index>::Type[];
759759
constexpr char llvm::dwarf::EnumTraits<Tag>::Type[];
760+
constexpr char llvm::dwarf::EnumTraits<LineNumberOps>::Type[];

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
1717
#include "llvm/Support/Errc.h"
1818
#include "llvm/Support/Format.h"
19+
#include "llvm/Support/FormatVariadic.h"
1920
#include "llvm/Support/WithColor.h"
2021
#include "llvm/Support/raw_ostream.h"
2122
#include <algorithm>
@@ -114,8 +115,9 @@ void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
114115
<< format(" opcode_base: %u\n", OpcodeBase);
115116

116117
for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
117-
OS << format("standard_opcode_lengths[%s] = %u\n",
118-
LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
118+
OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
119+
static_cast<dwarf::LineNumberOps>(I + 1),
120+
StandardOpcodeLengths[I]);
119121

120122
if (!IncludeDirectories.empty()) {
121123
// DWARF v5 starts directory indexes at 0.

llvm/test/tools/llvm-dwarfdump/X86/debug-line.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_prologue_end] = 0
3131
# CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_epilogue_begin] = 0
3232
# CHECK-NEXT: standard_opcode_lengths[DW_LNS_set_isa] = 1
33-
# CHECK-NEXT: standard_opcode_lengths[(null)] = 0
33+
# CHECK-NEXT: standard_opcode_lengths[DW_LNS_unknown_d] = 0
3434
# CHECK-NEXT: include_directories[ 0] = "dir1/dir2"
3535
# CHECK-NEXT: file_names[ 0]:
3636
# CHECK-NEXT: name: "file1.c"

0 commit comments

Comments
 (0)