[PATCH] D86998: [llvm-dwarfdump] Warn user when it encounters no null terminated strings.

Xing GUO via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 19:48:47 PDT 2020


Higuoxing created this revision.
Higuoxing added reviewers: jhenderson, grimar, MaskRay.
Herald added subscribers: llvm-commits, cmtice, hiraditya, aprantl.
Herald added a project: LLVM.
Higuoxing requested review of this revision.

When llvm-dwarfdump encounters no null terminated strings, we should
warn user about it rather than ignore it and print nothing.

Before this patch, when llvm-dwarfdump dumps a .debug_str section whose
content is "abc", it prints:

  .debug_str contents:

After this patch:

  .debug_str contents:
  warning: no null terminated string at offset 0x0


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86998

Files:
  llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
  llvm/test/tools/llvm-dwarfdump/debug-str.yaml


Index: llvm/test/tools/llvm-dwarfdump/debug-str.yaml
===================================================================
--- llvm/test/tools/llvm-dwarfdump/debug-str.yaml
+++ llvm/test/tools/llvm-dwarfdump/debug-str.yaml
@@ -44,3 +44,16 @@
 #  ESCAPED-NEXT: 0x00000002: "\001"
 #  ESCAPED-NEXT: 0x00000004: "\\001"
 # ESCAPED-EMPTY:
+
+## c) Test that llvm-dwarfdump emits a warning when it encounters a no null terminated string.
+
+##                        "abc\0"  "abc"
+# RUN: yaml2obj -DCONTENT="61626300616263" %s -o %t3.o
+# RUN: llvm-dwarfdump --debug-str %t3.o 2>&1 | FileCheck %s --check-prefix=WARN
+
+#      WARN: .debug_str contents:
+# WARN-NEXT: 0x00000000: "abc"
+# WARN-NEXT: warning: no null terminated string at offset 0x4
+#      WARN: .debug_str.dwo contents:
+# WARN-NEXT: 0x00000000: "abc"
+# WARN-NEXT: warning: no null terminated string at offset 0x4
Index: llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -528,9 +528,18 @@
 
   auto DumpStrSection = [&](StringRef Section) {
     DataExtractor StrData(Section, isLittleEndian(), 0);
+
+    Error Err = Error::success();
+    cantFail(std::move(Err));
+
     uint64_t Offset = 0;
     uint64_t StrOffset = 0;
-    while (const char *CStr = StrData.getCStr(&Offset)) {
+    while (StrData.isValidOffset(Offset)) {
+      const char *CStr = StrData.getCStr(&Offset, &Err);
+      if (Err) {
+        DumpOpts.WarningHandler(std::move(Err));
+        return;
+      }
       OS << format("0x%8.8" PRIx64 ": \"", StrOffset);
       OS.write_escaped(CStr);
       OS << "\"\n";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86998.289343.patch
Type: text/x-patch
Size: 1707 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200902/69962108/attachment.bin>


More information about the llvm-commits mailing list