[PATCH] D156798: Check if directory before opening file
Aaron Ballman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 1 07:49:49 PDT 2023
aaron.ballman requested changes to this revision.
aaron.ballman added a comment.
This revision now requires changes to proceed.
Please add comments to the patch description explaining what's changing and why (this helps reviewers as well as during code archeology after the changes land).
================
Comment at: llvm/lib/Support/Unix/Path.inc:17
//===----------------------------------------------------------------------===//
-
+#include <iostream>
#include "Unix.h"
----------------
Please do not include `iostream`: https://llvm.org/docs/CodingStandards.html#include-iostream-is-forbidden
================
Comment at: llvm/lib/Support/Unix/Path.inc:1017-1024
+
+ if (Access == FA_Read) {
+ struct stat Status;
+ if (stat(P.begin(), &Status) == -1)
+ return std::error_code(errno, std::generic_category());
+ if (S_ISDIR(Status.st_mode))
+ return make_error_code(errc::is_a_directory);
----------------
This introduces a TOCTOU issue with the code -- there's now a window where `stat` will return the status of the file but that status can change before the call to `open`.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156798/new/
https://reviews.llvm.org/D156798
More information about the llvm-commits
mailing list