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
This commit is contained in:
Hubert Tong 2020-01-15 10:42:27 -05:00
parent e429f24ed8
commit 63b428e386
4 changed files with 11 additions and 3 deletions

View File

@ -654,6 +654,11 @@ template <> struct EnumTraits<Tag> : public std::true_type {
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

View File

@ -757,3 +757,4 @@ constexpr char llvm::dwarf::EnumTraits<Attribute>::Type[];
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[];

View File

@ -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 @@ void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
<< 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.

View File

@ -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"