Skip to content

Commit d3f66bd

Browse files
committed
auto merge of #15779 : alexcrichton/rust/no-nul-terminator, r=pcwalton
Apparently the default getFile implementation for a memory buffer in LLVM ends up requiring a null terminator at the end of the file. This isn't true a good bit of the time apparently on OSX. There have been a number of failed nightly/snapshot builds recently with this strange assertion. This modifies the calls to MemoryBuffer::getFile to explicitly not ask for a null terminator.
2 parents c05bb4e + b29d106 commit d3f66bd

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/rustllvm/RustWrapper.cpp

+25-9
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,30 @@ using namespace llvm::object;
3131

3232
static char *LastError;
3333

34+
#if LLVM_VERSION_MINOR >= 5
35+
extern "C" LLVMMemoryBufferRef
36+
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
37+
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(Path,
38+
-1,
39+
false);
40+
if (!buf_or) {
41+
LLVMRustSetLastError(buf_or.getError().message().c_str());
42+
return nullptr;
43+
}
44+
return wrap(buf_or.get().release());
45+
}
46+
#else
3447
extern "C" LLVMMemoryBufferRef
3548
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
36-
LLVMMemoryBufferRef MemBuf = NULL;
37-
char *err = NULL;
38-
LLVMCreateMemoryBufferWithContentsOfFile(Path, &MemBuf, &err);
39-
if (err != NULL) {
40-
LLVMRustSetLastError(err);
49+
OwningPtr<MemoryBuffer> buf;
50+
error_code err = MemoryBuffer::getFile(Path, buf, -1, false);
51+
if (err) {
52+
LLVMRustSetLastError(err.message().c_str());
53+
return NULL;
4154
}
42-
return MemBuf;
55+
return wrap(buf.take());
4356
}
57+
#endif
4458

4559
extern "C" char *LLVMRustGetLastError(void) {
4660
char *ret = LastError;
@@ -658,10 +672,12 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
658672
#if LLVM_VERSION_MINOR >= 5
659673
extern "C" void*
660674
LLVMRustOpenArchive(char *path) {
661-
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path);
675+
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path,
676+
-1,
677+
false);
662678
if (!buf_or) {
663679
LLVMRustSetLastError(buf_or.getError().message().c_str());
664-
return NULL;
680+
return nullptr;
665681
}
666682

667683
std::error_code err;
@@ -676,7 +692,7 @@ LLVMRustOpenArchive(char *path) {
676692
extern "C" void*
677693
LLVMRustOpenArchive(char *path) {
678694
OwningPtr<MemoryBuffer> buf;
679-
error_code err = MemoryBuffer::getFile(path, buf);
695+
error_code err = MemoryBuffer::getFile(path, buf, -1, false);
680696
if (err) {
681697
LLVMRustSetLastError(err.message().c_str());
682698
return NULL;

0 commit comments

Comments
 (0)