[llvm] [PDB][NativeSession] Use better error code for invalid format (PR #167885)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 22 08:07:03 PST 2025
https://github.com/MuellerMP updated https://github.com/llvm/llvm-project/pull/167885
>From 81d4e190bb89f7cb27b11cc4f039a2b564916d3b Mon Sep 17 00:00:00 2001
From: MuellerMP <mirkomueller97 at live.de>
Date: Thu, 13 Nov 2025 15:47:54 +0100
Subject: [PATCH] [PDB][NativeSession] Use better error code for invalid format
Replaces the default "Success" std::error_code with a more meaningful one.
---
.../DebugInfo/PDB/Native/NativeSession.cpp | 25 +++++++++++++------
.../DebugInfo/PDB/NativeSessionTest.cpp | 12 +++++++++
2 files changed, 29 insertions(+), 8 deletions(-)
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