[PATCH] D66224: MemoryBuffer: Add a missing error-check to getOpenFileImpl
Pavel Labath via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 15 01:19:31 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL368977: MemoryBuffer: Add a missing error-check to getOpenFileImpl (authored by labath, committed by ).
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D66224/new/
https://reviews.llvm.org/D66224
Files:
llvm/trunk/lib/Support/MemoryBuffer.cpp
llvm/trunk/unittests/Support/MemoryBufferTest.cpp
Index: llvm/trunk/lib/Support/MemoryBuffer.cpp
===================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp
@@ -458,7 +458,9 @@
return make_error_code(errc::not_enough_memory);
}
- sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset);
+ if (std::error_code EC =
+ sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset))
+ return EC;
return std::move(Buf);
}
Index: llvm/trunk/unittests/Support/MemoryBufferTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/MemoryBufferTest.cpp
+++ llvm/trunk/unittests/Support/MemoryBufferTest.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/raw_ostream.h"
@@ -19,6 +20,25 @@
using namespace llvm;
+#define ASSERT_NO_ERROR(x) \
+ if (std::error_code ASSERT_NO_ERROR_ec = x) { \
+ SmallString<128> MessageStorage; \
+ raw_svector_ostream Message(MessageStorage); \
+ Message << #x ": did not return errc::success.\n" \
+ << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
+ << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
+ GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
+ } else { \
+ }
+
+#define ASSERT_ERROR(x) \
+ if (!x) { \
+ SmallString<128> MessageStorage; \
+ raw_svector_ostream Message(MessageStorage); \
+ Message << #x ": did not return a failure error code.\n"; \
+ GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
+ }
+
namespace {
class MemoryBufferTest : public testing::Test {
@@ -65,6 +85,37 @@
EXPECT_EQ("this is some data", data);
}
+TEST_F(MemoryBufferTest, getOpenFile) {
+ int FD;
+ SmallString<64> TestPath;
+ ASSERT_EQ(sys::fs::createTemporaryFile("MemoryBufferTest_getOpenFile", "temp",
+ FD, TestPath),
+ std::error_code());
+
+ FileRemover Cleanup(TestPath);
+ raw_fd_ostream OF(FD, /*shouldClose*/ true);
+ OF << "12345678";
+ OF.close();
+
+ {
+ Expected<sys::fs::file_t> File = sys::fs::openNativeFileForRead(TestPath);
+ ASSERT_THAT_EXPECTED(File, Succeeded());
+ auto OnExit =
+ make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
+ ErrorOr<OwningBuffer> MB = MemoryBuffer::getOpenFile(*File, TestPath, 6);
+ ASSERT_NO_ERROR(MB.getError());
+ EXPECT_EQ("123456", MB.get()->getBuffer());
+ }
+ {
+ Expected<sys::fs::file_t> File = sys::fs::openNativeFileForWrite(
+ TestPath, sys::fs::CD_OpenExisting, sys::fs::OF_None);
+ ASSERT_THAT_EXPECTED(File, Succeeded());
+ auto OnExit =
+ make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
+ ASSERT_ERROR(MemoryBuffer::getOpenFile(*File, TestPath, 6).getError());
+ }
+}
+
TEST_F(MemoryBufferTest, NullTerminator4K) {
// Test that a file with size that is a multiple of the page size can be null
// terminated correctly by MemoryBuffer.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66224.215339.patch
Type: text/x-patch
Size: 3754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190815/25f68954/attachment.bin>
More information about the llvm-commits
mailing list