[llvm] 29cfef1 - [PDB][NativeSession] Use better error code for invalid format (#167885)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 24 07:53:14 PST 2025
Author: Mirko
Date: 2025-11-24T10:53:09-05:00
New Revision: 29cfef188088cb0101b3ec70b13d68c06a2d49d6
URL: https://github.com/llvm/llvm-project/commit/29cfef188088cb0101b3ec70b13d68c06a2d49d6
DIFF: https://github.com/llvm/llvm-project/commit/29cfef188088cb0101b3ec70b13d68c06a2d49d6.diff
LOG: [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`.
Added:
Modified:
llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp
Removed:
################################################################################
diff --git a/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp b/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
index 8967a2eb1749e..49674b4c32de0 100644
--- a/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
+++ b/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
@@ -87,6 +87,19 @@ Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
return Error::success();
}
+static Error validatePdbMagic(StringRef PdbPath) {
+ file_magic Magic;
+ if (auto EC = identify_magic(PdbPath, Magic))
+ return make_error<RawError>(EC);
+
+ if (Magic != file_magic::pdb)
+ return make_error<RawError>(
+ raw_error_code::invalid_format,
+ "The input file did not contain the pdb file magic.");
+
+ return Error::success();
+}
+
static Expected<std::unique_ptr<PDBFile>>
loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
@@ -97,10 +110,8 @@ loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
PdbPath = Buffer->getBufferIdentifier();
- file_magic Magic;
- auto EC = identify_magic(PdbPath, Magic);
- if (EC || Magic != file_magic::pdb)
- return make_error<RawError>(EC);
+ if (auto EC = validatePdbMagic(PdbPath))
+ return std::move(EC);
auto Stream = std::make_unique<MemoryBufferByteStream>(
std::move(Buffer), llvm::endianness::little);
@@ -152,10 +163,8 @@ Error NativeSession::createFromExe(StringRef ExePath,
if (!PdbPath)
return PdbPath.takeError();
- file_magic Magic;
- auto EC = identify_magic(PdbPath.get(), Magic);
- if (EC || Magic != file_magic::pdb)
- return make_error<RawError>(EC);
+ if (auto EC = validatePdbMagic(PdbPath.get()))
+ return EC;
auto Allocator = std::make_unique<BumpPtrAllocator>();
auto File = loadPdbFile(PdbPath.get(), Allocator);
diff --git a/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp b/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp
index cffaf7c9543fb..20ae253513f05 100644
--- a/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp
+++ b/llvm/unittests/DebugInfo/PDB/NativeSessionTest.cpp
@@ -40,6 +40,18 @@ TEST(NativeSessionTest, TestCreateFromExe) {
ASSERT_THAT_ERROR(std::move(E), Succeeded());
}
+TEST(NativeSessionTest, TestInvalidPdbMagicError) {
+ SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
+ llvm::sys::path::append(InputsDir, "SimpleTest.cpp");
+ std::string CppPath{InputsDir};
+ std::unique_ptr<IPDBSession> S;
+
+ Error E = NativeSession::createFromPdbPath(CppPath, S);
+ const char *FormatErr = "The record is in an unexpected format. "
+ "The input file did not contain the pdb file magic.";
+ ASSERT_THAT_ERROR(std::move(E), FailedWithMessage(FormatErr));
+}
+
TEST(NativeSessionTest, TestSetLoadAddress) {
std::unique_ptr<IPDBSession> S;
Error E = pdb::loadDataForEXE(PDB_ReaderType::Native, getExePath(), S);
More information about the llvm-commits
mailing list