[clang] [clang-installapi] ] Fix potential null pointer dereference in file enumeration (PR #97900)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 6 08:55:08 PDT 2024
https://github.com/smanna12 updated https://github.com/llvm/llvm-project/pull/97900
>From dd36ef6dd52f57d175fd60534172f0e28e11ef48 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Sat, 6 Jul 2024 08:29:26 -0700
Subject: [PATCH 1/2] [InstallAPI] Fix potential null pointer dereference in
file enumeration
This patch addresses a static analyser concern about a potential null pointer
dereference in the clang::installapi::enumerateFiles function.
The recursive_directory_iterator could become invalid (i.e., i.State set to nullptr)
when iterating over files.
We now check the validity of the iterator before dereferencing it and handle possible
errors from FS.status. This fix ensures safe iteration over the directory entries and
prevents crashes due to undefined behavior.
---
clang/lib/InstallAPI/HeaderFile.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/clang/lib/InstallAPI/HeaderFile.cpp b/clang/lib/InstallAPI/HeaderFile.cpp
index 0b7041ec8147e..f25bf3428ee13 100644
--- a/clang/lib/InstallAPI/HeaderFile.cpp
+++ b/clang/lib/InstallAPI/HeaderFile.cpp
@@ -51,8 +51,13 @@ llvm::Expected<PathSeq> enumerateFiles(FileManager &FM, StringRef Directory) {
if (EC)
return errorCodeToError(EC);
+ Ensure the iterator is valid before dereferencing.
+ if (i == ie || !i->isValid())
+ break;
+
// Skip files that do not exist. This usually happens for broken symlinks.
- if (FS.status(i->path()) == std::errc::no_such_file_or_directory)
+ auto StatusOrErr = FS.status(i->path());
+ if (!StatusOrErr || StatusOrErr.getError() == std::errc::no_such_file_or_directory)
continue;
StringRef Path = i->path();
>From fe68a4ec84e02df7ecba701bcd7f3508ff8e85f7 Mon Sep 17 00:00:00 2001
From: "Manna, Soumi" <soumi.manna at intel.com>
Date: Sat, 6 Jul 2024 08:54:32 -0700
Subject: [PATCH 2/2] Fix clang format errors
---
clang/lib/InstallAPI/HeaderFile.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/lib/InstallAPI/HeaderFile.cpp b/clang/lib/InstallAPI/HeaderFile.cpp
index f25bf3428ee13..d69b2ada27e9f 100644
--- a/clang/lib/InstallAPI/HeaderFile.cpp
+++ b/clang/lib/InstallAPI/HeaderFile.cpp
@@ -51,13 +51,14 @@ llvm::Expected<PathSeq> enumerateFiles(FileManager &FM, StringRef Directory) {
if (EC)
return errorCodeToError(EC);
- Ensure the iterator is valid before dereferencing.
+ // Ensure the iterator is valid before dereferencing.
if (i == ie || !i->isValid())
break;
// Skip files that do not exist. This usually happens for broken symlinks.
auto StatusOrErr = FS.status(i->path());
- if (!StatusOrErr || StatusOrErr.getError() == std::errc::no_such_file_or_directory)
+ if (!StatusOrErr ||
+ StatusOrErr.getError() == std::errc::no_such_file_or_directory)
continue;
StringRef Path = i->path();
More information about the cfe-commits
mailing list