[llvm-branch-commits] [lldb] 13dea03 - [lldb] Fix some bugs in the Pipe class and add tests

Pavel Labath via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Jan 10 13:04:13 PST 2021


Author: Pavel Labath
Date: 2021-01-10T21:59:16+01:00
New Revision: 13dea030b3d794d05a08dd0080c35844c9ca1b30

URL: https://github.com/llvm/llvm-project/commit/13dea030b3d794d05a08dd0080c35844c9ca1b30
DIFF: https://github.com/llvm/llvm-project/commit/13dea030b3d794d05a08dd0080c35844c9ca1b30.diff

LOG: [lldb] Fix some bugs in the Pipe class and add tests

- s/createUniqueFile/createUniquePath -- we don't want to create the file,
  just the file name
- s/data()/str().c_str()/ -- paths given to system apis must be
  null-terminated

Added: 
    lldb/unittests/Host/PipeTest.cpp

Modified: 
    lldb/source/Host/posix/PipePosix.cpp
    lldb/unittests/Host/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/source/Host/posix/PipePosix.cpp b/lldb/source/Host/posix/PipePosix.cpp
index 780222dffbf8..7cd05a1ad2eb 100644
--- a/lldb/source/Host/posix/PipePosix.cpp
+++ b/lldb/source/Host/posix/PipePosix.cpp
@@ -117,7 +117,7 @@ Status PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit) {
     return Status("Pipe is already opened");
 
   Status error;
-  if (::mkfifo(name.data(), 0660) != 0)
+  if (::mkfifo(name.str().c_str(), 0660) != 0)
     error.SetErrorToErrno();
 
   return error;
@@ -138,8 +138,8 @@ Status PipePosix::CreateWithUniqueName(llvm::StringRef prefix,
   // try again.
   Status error;
   do {
-    llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(),
-                                    named_pipe_path);
+    llvm::sys::fs::createUniquePath(tmpdir_file_spec.GetPath(), named_pipe_path,
+                                    /*MakeAbsolute=*/false);
     error = CreateNew(named_pipe_path, child_process_inherit);
   } while (error.GetError() == EEXIST);
 
@@ -158,7 +158,7 @@ Status PipePosix::OpenAsReader(llvm::StringRef name,
     flags |= O_CLOEXEC;
 
   Status error;
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.data(), flags);
+  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.str().c_str(), flags);
   if (fd != -1)
     m_fds[READ] = fd;
   else
@@ -189,7 +189,7 @@ PipePosix::OpenAsWriterWithTimeout(llvm::StringRef name,
     }
 
     errno = 0;
-    int fd = ::open(name.data(), flags);
+    int fd = ::open(name.str().c_str(), flags);
     if (fd == -1) {
       const auto errno_copy = errno;
       // We may get ENXIO if a reader side of the pipe hasn't opened yet.

diff  --git a/lldb/unittests/Host/CMakeLists.txt b/lldb/unittests/Host/CMakeLists.txt
index 663645c986f0..1cc0cb081e49 100644
--- a/lldb/unittests/Host/CMakeLists.txt
+++ b/lldb/unittests/Host/CMakeLists.txt
@@ -7,6 +7,7 @@ set (FILES
   HostTest.cpp
   MainLoopTest.cpp
   NativeProcessProtocolTest.cpp
+  PipeTest.cpp
   ProcessLaunchInfoTest.cpp
   SocketAddressTest.cpp
   SocketTest.cpp

diff  --git a/lldb/unittests/Host/PipeTest.cpp b/lldb/unittests/Host/PipeTest.cpp
new file mode 100644
index 000000000000..e8d2c49c4490
--- /dev/null
+++ b/lldb/unittests/Host/PipeTest.cpp
@@ -0,0 +1,48 @@
+//===-- PipeTest.cpp ------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/Pipe.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+
+class PipeTest : public testing::Test {
+public:
+  SubsystemRAII<FileSystem, HostInfo> subsystems;
+};
+
+TEST_F(PipeTest, CreateWithUniqueName) {
+  Pipe pipe;
+  llvm::SmallString<0> name;
+  ASSERT_THAT_ERROR(pipe.CreateWithUniqueName("PipeTest-CreateWithUniqueName",
+                                              /*child_process_inherit=*/false,
+                                              name)
+                        .ToError(),
+                    llvm::Succeeded());
+}
+
+TEST_F(PipeTest, OpenAsReader) {
+  Pipe pipe;
+  llvm::SmallString<0> name;
+  ASSERT_THAT_ERROR(pipe.CreateWithUniqueName("PipeTest-OpenAsReader",
+                                              /*child_process_inherit=*/false,
+                                              name)
+                        .ToError(),
+                    llvm::Succeeded());
+
+  // Ensure name is not null-terminated
+  size_t name_len = name.size();
+  name += "foobar";
+  llvm::StringRef name_ref(name.data(), name_len);
+  ASSERT_THAT_ERROR(
+      pipe.OpenAsReader(name_ref, /*child_process_inherit=*/false).ToError(),
+      llvm::Succeeded());
+}


        


More information about the llvm-branch-commits mailing list