From: Pavel Labath Date: Fri, 8 Jan 2021 19:37:09 +0000 (+0100) Subject: [lldb] Fix some bugs in the Pipe class and add tests X-Git-Tag: llvmorg-13-init~1590 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=13dea030b3d794d05a08dd0080c35844c9ca1b30;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/lldb/source/Host/posix/PipePosix.cpp b/lldb/source/Host/posix/PipePosix.cpp index 780222d..7cd05a1 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 663645c..1cc0cb0 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 0000000..e8d2c49 --- /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 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()); +}