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

Hubert Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 15:23:58 PST 2020


hubert.reinterpretcast created this revision.
hubert.reinterpretcast added reviewers: jhenderson, daltenty, stevewan.
Herald added subscribers: hiraditya, aprantl.
Herald added a project: LLVM.

As `llvm/test/tools/llvm-dwarfdump/X86/debug-line.s` demonstrates, the code in question might attempt to format a null pointer as a string. 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. This patch avoids the undefined behaviour by providing a string that corresponds to that which is expected by the aforementioned test.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72369

Files:
  llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp


Index: llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
@@ -113,9 +113,11 @@
      << format("      line_range: %u\n", LineRange)
      << format("     opcode_base: %u\n", OpcodeBase);
 
+  const auto PrettyNullString = [](const char *S) { return S ? S : "(null)"; };
   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
     OS << format("standard_opcode_lengths[%s] = %u\n",
-                 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
+                 PrettyNullString(LNStandardString(I + 1).data()),
+                 StandardOpcodeLengths[I]);
 
   if (!IncludeDirectories.empty()) {
     // DWARF v5 starts directory indexes at 0.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72369.236702.patch
Type: text/x-patch
Size: 825 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200107/49554128/attachment.bin>


More information about the llvm-commits mailing list