Skip to content

Commit 29cfef1

Browse files
authored
[PDB][NativeSession] Use better error code for invalid format (#167885)
Replaces the default "Success" std::error_code with a more meaningful one if `Magic != file_magic::pdb`.
1 parent f4ba8e3 commit 29cfef1

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

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

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

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

99112
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);
113+
if (auto EC = validatePdbMagic(PdbPath))
114+
return std::move(EC);
104115

105116
auto Stream = std::make_unique<MemoryBufferByteStream>(
106117
std::move(Buffer), llvm::endianness::little);
@@ -152,10 +163,8 @@ Error NativeSession::createFromExe(StringRef ExePath,
152163
if (!PdbPath)
153164
return PdbPath.takeError();
154165

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);
166+
if (auto EC = validatePdbMagic(PdbPath.get()))
167+
return EC;
159168

160169
auto Allocator = std::make_unique<BumpPtrAllocator>();
161170
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)