]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Preserve record order in pdnsutil add-record. 15170/head
authorMiod Vallat <miod.vallat@powerdns.com>
Wed, 19 Feb 2025 11:16:32 +0000 (12:16 +0100)
committerMiod Vallat <miod.vallat@powerdns.com>
Wed, 5 Mar 2025 07:33:17 +0000 (08:33 +0100)
pdns/pdnsutil.cc

index b12203ebebc36cbade6e4c42350d1c6f4364f796..a6fa66ebaa5c41db44c5daa139b33d3e6311188a 100644 (file)
@@ -1727,22 +1727,21 @@ static int addOrReplaceRecord(bool isAdd, const vector<string>& cmds) {
   }
 
   if(isAdd) {
-    // the 'add' case; preserve existing records...
-    size_t newRecordsCount = newrrs.size();
+    // the 'add' case; preserve existing records, making sure to discard
+    // would-be new records which contents are identical to the existing ones.
+    vector<DNSResourceRecord> oldrrs;
     di.backend->lookup(rr.qtype, rr.qname, static_cast<int>(di.id));
-    while(di.backend->get(oldrr)) {
-      // ...unless their contents are identical to the records we are adding.
-      bool skip{false};
-      for (size_t idx = 0; idx < newRecordsCount; ++idx) {
-        if (newrrs.at(idx).content == oldrr.content) {
-          skip = true;
+    while (di.backend->get(oldrr)) {
+      oldrrs.push_back(oldrr);
+      for (auto iter = newrrs.begin(); iter != newrrs.end(); ++iter) {
+        if (iter->content == oldrr.content) {
+          newrrs.erase(iter);
           break;
         }
       }
-      if (!skip) {
-        newrrs.push_back(oldrr);
-      }
     }
+    oldrrs.insert(oldrrs.end(), newrrs.begin(), newrrs.end());
+    newrrs = std::move(oldrrs);
   }
   else {
     cout<<"All existing records for "<<rr.qname<<" IN "<<rr.qtype.toString()<<" will be replaced"<<endl;