Skip to content
Snippets Groups Projects
Commit 8fca3ff9 authored by Sébastien Blin's avatar Sébastien Blin
Browse files

data_transfer: avoid double free on shutdown

GitLab: #943
Change-Id: I6f011053278bd2d3016e390c281f1cf47dfeac79
parent 577564e6
Branches
No related tags found
No related merge requests found
...@@ -167,8 +167,11 @@ IncomingFile::~IncomingFile() ...@@ -167,8 +167,11 @@ IncomingFile::~IncomingFile()
{ {
if (channel_) if (channel_)
channel_->setOnRecv({}); channel_->setOnRecv({});
{
std::lock_guard<std::mutex> lk(streamMtx_);
if (stream_ && stream_.is_open()) if (stream_ && stream_.is_open())
stream_.close(); stream_.close();
}
if (channel_) if (channel_)
channel_->shutdown(); channel_->shutdown();
} }
...@@ -187,6 +190,7 @@ IncomingFile::process() ...@@ -187,6 +190,7 @@ IncomingFile::process()
{ {
channel_->setOnRecv([w = weak()](const uint8_t* buf, size_t len) { channel_->setOnRecv([w = weak()](const uint8_t* buf, size_t len) {
if (auto shared = w.lock()) { if (auto shared = w.lock()) {
// No need to lock, setOnRecv is resetted before closing
if (shared->stream_.is_open()) if (shared->stream_.is_open())
shared->stream_.write(reinterpret_cast<const char*>(buf), len); shared->stream_.write(reinterpret_cast<const char*>(buf), len);
shared->info_.bytesProgress = shared->stream_.tellp(); shared->info_.bytesProgress = shared->stream_.tellp();
...@@ -197,8 +201,11 @@ IncomingFile::process() ...@@ -197,8 +201,11 @@ IncomingFile::process()
auto shared = w.lock(); auto shared = w.lock();
if (!shared) if (!shared)
return; return;
{
std::lock_guard<std::mutex> lk(shared->streamMtx_);
if (shared->stream_ && shared->stream_.is_open()) if (shared->stream_ && shared->stream_.is_open())
shared->stream_.close(); shared->stream_.close();
}
auto correct = shared->sha3Sum_.empty(); auto correct = shared->sha3Sum_.empty();
if (!correct) { if (!correct) {
// Verify shaSum // Verify shaSum
......
...@@ -97,6 +97,7 @@ private: ...@@ -97,6 +97,7 @@ private:
{ {
return std::static_pointer_cast<IncomingFile>(shared_from_this()); return std::static_pointer_cast<IncomingFile>(shared_from_this());
} }
std::mutex streamMtx_;
std::ofstream stream_; std::ofstream stream_;
std::string sha3Sum_ {}; std::string sha3Sum_ {};
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment