[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
This commit is contained in:
Xing GUO 2020-09-03 08:49:51 +08:00
parent 2324482383
commit 67ce11405b
2 changed files with 20 additions and 1 deletions

View File

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

View File

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