From 20d84f775a6b00ca4d611490412d354ebd0a0ce2 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Mon, 14 Aug 2017 22:19:02 +0200 Subject: [PATCH] Fix sortlist in the presence of CNAME In #5357 @killerwhile discovered we were missorting CNAME records when using sortlist. With this commit, we should get it right by moving to stable_sort and being more careful about type equivalence. --- pdns/pdns_recursor.cc | 2 +- pdns/sortlist.cc | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index afad36ca6d..8e84f339d0 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -1089,7 +1089,7 @@ static void startDoResolve(void *p) if(ret.size()) { orderAndShuffle(ret); if(auto sl = luaconfsLocal->sortlist.getOrderCmp(dc->d_remote)) { - sort(ret.begin(), ret.end(), *sl); + stable_sort(ret.begin(), ret.end(), *sl); variableAnswer=true; } } diff --git a/pdns/sortlist.cc b/pdns/sortlist.cc index f840844f0c..49a51bfb12 100644 --- a/pdns/sortlist.cc +++ b/pdns/sortlist.cc @@ -52,15 +52,21 @@ bool SortListOrderCmp::operator()(const ComboAddress& a, const ComboAddress& b) return aOrder < bOrder; } +// call this with **stable_sort** bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const { - if(ar.d_type < br.d_type) - return true; - if(ar.d_type > br.d_type) - return false; + bool aAddr = (ar.d_type == QType::A || ar.d_type == QType::AAAA); + bool bAddr = (br.d_type == QType::A || br.d_type == QType::AAAA); - if(ar.d_type != QType::A && ar.d_type != QType::AAAA) - return false; // all other types are equal among themselves + // anything address related is always 'larger', rest is equal + + if(aAddr && !bAddr) + return false; + else if(!aAddr && bAddr) + return true; + else if(!aAddr && !bAddr) + return true; + int aOrder=std::numeric_limits::max(); int bOrder=aOrder; -- 2.47.2