From: Otto Moerbeek Date: Thu, 12 Jun 2025 09:43:20 +0000 (+0200) Subject: Expose and tidy txtEscape X-Git-Tag: dnsdist-2.0.0-beta1~19^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1eaa81466eab956b6be69b07e956f1495297575;p=thirdparty%2Fpdns.git Expose and tidy txtEscape --- diff --git a/pdns/dnsparser.cc b/pdns/dnsparser.cc index 4cda7b6d54..670cede546 100644 --- a/pdns/dnsparser.cc +++ b/pdns/dnsparser.cc @@ -470,22 +470,24 @@ DNSName PacketReader::getName() throw PDNSException("PacketReader::getName(): name is empty"); } -static string txtEscape(const string &name) +string txtEscape(const string &name) { string ret; - char ebuf[5]; + std::array ebuf{}; - for(char i : name) { - if((unsigned char) i >= 127 || (unsigned char) i < 32) { - snprintf(ebuf, sizeof(ebuf), "\\%03u", (unsigned char)i); - ret += ebuf; + for (char letter : name) { + const unsigned uch = static_cast(letter); + if (uch >= 127 || uch < 32) { + snprintf(ebuf.data(), ebuf.size(), "\\%03u", uch); + ret += ebuf.data(); } - else if(i=='"' || i=='\\'){ + else if (letter == '"' || letter == '\\'){ ret += '\\'; - ret += i; + ret += letter; + } + else { + ret += letter; } - else - ret += i; } return ret; } diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index 947ba6c77c..e3f777452b 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -670,3 +670,5 @@ private: uint32_t d_notyouroffset; // only 'moveOffset' can touch this const uint32_t& d_offset; // look.. but don't touch }; + +string txtEscape(const string &name); diff --git a/pdns/rcpgenerator.cc b/pdns/rcpgenerator.cc index bdb14bf148..ba16af7346 100644 --- a/pdns/rcpgenerator.cc +++ b/pdns/rcpgenerator.cc @@ -838,27 +838,6 @@ void RecordTextWriter::xfrHexBlob(const string& val, bool) } } -// FIXME copied from dnsparser.cc, see #6010 and #3503 if you want a proper solution -static string txtEscape(const string &name) -{ - string ret; - char ebuf[5]; - - for(char i : name) { - if((unsigned char) i >= 127 || (unsigned char) i < 32) { - snprintf(ebuf, sizeof(ebuf), "\\%03u", (unsigned char)i); - ret += ebuf; - } - else if(i=='"' || i=='\\'){ - ret += '\\'; - ret += i; - } - else - ret += i; - } - return ret; -} - void RecordTextWriter::xfrSVCBValueList(const vector &val) { bool shouldQuote{false}; vector escaped;