Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 4cf866c

Browse files
committed
MemoryBuffer: Add a missing error-check to getOpenFileImpl
Summary: 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@368977 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f7118a4 commit 4cf866c

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/Support/MemoryBuffer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
458458
return make_error_code(errc::not_enough_memory);
459459
}
460460

461-
sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset);
461+
if (std::error_code EC =
462+
sys::fs::readNativeFileSlice(FD, Buf->getBuffer(), Offset))
463+
return EC;
462464

463465
return std::move(Buf);
464466
}

unittests/Support/MemoryBufferTest.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Support/MemoryBuffer.h"
14+
#include "llvm/ADT/ScopeExit.h"
1415
#include "llvm/Support/FileSystem.h"
1516
#include "llvm/Support/FileUtilities.h"
1617
#include "llvm/Support/raw_ostream.h"
@@ -19,6 +20,25 @@
1920

2021
using namespace llvm;
2122

23+
#define ASSERT_NO_ERROR(x) \
24+
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
25+
SmallString<128> MessageStorage; \
26+
raw_svector_ostream Message(MessageStorage); \
27+
Message << #x ": did not return errc::success.\n" \
28+
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
29+
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
30+
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
31+
} else { \
32+
}
33+
34+
#define ASSERT_ERROR(x) \
35+
if (!x) { \
36+
SmallString<128> MessageStorage; \
37+
raw_svector_ostream Message(MessageStorage); \
38+
Message << #x ": did not return a failure error code.\n"; \
39+
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
40+
}
41+
2242
namespace {
2343

2444
class MemoryBufferTest : public testing::Test {
@@ -65,6 +85,37 @@ TEST_F(MemoryBufferTest, get) {
6585
EXPECT_EQ("this is some data", data);
6686
}
6787

88+
TEST_F(MemoryBufferTest, getOpenFile) {
89+
int FD;
90+
SmallString<64> TestPath;
91+
ASSERT_EQ(sys::fs::createTemporaryFile("MemoryBufferTest_getOpenFile", "temp",
92+
FD, TestPath),
93+
std::error_code());
94+
95+
FileRemover Cleanup(TestPath);
96+
raw_fd_ostream OF(FD, /*shouldClose*/ true);
97+
OF << "12345678";
98+
OF.close();
99+
100+
{
101+
Expected<sys::fs::file_t> File = sys::fs::openNativeFileForRead(TestPath);
102+
ASSERT_THAT_EXPECTED(File, Succeeded());
103+
auto OnExit =
104+
make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
105+
ErrorOr<OwningBuffer> MB = MemoryBuffer::getOpenFile(*File, TestPath, 6);
106+
ASSERT_NO_ERROR(MB.getError());
107+
EXPECT_EQ("123456", MB.get()->getBuffer());
108+
}
109+
{
110+
Expected<sys::fs::file_t> File = sys::fs::openNativeFileForWrite(
111+
TestPath, sys::fs::CD_OpenExisting, sys::fs::OF_None);
112+
ASSERT_THAT_EXPECTED(File, Succeeded());
113+
auto OnExit =
114+
make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); });
115+
ASSERT_ERROR(MemoryBuffer::getOpenFile(*File, TestPath, 6).getError());
116+
}
117+
}
118+
68119
TEST_F(MemoryBufferTest, NullTerminator4K) {
69120
// Test that a file with size that is a multiple of the page size can be null
70121
// terminated correctly by MemoryBuffer.

0 commit comments

Comments
 (0)