From abd6e4c558ecec8b670e074ef06f8521116ee684 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 25 Jun 2021 05:08:31 -0400 Subject: [PATCH] use weak_ptr instead of bare pointer to prevent use after free --- llarp/service/outbound_context.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index cd83b43e7..12042b1e2 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -195,8 +195,16 @@ namespace llarp OutboundContext::HandlePathBuilt(path::Path_ptr p) { path::Builder::HandlePathBuilt(p); - p->SetDataHandler(util::memFn(&OutboundContext::HandleHiddenServiceFrame, this)); - p->SetDropHandler(util::memFn(&OutboundContext::HandleDataDrop, this)); + p->SetDataHandler([self = weak_from_this()](auto path, auto frame) { + if (auto ptr = self.lock()) + return ptr->HandleHiddenServiceFrame(path, frame); + return false; + }); + p->SetDropHandler([self = weak_from_this()](auto path, auto id, auto seqno) { + if (auto ptr = self.lock()) + return ptr->HandleDataDrop(path, id, seqno); + return false; + }); if (markedBad) { // ignore new path if we are marked dead