llvm.org GIT mirror llvm / 01410fc
Recommit "MemoryBuffer: Add a missing error-check to getOpenFileImpl" This recommits r368977, which was reverted in r369027 due to test failures in lldb. The cause of this was different behavior of readNativeFileSlice on windows and unix. These have been addressed in r369269. The original commit message was: In case the function was called with a desired read size *and* the file was not an "mmap()" candidate, the function was falling back to a "pread()", but it was failing to check the result of that system call. This meant that the function would return "success" even though the read operation failed, and it returned a buffer full of uninitialized memory. Reviewers: rnk, dblaikie Subscribers: kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D66224 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369370 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath 29 days ago
2 changed file(s) with 54 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
457457 return make_error_code(errc::not_enough_memory);
458458 }
459459
460 sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset);
460 if (std::error_code EC =
461 sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset))
462 return EC;
461463
462464 return std::move(Buf);
463465 }
1010 //===----------------------------------------------------------------------===//
1111
1212 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/ADT/ScopeExit.h"
1314 #include "llvm/Support/FileSystem.h"
1415 #include "llvm/Support/FileUtilities.h"
1516 #include "llvm/Support/raw_ostream.h"
1718 #include "gtest/gtest.h"
1819
1920 using namespace llvm;
21
22 #define ASSERT_NO_ERROR(x) \
23 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
24 SmallString<128> MessageStorage; \
25 raw_svector_ostream Message(MessageStorage); \
26 Message << #x ": did not return errc::success.\n" \
27 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
28 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
29 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
30 } else { \
31 }
32
33 #define ASSERT_ERROR(x) \
34 if (!x) { \
35 SmallString<128> MessageStorage; \
36 raw_svector_ostream Message(MessageStorage); \
37 Message << #x ": did not return a failure error code.\n"; \
38 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
39 }
2040
2141 namespace {
2242
6282 MB2.reset();
6383 MB3.reset();
6484 EXPECT_EQ("this is some data", data);
85 }
86
87 TEST_F(MemoryBufferTest, getOpenFile) {
88 int FD;
89 SmallString<64> TestPath;
90 ASSERT_EQ(sys::fs::createTemporaryFile("MemoryBufferTest_getOpenFile", "temp",
91 FD, TestPath),
92 std::error_code());
93
94 FileRemover Cleanup(TestPath);
95 raw_fd_ostream OF(FD, /*shouldClose*/ true);
96 OF << "12345678";
97 OF.close();
98
99 {
100 Expected File = sys::fs::openNativeFileForRead(TestPath);
101 ASSERT_THAT_EXPECTED(File, Succeeded());
102 auto OnExit =
103 make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
104 ErrorOr MB = MemoryBuffer::getOpenFile(*File, TestPath, 6);
105 ASSERT_NO_ERROR(MB.getError());
106 EXPECT_EQ("123456", MB.get()->getBuffer());
107 }
108 {
109 Expected File = sys::fs::openNativeFileForWrite(
110 TestPath, sys::fs::CD_OpenExisting, sys::fs::OF_None);
111 ASSERT_THAT_EXPECTED(File, Succeeded());
112 auto OnExit =
113 make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
114 ASSERT_ERROR(MemoryBuffer::getOpenFile(*File, TestPath, 6).getError());
115 }
65116 }
66117
67118 TEST_F(MemoryBufferTest, NullTerminator4K) {