From c99f43f8b79fcef3e5459e0d285af6b347731c9b Mon Sep 17 00:00:00 2001 From: I-am-Krish Date: Mon, 24 Aug 2026 17:38:56 +0530 Subject: [PATCH] Address code review feedback on PR #3448 1. Used C++98 const_iterator for range loop in output.cc 2. Added missing guard condition for empty aliases in output.cc 3. Added is_resolved_address(ss) check in targets.cc to prevent netmask addresses from applying as names 4. Added architectural comment explaining why cross-batch aliases cannot be appended due to streaming XML constraints --- output.cc | 7 ++++--- targets.cc | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/output.cc b/output.cc index 003306297..36be0a23d 100644 --- a/output.cc +++ b/output.cc @@ -1271,7 +1271,7 @@ static void write_xml_initial_hostinfo(const Target *currenths, print_MAC_XML_Info(currenths); /* Output a hostnames element whenever we have a name to write or the target is up. */ - if (currenths->TargetName() != NULL || *currenths->HostName() || strcmp(status, "up") == 0) { + if (currenths->TargetName() != NULL || *currenths->HostName() || strcmp(status, "up") == 0 || !currenths->getTargetNameAliases().empty()) { xml_start_tag("hostnames"); xml_newline(); if (currenths->TargetName() != NULL) { @@ -1282,9 +1282,10 @@ static void write_xml_initial_hostinfo(const Target *currenths, xml_newline(); } /* Print additional names collected when --unique suppressed duplicate scanning. */ - for (const std::string &alias : currenths->getTargetNameAliases()) { + for (std::vector::const_iterator alias = currenths->getTargetNameAliases().begin(); + alias != currenths->getTargetNameAliases().end(); ++alias) { xml_open_start_tag("hostname"); - xml_attribute("name", "%s", alias.c_str()); + xml_attribute("name", "%s", alias->c_str()); xml_attribute("type", "user"); xml_close_empty_tag(); xml_newline(); diff --git a/targets.cc b/targets.cc index a7cfa6e6b..aa151a5d3 100644 --- a/targets.cc +++ b/targets.cc @@ -470,8 +470,13 @@ bool HostGroupState::get_next_host(struct sockaddr_storage *ss, size_t *sslen, s /* This IP is already in the exclude list (a --unique duplicate). At this point *ss holds the excluded IP. If it came from a named host, attach that name as an alias on the existing Target in the - current batch so it appears in XML output. */ - if (o.unique && current_group.get_namedhost()) { + current batch so it appears in XML output. + + Note: Nmap's streaming XML architecture finalizes and closes blocks + after each batch. Therefore, it is impossible to attach aliases to duplicates + that occur in a later batch than the original target without buffering + all XML in memory. We restrict our search to the current batch. */ + if (o.unique && current_group.get_namedhost() && current_group.is_resolved_address(ss)) { const char *skipped_name = current_group.get_resolved_name(); if (skipped_name) { for (int i = 0; i < current_batch_sz; i++) {