diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2014-08-25 16:22:11 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2014-08-25 16:23:00 -0700 |
commit | e8aa00b76fb9d19714a9d05dc6dd09ae10c5250e (patch) | |
tree | f44003de86b3160de1ab39fcf4c7559b8fa25f7b /src | |
parent | e11badbebd078be3437a06a1fb72a7f01c3afd5c (diff) | |
download | tmwa-e8aa00b76fb9d19714a9d05dc6dd09ae10c5250e.tar.gz tmwa-e8aa00b76fb9d19714a9d05dc6dd09ae10c5250e.tar.bz2 tmwa-e8aa00b76fb9d19714a9d05dc6dd09ae10c5250e.tar.xz tmwa-e8aa00b76fb9d19714a9d05dc6dd09ae10c5250e.zip |
Fix potential file corruption with partial writesv14.4.20
Diffstat (limited to 'src')
-rw-r--r-- | src/io/write.cpp | 1 | ||||
-rw-r--r-- | src/io/write_test.cpp | 35 |
2 files changed, 35 insertions, 1 deletions
diff --git a/src/io/write.cpp b/src/io/write.cpp index 5993a69..a216b03 100644 --- a/src/io/write.cpp +++ b/src/io/write.cpp @@ -89,6 +89,7 @@ namespace io dat += rv; len -= rv; } + std::copy(dat, dat + len, buf); buflen = len; maybe_linebuffered: diff --git a/src/io/write_test.cpp b/src/io/write_test.cpp index ae8eccd..8c08833 100644 --- a/src/io/write_test.cpp +++ b/src/io/write_test.cpp @@ -27,7 +27,7 @@ #include "../strings/mstring.hpp" #include "../strings/xstring.hpp" -#include "../poison.hpp" +//#include "../poison.hpp" static io::FD pipew(io::FD& rfd) @@ -100,3 +100,36 @@ TEST(io, write2) EXPECT_TRUE(wf.close()); EXPECT_EQ("XXX", pw.slurp()); } + +TEST(io, write3) +{ + // TODO see if it's possible to get the real value + constexpr size_t PIPE_CAPACITY = 65536; + char buf[PIPE_CAPACITY]; + + PipeWriter pw(false); + io::WriteFile& wf = pw.wf; + + memset(buf, 'a', sizeof(buf)); + wf.really_put(buf, 1); + EXPECT_EQ("", pw.slurp()); + + memset(buf, 'b', sizeof(buf)); + wf.really_put(buf, sizeof(buf)); + + // write 1 + PIPE_CAPACITY + // read 1 + N + (PIPE_CAPACITY - N) + size_t remaining; + { + AString a = pw.slurp(); + XString x = a.xslice_t(1); + EXPECT_EQ(a.front(), 'a'); + EXPECT_EQ(x.front(), 'b'); + EXPECT_EQ(x.back(), 'b'); + EXPECT_EQ(x, XString(buf, buf + x.size(), nullptr)); + remaining = sizeof(buf) - x.size(); + } + + EXPECT_TRUE(wf.close()); + EXPECT_EQ(pw.slurp(), XString(buf, buf + remaining, nullptr)); +} |