Skip to content

Commit 2ef5e22

Browse files
authored
Merge pull request #11813 from xokdvium/dev/fix-use-after-free-libstore-tests
fix(libstore-tests): remove use-after-free bug for `StringSource`
2 parents bf19e5c + 5bc8957 commit 2ef5e22

3 files changed

Lines changed: 16 additions & 22 deletions

File tree

src/libstore-tests/serve-protocol.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -459,21 +459,14 @@ TEST_F(ServeProtoTest, handshake_client_truncated_replay_throws)
459459
CharacterizationTest::readTest("handshake-to-client", [&](std::string toClientLog) {
460460
for (size_t len = 0; len < toClientLog.size(); ++len) {
461461
NullBufferedSink nullSink;
462-
StringSource in {
463-
// truncate
464-
toClientLog.substr(0, len)
465-
};
462+
auto substring = toClientLog.substr(0, len);
463+
StringSource in{substring};
466464
if (len < 8) {
467465
EXPECT_THROW(
468-
ServeProto::BasicClientConnection::handshake(
469-
nullSink, in, defaultVersion, "blah"),
470-
EndOfFile);
466+
ServeProto::BasicClientConnection::handshake(nullSink, in, defaultVersion, "blah"), EndOfFile);
471467
} else {
472468
// Not sure why cannot keep on checking for `EndOfFile`.
473-
EXPECT_THROW(
474-
ServeProto::BasicClientConnection::handshake(
475-
nullSink, in, defaultVersion, "blah"),
476-
Error);
469+
EXPECT_THROW(ServeProto::BasicClientConnection::handshake(nullSink, in, defaultVersion, "blah"), Error);
477470
}
478471
}
479472
});

src/libstore-tests/worker-protocol.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -725,21 +725,14 @@ TEST_F(WorkerProtoTest, handshake_client_truncated_replay_throws)
725725
CharacterizationTest::readTest("handshake-to-client", [&](std::string toClientLog) {
726726
for (size_t len = 0; len < toClientLog.size(); ++len) {
727727
NullBufferedSink nullSink;
728-
StringSource in {
729-
// truncate
730-
toClientLog.substr(0, len)
731-
};
728+
auto substring = toClientLog.substr(0, len);
729+
StringSource in{substring};
732730
if (len < 8) {
733731
EXPECT_THROW(
734-
WorkerProto::BasicClientConnection::handshake(
735-
nullSink, in, defaultVersion, {}),
736-
EndOfFile);
732+
WorkerProto::BasicClientConnection::handshake(nullSink, in, defaultVersion, {}), EndOfFile);
737733
} else {
738734
// Not sure why cannot keep on checking for `EndOfFile`.
739-
EXPECT_THROW(
740-
WorkerProto::BasicClientConnection::handshake(
741-
nullSink, in, defaultVersion, {}),
742-
Error);
735+
EXPECT_THROW(WorkerProto::BasicClientConnection::handshake(nullSink, in, defaultVersion, {}), Error);
743736
}
744737
}
745738
});

src/libutil/serialise.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
///@file
33

44
#include <memory>
5+
#include <type_traits>
56

67
#include "types.hh"
78
#include "util.hh"
@@ -202,7 +203,14 @@ struct StringSource : Source
202203
{
203204
std::string_view s;
204205
size_t pos;
206+
207+
// NOTE: Prevent unintentional dangling views when an implicit conversion
208+
// from std::string -> std::string_view occurs when the string is passed
209+
// by rvalue.
210+
StringSource(std::string &&) = delete;
205211
StringSource(std::string_view s) : s(s), pos(0) { }
212+
StringSource(const std::string& str): StringSource(std::string_view(str)) {}
213+
206214
size_t read(char * data, size_t len) override;
207215
};
208216

0 commit comments

Comments
 (0)