Skip to content

Commit c4566d9

Browse files
committed
[PDB][NativeSession] Use better error code for invalid format
Replaces the default "Success" std::error_code with a more meaningful one.
1 parent 35ffe10 commit c4566d9

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
8787
return Error::success();
8888
}
8989

90+
Error validatePdbMagic(StringRef PdbPath) {
91+
file_magic Magic;
92+
auto EC = identify_magic(PdbPath, Magic);
93+
if (EC)
94+
return make_error<RawError>(EC);
95+
96+
if (Magic != file_magic::pdb)
97+
return make_error<RawError>(
98+
raw_error_code::invalid_format,
99+
"The input file did not contain the pdb file magic.");
100+
101+
return Error::success();
102+
}
103+
90104
static Expected<std::unique_ptr<PDBFile>>
91105
loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
92106
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
@@ -97,10 +111,8 @@ loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
97111
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
98112

99113
PdbPath = Buffer->getBufferIdentifier();
100-
file_magic Magic;
101-
auto EC = identify_magic(PdbPath, Magic);
102-
if (EC || Magic != file_magic::pdb)
103-
return make_error<RawError>(EC);
114+
if (auto EC = validatePdbMagic(PdbPath))
115+
return std::move(EC);
104116

105117
auto Stream = std::make_unique<MemoryBufferByteStream>(
106118
std::move(Buffer), llvm::endianness::little);
@@ -152,10 +164,8 @@ Error NativeSession::createFromExe(StringRef ExePath,
152164
if (!PdbPath)
153165
return PdbPath.takeError();
154166

155-
file_magic Magic;
156-
auto EC = identify_magic(PdbPath.get(), Magic);
157-
if (EC || Magic != file_magic::pdb)
158-
return make_error<RawError>(EC);
167+
if (auto EC = validatePdbMagic(PdbPath.get()))
168+
return EC;
159169

160170
auto Allocator = std::make_unique<BumpPtrAllocator>();
161171
auto File = loadPdbFile(PdbPath.get(), Allocator);

llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ TEST(NativeSessionTest, TestCreateFromExe) {
4040
ASSERT_THAT_ERROR(std::move(E), Succeeded());
4141
}
4242

43+
TEST(NativeSessionTest, TestInvalidPdbMagicError) {
44+
SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
45+
llvm::sys::path::append(InputsDir, "SimpleTest.cpp");
46+
std::string CppPath{InputsDir};
47+
std::unique_ptr<IPDBSession> S;
48+
49+
Error E = NativeSession::createFromPdbPath(CppPath, S);
50+
const char *FormatErr = "The record is in an unexpected format. "
51+
"The input file did not contain the pdb file magic.";
52+
ASSERT_THAT_ERROR(std::move(E), FailedWithMessage(FormatErr));
53+
}
54+
4355
TEST(NativeSessionTest, TestSetLoadAddress) {
4456
std::unique_ptr<IPDBSession> S;
4557
Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);

0 commit comments

Comments
 (0)