Skip to content

Conversation

@MuellerMP
Copy link
Contributor

Replaces the default "Success" std::error_code with a more meaningful one if Magic != file_magic::pdb.

@llvmbot
Copy link
Member

llvmbot commented Nov 13, 2025

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-platform-windows

Author: Mirko (MuellerMP)

Changes

Replaces the default "Success" std::error_code with a more meaningful one if Magic != file_magic::pdb.


Full diff: https://github.com/llvm/llvm-project/pull/167885.diff

1 Files Affected:

  • (modified) llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp (+8-2)
diff --git a/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp b/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
index 8967a2eb1749e..982153beb32b4 100644
--- a/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
+++ b/llvm/lib/DebugInfo/PDB/Native/NativeSession.cpp
@@ -99,9 +99,12 @@ loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
   PdbPath = Buffer->getBufferIdentifier();
   file_magic Magic;
   auto EC = identify_magic(PdbPath, Magic);
-  if (EC || Magic != file_magic::pdb)
+  if (EC)
     return make_error<RawError>(EC);
 
+  if (Magic != file_magic::pdb)
+    return make_error<RawError>(raw_error_code::invalid_format);
+
   auto Stream = std::make_unique<MemoryBufferByteStream>(
       std::move(Buffer), llvm::endianness::little);
 
@@ -154,9 +157,12 @@ Error NativeSession::createFromExe(StringRef ExePath,
 
   file_magic Magic;
   auto EC = identify_magic(PdbPath.get(), Magic);
-  if (EC || Magic != file_magic::pdb)
+  if (EC)
     return make_error<RawError>(EC);
 
+  if (Magic != file_magic::pdb)
+    return make_error<RawError>(raw_error_code::invalid_format);
+
   auto Allocator = std::make_unique<BumpPtrAllocator>();
   auto File = loadPdbFile(PdbPath.get(), Allocator);
   if (!File)

@MuellerMP
Copy link
Contributor Author

MuellerMP commented Nov 13, 2025

This fixes the case where identify_magic(PdbPath, Magic) can identify a Magic, but its not the PDB one (e.g. input is a ascii text file).
Can also add a unit test where i input the SimpleTest.cpp and assert that the message is not the same as a default constructed std::error_code if required.

@aganea
Copy link
Member

aganea commented Nov 13, 2025

Can also add a unit test where i input the SimpleTest.cpp and assert that the message is not the same as a default constructed std::error_code if required.

Yes, please add a test.

@MuellerMP MuellerMP force-pushed the nativesession_better_errorcode branch from 08eae4d to c9790ff Compare November 14, 2025 08:30
@MuellerMP
Copy link
Contributor Author

Can also add a unit test where i input the SimpleTest.cpp and assert that the message is not the same as a default constructed std::error_code if required.

Yes, please add a test.

done -> also improved the error message and reduced the original code duplication a bit

@MuellerMP MuellerMP force-pushed the nativesession_better_errorcode branch 2 times, most recently from c4566d9 to 24debcb Compare November 14, 2025 08:47
@MuellerMP
Copy link
Contributor Author

Hey @aganea - would you perhaps have time to review this?

Copy link
Member

@aganea aganea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a nit:


static Error validatePdbMagic(StringRef PdbPath) {
file_magic Magic;
auto EC = identify_magic(PdbPath, Magic);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write if (auto EC = ... ) to make it shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks - made it shorter

sorry for the amount of commit updates - i messed up my local git config

@MuellerMP MuellerMP force-pushed the nativesession_better_errorcode branch 3 times, most recently from 81d4e19 to a00f0be Compare November 22, 2025 16:10
Replaces the default "Success" std::error_code with a more meaningful one.
@MuellerMP MuellerMP force-pushed the nativesession_better_errorcode branch from a00f0be to 712b6fd Compare November 22, 2025 16:13
@MuellerMP
Copy link
Contributor Author

@aganea thanks for the review!
Would it be possible that you merge this for me since I don't have commit access?

@aganea aganea merged commit 29cfef1 into llvm:main Nov 24, 2025
10 checks passed
@MuellerMP MuellerMP deleted the nativesession_better_errorcode branch November 26, 2025 09:11
aadeshps-mcw pushed a commit to aadeshps-mcw/llvm-project that referenced this pull request Nov 26, 2025
…67885)

Replaces the default "Success" std::error_code with a more meaningful
one if `Magic != file_magic::pdb`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants