[llvm] 67ce114 - [llvm-dwarfdump] Warn user when it encounters no null terminated strings.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 2 17:50:52 PDT 2020


Author: Xing GUO
Date: 2020-09-03T08:49:57+08:00
New Revision: 67ce11405b08609afb35e218ed7d28ef5e16a55f

URL: https://github.com/llvm/llvm-project/commit/67ce11405b08609afb35e218ed7d28ef5e16a55f
DIFF: https://github.com/llvm/llvm-project/commit/67ce11405b08609afb35e218ed7d28ef5e16a55f.diff

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

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
```

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D86998

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
index 88f118bb05e3..d31c35879821 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -530,7 +530,13 @@ void DWARFContext::dump(
     DataExtractor StrData(Section, isLittleEndian(), 0);
     uint64_t Offset = 0;
     uint64_t StrOffset = 0;
-    while (const char *CStr = StrData.getCStr(&Offset)) {
+    while (StrData.isValidOffset(Offset)) {
+      Error Err = Error::success();
+      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";

diff  --git a/llvm/test/tools/llvm-dwarfdump/debug-str.yaml b/llvm/test/tools/llvm-dwarfdump/debug-str.yaml
index 36729c182310..0f8cf2f19902 100644
--- a/llvm/test/tools/llvm-dwarfdump/debug-str.yaml
+++ b/llvm/test/tools/llvm-dwarfdump/debug-str.yaml
@@ -44,3 +44,16 @@ Sections:
 #  ESCAPED-NEXT: 0x00000002: "\001"
 #  ESCAPED-NEXT: 0x00000004: "\\001"
 # ESCAPED-EMPTY:
+
+## c) Test that llvm-dwarfdump emits a warning when it encounters a string without a null terminator.
+
+##                        "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


        


More information about the llvm-commits mailing list