From 20dc5a0ef85e4e1ea8c665ffb83270353cd6fa34 Mon Sep 17 00:00:00 2001 From: Ryan Zuklie Date: Mon, 10 Jul 2023 17:18:27 -0700 Subject: [PATCH] Ensure locked Perfetto handle is non-null. Although the time between when Trace starts and GetDataSourceLocked is called is small, it's still possible for the handle to be invalidated between those two points in time. Check validity prior to use. Test: atest libnetworkstats_test bug: 289476598 Change-Id: Ie4b3d65178e6ca24382efde3f38162f332144e81 --- .../native/libs/libnetworkstats/NetworkTraceHandler.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp b/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp index c5f9631c62..ec63e41694 100644 --- a/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp +++ b/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp @@ -119,7 +119,14 @@ NetworkTracePoller NetworkTraceHandler::sPoller( // the session and delegates writing. The corresponding handler will write // with the setting specified in the trace config. NetworkTraceHandler::Trace([&](NetworkTraceHandler::TraceContext ctx) { - ctx.GetDataSourceLocked()->Write(packets, ctx); + perfetto::LockedHandle handle = + ctx.GetDataSourceLocked(); + // The underlying handle can be invalidated between when Trace starts + // and GetDataSourceLocked is called, but not while the LockedHandle + // exists and holds the lock. Check validity prior to use. + if (handle.valid()) { + handle->Write(packets, ctx); + } }); });