From: Miod Vallat Date: Mon, 11 Aug 2025 09:27:25 +0000 (+0200) Subject: Rework editZone variable scope again. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49f3d4d225bacf9c84618c032628004ee06ce948;p=thirdparty%2Fpdns.git Rework editZone variable scope again. We only need grouped when setting up the replaceRRSet() calls, so only build it at this point. Signed-off-by: Miod Vallat --- diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index 969a38b52e..548f1ca388 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -1965,7 +1965,6 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) vector pre; vector post; - map, vector> grouped; map, string> changed; enum { CREATEZONEFILE, EDITFILE, INVALIDZONE, ASKAPPLY, ASKSOA, VALIDATE, APPLY } state{CREATEZONEFILE}; @@ -1990,10 +1989,6 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) state = INVALIDZONE; break; } - grouped.clear(); - for (const auto& rec : post) { - grouped[{rec.d_name,rec.d_type}].push_back(rec); - } { vector checkrr; checkrr.reserve(post.size()); @@ -2073,7 +2068,11 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) switch (resp) { case 'y': { - DNSRecord oldSoaDR = grouped[{zone.operator const DNSName&(), QType::SOA}].at(0); // there should be only one SOA record, so we can use .at(0); + auto iter = std::find_if(post.begin(), post.end(), [&zone](const DNSRecord& rec) { return rec.d_type == QType::SOA && rec.d_name == zone.operator const DNSName&(); }); + // There should be one SOA record, therefore iter should be valid... + // ...but it is possible to f*ck up a zone well enough to reach this + // path with iter being invalid (fix in a forthcoming commit) + DNSRecord oldSoaDR = *iter; ostringstream str; str<< col.red() << "-" << oldSoaDR.d_name << " " << oldSoaDR.d_ttl << " IN " << DNSRecordContent::NumberToType(oldSoaDR.d_type) << " " <getZoneRepresentation(true) << col.rst() <getZoneRepresentation(true) << col.rst() <startTransaction(zone, UnknownDomainID); - for(const auto& change : changed) { - vector records; - for(const DNSRecord& rec : grouped[change.first]) { - DNSResourceRecord resrec = DNSResourceRecord::fromWire(rec); - resrec.domain_id = info.id; - records.push_back(std::move(resrec)); + { + map, vector> grouped; + for (const auto& rec : post) { + grouped[{rec.d_name,rec.d_type}].push_back(rec); + } + for(const auto& change : changed) { + vector records; + for(const DNSRecord& rec : grouped[change.first]) { + DNSResourceRecord resrec = DNSResourceRecord::fromWire(rec); + resrec.domain_id = info.id; + records.push_back(std::move(resrec)); + } + info.backend->replaceRRSet(info.id, change.first.first, QType(change.first.second), records); } - info.backend->replaceRRSet(info.id, change.first.first, QType(change.first.second), records); } + post.clear(); rectifyZone(dsk, zone, false, false); info.backend->commitTransaction(); return EXIT_SUCCESS;