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

Ayush Pareek via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 13:56:47 PDT 2025


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


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

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



More information about the cfe-commits mailing list