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

Ayush Pareek via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 02:08:01 PDT 2025


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

>From 94b213ad0edf8295451cdb315093cd73923714bb Mon Sep 17 00:00:00 2001
From: Ayush Pareek <AYUSHPAREEK1980 at GMAIL.COM>
Date: Tue, 11 Mar 2025 02:23:49 +0530
Subject: [PATCH 1/2] Update DependencyScanningFilesystem.h

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

Since isError() and isDirectory() imply that Contents is nullptr, the last assertion is unnecessary
---
 .../DependencyScanning/DependencyScanningFilesystem.h        | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

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, "");
   }
 

>From 815393bc35d31e4e9dab6cc7c55b642f806fe34b Mon Sep 17 00:00:00 2001
From: Ayush Pareek <AYUSHPAREEK1980 at GMAIL.COM>
Date: Wed, 12 Mar 2025 14:37:53 +0530
Subject: [PATCH 2/2] Update DependencyScanningFilesystem.h

---
 .../Tooling/DependencyScanning/DependencyScanningFilesystem.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

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



More information about the cfe-commits mailing list