From 9436c9b81ad13f83f988cb3002df6f5bb76bd459 Mon Sep 17 00:00:00 2001 From: jinyaoguo Date: Thu, 3 Jul 2025 15:19:50 -0400 Subject: [PATCH 1/2] Fix null pointer dereference in function MdnsAvahi::Resolve The function AllocateResolveContext returns a null pointer if fails to create a new object. The NULL value potentially returned by AllocateResolveContext() is dereferenced without a null check, causing a null pointer dereference bug. --- src/platform/Linux/DnssdImpl.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/platform/Linux/DnssdImpl.cpp b/src/platform/Linux/DnssdImpl.cpp index e8514758c05f41..b15cd441195806 100644 --- a/src/platform/Linux/DnssdImpl.cpp +++ b/src/platform/Linux/DnssdImpl.cpp @@ -857,6 +857,11 @@ CHIP_ERROR MdnsAvahi::Resolve(const char * name, const char * type, DnssdService { AvahiIfIndex avahiInterface = static_cast(interface.GetPlatformInterface()); ResolveContext * resolveContext = AllocateResolveContext(); + if (resolveContext == nullptr) + { + ChipLogError(Discovery, "Failed to allocate resolve context"); + return CHIP_ERROR_NO_MEMORY; + } CHIP_ERROR error = CHIP_NO_ERROR; resolveContext->mInstance = this; resolveContext->mCallback = callback; From 1019c08e564e40df25b09c477d16cef115d515dc Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 3 Jul 2025 19:23:34 +0000 Subject: [PATCH 2/2] Restyled by clang-format --- src/platform/Linux/DnssdImpl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/platform/Linux/DnssdImpl.cpp b/src/platform/Linux/DnssdImpl.cpp index b15cd441195806..518d9b1be0728f 100644 --- a/src/platform/Linux/DnssdImpl.cpp +++ b/src/platform/Linux/DnssdImpl.cpp @@ -862,10 +862,10 @@ CHIP_ERROR MdnsAvahi::Resolve(const char * name, const char * type, DnssdService ChipLogError(Discovery, "Failed to allocate resolve context"); return CHIP_ERROR_NO_MEMORY; } - CHIP_ERROR error = CHIP_NO_ERROR; - resolveContext->mInstance = this; - resolveContext->mCallback = callback; - resolveContext->mContext = context; + CHIP_ERROR error = CHIP_NO_ERROR; + resolveContext->mInstance = this; + resolveContext->mCallback = callback; + resolveContext->mContext = context; if (!interface.IsPresent()) {