Don't allow pwrite to resize a stream.

The current implementations could exhibit some behavior differences:

raw_fd_ostream: Whatever the underlying fd does with seek+write. In a normal
file, the write position would be back to the old offset.

raw_svector_ostream: The write position is always the end of the stream, so
after pwrite the write position would be the new end. This matches what OS_X
(all BSD?) do with a pwrite in a O_APPEND fd.

Given that we don't need that feature and don't use O_APPEND a lot in LLVM,
just disallow it.

I am open to suggestions on renaming pwrite to something else, but this fixes
the issue for now.

Thanks to Yaron Keren for reporting it.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235303 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2015-04-20 13:04:30 +00:00
parent dbef0175c3
commit f30ac8f5ec
3 changed files with 26 additions and 15 deletions

View File

@@ -630,7 +630,8 @@ uint64_t raw_fd_ostream::seek(uint64_t off) {
return pos;
}
void raw_fd_ostream::pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
uint64_t Offset) {
uint64_t Pos = tell();
seek(Offset);
write(Ptr, Size);
@@ -781,14 +782,9 @@ raw_svector_ostream::~raw_svector_ostream() {
flush();
}
void raw_svector_ostream::pwrite(const char *Ptr, size_t Size,
uint64_t Offset) {
void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
uint64_t Offset) {
flush();
uint64_t End = Offset + Size;
if (End > OS.size())
OS.resize(End);
memcpy(OS.begin() + Offset, Ptr, Size);
}
@@ -847,4 +843,5 @@ uint64_t raw_null_ostream::current_pos() const {
return 0;
}
void raw_null_ostream::pwrite(const char *Ptr, size_t Size, uint64_t Offset) {}
void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
uint64_t Offset) {}