]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
pubsuffix: tidy and use C++ strings 15945/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 9 Jul 2025 12:39:38 +0000 (14:39 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 30 Jul 2025 07:17:31 +0000 (09:17 +0200)
Signed-off-by: Otto Moerbeek <otto.moerbeek@open-xchange.com>
pdns/recursordist/mkpubsuffixcc
pdns/recursordist/pubsuffix.hh
pdns/recursordist/pubsuffixloader.cc

index 48de2058aeb69fc6a8510b20bc98019a4b1a2c38..e2d8958e1e8250f158561407206fe18a00fc5103 100755 (executable)
@@ -9,9 +9,10 @@ temp=$(mktemp pubsuffixXXXXXX)
 trap "rm -f $temp" 0 1 2 3 15
 set -e
 curl -o $temp -s -S https://publicsuffix.org/list/public_suffix_list.dat
-(echo "const char* g_pubsuffix[]={";
+(echo "#include \"pubsuffix.hh\""
+ echo "const std::vector<std::string> g_pubsuffix = {";
        for a in $(grep -v "//" "$temp" | grep \\. | egrep "^[.0-9a-z-]*$")
        do
                echo \"$a\",
        done
-echo "0};") > "$1"
+echo "};") > "$1"
index cc5f7c4272ca529677bc23c92d000f5ce24cb6c0..1179cec2621c93a694052fcf8a22462e68a6cdf8 100644 (file)
@@ -25,6 +25,7 @@
 #include <vector>
 
 extern std::vector<std::vector<std::string>> g_pubs;
+extern const std::vector<std::string> g_pubsuffix;
 
 /* initialize the g_pubs variable with the public suffix list,
    using the file passed in parameter if any, or the built-in
index 1174a82c56d20b72ec6528d14fbb8b0882c78806..719151ec9d06a7f5417a61cef2d09476b202b112 100644 (file)
 #include <stdexcept>
 
 #include "dnsname.hh"
-#include "logger.hh"
 #include "logging.hh"
 #include "misc.hh"
 #include "pubsuffix.hh"
 
-extern const char* g_pubsuffix[];
 std::vector<std::vector<std::string>> g_pubs;
 
 void initPublicSuffixList(const std::string& file)
@@ -60,36 +58,35 @@ void initPublicSuffixList(const std::string& file)
           if (name.countLabels() < 2) {
             continue;
           }
-          pbList.push_back(name.labelReverse().getRawLabels());
+          pbList.emplace_back(name.labelReverse().getRawLabels());
         }
         catch (...) {
           /* not a DNS name, ignoring */
+          continue;
         }
       }
 
-      SLOG(g_log << Logger::Info << "Loaded the Public Suffix List from '" << file << "'" << endl,
-           g_slog->withName("runtime")->info(Logr::Info, "Loaded the Public Suffix List", "file", Logging::Loggable(file)));
+      g_slog->withName("runtime")->info(Logr::Info, "Loaded the Public Suffix List", "file", Logging::Loggable(file));
       loaded = true;
     }
     catch (const std::exception& e) {
-      SLOG(g_log << Logger::Warning << "Error while loading the Public Suffix List from '" << file << "', falling back to the built-in list: " << e.what() << endl,
-           g_slog->withName("runtime")->error(Logr::Error, e.what(), "Error while loading the Public Suffix List", "file", Logging::Loggable(file)));
+      g_slog->withName("runtime")->error(Logr::Error, e.what(), "Error while loading the Public Suffix List", "file", Logging::Loggable(file));
     }
   }
 
   if (!loaded) {
     pbList.clear();
 
-    for (const char** p = g_pubsuffix; *p; ++p) {
-      string low = toLower(*p);
-
-      vector<string> parts;
+    for (const auto& entry : g_pubsuffix) {
+      const auto low = toLower(entry);
+      std::vector<std::string> parts;
       stringtok(parts, low, ".");
-      reverse(parts.begin(), parts.end());
-      pbList.push_back(std::move(parts));
+      std::reverse(parts.begin(), parts.end());
+      parts.shrink_to_fit();
+      pbList.emplace_back(std::move(parts));
     }
   }
 
-  sort(pbList.begin(), pbList.end());
+  std::sort(pbList.begin(), pbList.end());
   g_pubs = std::move(pbList);
 }