[clang] Remove redundant assertion & fix ClearStatName error (PR #130667)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 13:57:36 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Ayush Pareek (ayushpareek2003)

<details>
<summary>Changes</summary>


Issue: Calling clearStatName() on an error object
The function clearStatName() is called inside constructors before checking whether MaybeStat contains an error. If MaybeStat is an error, calling copyWithNewName() can cause undefined behavior


Issue: Multiple assert condition in function getDirectiveTokens()
Since isError() and isDirectory() imply that Contents is nullptr, the last assertion is unnecessary

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


1 Files Affected:

- (modified) clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h (+3-2) 


``````````diff
diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
index d12814e7c9253..b69a99cf12bcb 100644
--- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
@@ -93,7 +93,7 @@ class CachedFileSystemEntry {
   getDirectiveTokens() const {
     assert(!isError() && "error");
     assert(!isDirectory() && "not a file");
-    assert(Contents && "contents not initialized");
+    // Since isError() and isDirectory() imply that Contents is nullptr, the last assertion is unnecessary
     if (auto *Directives = Contents->DepDirectives.load()) {
       if (Directives->has_value())
         return ArrayRef<dependency_directives_scan::Directive>(**Directives);
@@ -126,7 +126,8 @@ class CachedFileSystemEntry {
 
 private:
   void clearStatName() {
-    if (MaybeStat)
+      
+    if (MaybeStat && MaybeStat->getName().empty())   //If MaybeStat is an error, calling copyWithNewName() can cause undefined behavior
       MaybeStat = llvm::vfs::Status::copyWithNewName(*MaybeStat, "");
   }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/130667


More information about the cfe-commits mailing list