Skip to content

Commit 9348822

Browse files
committed
quic: Instead of shared_ptr use reference to Session
1 parent d08b853 commit 9348822

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/quic/session.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,9 @@ struct Session::Impl final : public MemoryRetainer {
542542
remote_address_(config.remote_address),
543543
application_(SelectApplication(session, config_)),
544544
timer_(session_->env(), [this] {
545-
auto impl = session_->impl_; // we hold a reference to ourself,
546-
// as the reference from session to us may go away
545+
BaseObjectPtr<Session> session = BaseObjectPtr<Session>(session_);
546+
// we hold a reference to session,
547+
// as the reference from session to us may go away otherwise
547548
// while we call OnTimeout
548549
session_->OnTimeout();
549550
}) {
@@ -562,10 +563,10 @@ struct Session::Impl final : public MemoryRetainer {
562563
state_->closing = 1;
563564
STAT_RECORD_TIMESTAMP(Stats, closing_at);
564565

565-
// we hold a reference to ourself,
566-
// as the reference from session to us may go away
566+
// we hold a reference to our session,
567+
// as the reference from session to us may go away otherwise
567568
// while we destroy streams
568-
auto impl = session_->impl_;
569+
BaseObjectPtr<Session> session = BaseObjectPtr<Session>(session_);
569570

570571
// Iterate through all of the known streams and close them. The streams
571572
// will remove themselves from the Session as soon as they are closed.
@@ -1306,7 +1307,7 @@ Session::Session(Endpoint* endpoint,
13061307
: AsyncWrap(endpoint->env(), object, PROVIDER_QUIC_SESSION),
13071308
side_(config.side),
13081309
allocator_(BindingData::Get(env())),
1309-
impl_(std::make_shared<Impl>(this, endpoint, config)),
1310+
impl_(std::make_unique<Impl>(this, endpoint, config)),
13101311
connection_(InitConnection()),
13111312
tls_session_(tls_context->NewSession(this, session_ticket)) {
13121313
DCHECK(impl_);

src/quic/session.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,7 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
497497

498498
Side side_;
499499
ngtcp2_mem allocator_;
500-
std::shared_ptr<Impl> impl_; // we need to have a shared ptr,
501-
// there are situations, where Impl calls the session and
502-
// the session resets this pointer to Impl,
503-
// in this case we need to hold a local shared ptr
504-
// to prevent use after free
500+
std::unique_ptr<Impl> impl_;
505501
QuicConnectionPointer connection_;
506502
std::unique_ptr<TLSSession> tls_session_;
507503
BaseObjectPtr<LogStream> qlog_stream_;

0 commit comments

Comments
 (0)