[llvm] r184119 - Only delete regular files and directories.

Rafael Espindola rafael.espindola at gmail.com
Mon Jun 17 13:35:51 PDT 2013


Author: rafael
Date: Mon Jun 17 15:35:51 2013
New Revision: 184119

URL: http://llvm.org/viewvc/llvm-project?rev=184119&view=rev
Log:
Only delete regular files and directories.

This ports a missing feature from PathV1.h. I am not sure how to test this
with the regular infrastructure, but an Apple bot should check this when
r183985 is reapplied.

Modified:
    llvm/trunk/lib/Support/Unix/PathV2.inc

Modified: llvm/trunk/lib/Support/Unix/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/PathV2.inc?rev=184119&r1=184118&r2=184119&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Unix/PathV2.inc Mon Jun 17 15:35:51 2013
@@ -236,6 +236,22 @@ error_code remove(const Twine &path, boo
   SmallString<128> path_storage;
   StringRef p = path.toNullTerminatedStringRef(path_storage);
 
+  struct stat buf;
+  if (stat(p.begin(), &buf) != 0) {
+    if (errno != errc::no_such_file_or_directory)
+      return error_code(errno, system_category());
+    existed = false;
+    return error_code::success();
+  }
+
+  // Note: this check catches strange situations. In all cases, LLVM should
+  // only be involved in the creation and deletion of regular files.  This
+  // check ensures that what we're trying to erase is a regular file. It
+  // effectively prevents LLVM from erasing things like /dev/null, any block
+  // special file, or other things that aren't "regular" files.
+  if (!S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode))
+    return make_error_code(errc::operation_not_permitted);
+
   if (::remove(p.begin()) == -1) {
     if (errno != errc::no_such_file_or_directory)
       return error_code(errno, system_category());





More information about the llvm-commits mailing list