From 6d4e973807846201e598b3c5e919c5f85f6f3675 Mon Sep 17 00:00:00 2001 From: abuthahirm Date: Wed, 25 Mar 2026 19:44:37 +0530 Subject: [PATCH] NetworkPkg/DnsDxe: Refactor answer loop to for in ParseDnsResponse The answer section loop in ParseDnsResponse() iterated a counter AnswerSectionNum from 0 to DnsHeader->AnswersNum using a while loop with a manual increment at the bottom. Convert it to an equivalent for loop to make the initialization, condition, and increment explicit. Also replace the unconditional Status = EFI_NOT_FOUND assignment before the loop with a conditional that only sets it when the loop is never entered (DnsHeader->AnswersNum == 0). ParseDnsResponse() initializes Status to EFI_SUCCESS at function entry, and the loop body always overwrites Status, so the pre-assignment was dead code in the common case. The conditional form makes the intent explicit: EFI_NOT_FOUND is only the result when there are no answer records. Cc: Saloni Kasbekar Cc: Zachary Clark-williams Signed-off-by: abuthahirm --- NetworkPkg/DnsDxe/DnsImpl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/NetworkPkg/DnsDxe/DnsImpl.c b/NetworkPkg/DnsDxe/DnsImpl.c index c2629bb8df..f7581e205d 100644 --- a/NetworkPkg/DnsDxe/DnsImpl.c +++ b/NetworkPkg/DnsDxe/DnsImpl.c @@ -1165,10 +1165,9 @@ ParseDnsResponse ( Dns4TokenEntry = NULL; Dns6TokenEntry = NULL; - IpCount = 0; - RRCount = 0; - AnswerSectionNum = 0; - CNameTtl = 0; + IpCount = 0; + RRCount = 0; + CNameTtl = 0; HostAddr4 = NULL; HostAddr6 = NULL; @@ -1378,8 +1377,6 @@ ParseDnsResponse ( } } - Status = EFI_NOT_FOUND; - // // Get Answer name // @@ -1388,7 +1385,11 @@ ParseDnsResponse ( // // Processing AnswerSection. // - while (AnswerSectionNum < DnsHeader->AnswersNum) { + if (DnsHeader->AnswersNum == 0) { + Status = EFI_NOT_FOUND; + } + + for (AnswerSectionNum = 0; AnswerSectionNum < DnsHeader->AnswersNum; AnswerSectionNum++) { // // Check whether the remaining packet length is available or not. // @@ -1635,7 +1636,6 @@ ParseDnsResponse ( // Find next one // AnswerName = (CHAR8 *)AnswerSection + sizeof (*AnswerSection) + AnswerSection->DataLength; - AnswerSectionNum++; } if (Instance->Service->IpVersion == IP_VERSION_4) {