[poppler] goo/gdir.h poppler/SignatureHandler.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Jan 17 15:25:45 UTC 2022


 goo/gdir.h                  |    4 ++++
 poppler/SignatureHandler.cc |   44 +++++++++++++++-----------------------------
 2 files changed, 19 insertions(+), 29 deletions(-)

New commits:
commit b3ca5e58c3839b34c45319848db6b19af344a7f3
Author: Albert Astals Cid <aacid at kde.org>
Date:   Mon Jan 17 16:11:14 2022 +0100

    Use GDir in getDefaultFirefoxCertDB_Linux

diff --git a/goo/gdir.h b/goo/gdir.h
index 1d752090..74a1d00e 100644
--- a/goo/gdir.h
+++ b/goo/gdir.h
@@ -41,6 +41,10 @@
 
 class GooString;
 
+#ifndef _WIN32
+#    include <dirent.h>
+#endif
+
 //------------------------------------------------------------------------
 // GDir and GDirEntry
 //------------------------------------------------------------------------
diff --git a/poppler/SignatureHandler.cc b/poppler/SignatureHandler.cc
index 7cbbeb48..36b970a1 100644
--- a/poppler/SignatureHandler.cc
+++ b/poppler/SignatureHandler.cc
@@ -22,9 +22,11 @@
 #include <config.h>
 
 #include "SignatureHandler.h"
+#include "goo/gdir.h"
 #include "goo/gmem.h"
 
-#include <dirent.h>
+#include <optional>
+
 #include <Error.h>
 
 /* NSS headers */
@@ -665,33 +667,18 @@ std::unique_ptr<X509CertificateInfo> SignatureHandler::getCertificateInfo() cons
     }
 }
 
-static GooString *getDefaultFirefoxCertDB_Linux()
+static std::optional<std::string> getDefaultFirefoxCertDB_Linux()
 {
-    GooString *finalPath = nullptr;
-    DIR *toSearchIn;
-    struct dirent *subFolder;
-
-    GooString *homePath = new GooString(getenv("HOME"));
-    homePath = homePath->append("/.mozilla/firefox/");
+    const std::string firefoxPath = std::string(getenv("HOME")) + "/.mozilla/firefox/";
 
-    if ((toSearchIn = opendir(homePath->c_str())) == nullptr) {
-        error(errInternal, 0, "couldn't find default Firefox Folder");
-        delete homePath;
-        return nullptr;
-    }
-    do {
-        if ((subFolder = readdir(toSearchIn)) != nullptr) {
-            if (strstr(subFolder->d_name, "default") != nullptr) {
-                finalPath = homePath->append(subFolder->d_name);
-                closedir(toSearchIn);
-                return finalPath;
-            }
+    GDir firefoxDir(firefoxPath.c_str());
+    std::unique_ptr<GDirEntry> entry;
+    while (entry = firefoxDir.getNextEntry(), entry != nullptr) {
+        if (entry->isDir() && entry->getName()->toStr().find("default") != std::string::npos) {
+            return entry->getFullPath()->toStr();
         }
-    } while (subFolder != nullptr);
-
-    closedir(toSearchIn);
-    delete homePath;
-    return nullptr;
+    }
+    return {};
 }
 
 std::string SignatureHandler::sNssDir;
@@ -720,13 +707,13 @@ void SignatureHandler::setNSSDir(const GooString &nssDir)
         initSuccess = (NSS_Init(nssDir.c_str()) == SECSuccess);
         sNssDir = nssDir.toStr();
     } else {
-        GooString *certDBPath = getDefaultFirefoxCertDB_Linux();
-        if (certDBPath == nullptr) {
+        const std::optional<std::string> certDBPath = getDefaultFirefoxCertDB_Linux();
+        if (!certDBPath) {
             initSuccess = (NSS_Init("sql:/etc/pki/nssdb") == SECSuccess);
             sNssDir = "sql:/etc/pki/nssdb";
         } else {
             initSuccess = (NSS_Init(certDBPath->c_str()) == SECSuccess);
-            sNssDir = certDBPath->toStr();
+            sNssDir = *certDBPath;
         }
         if (!initSuccess) {
             GooString homeNssDb(getenv("HOME"));
@@ -737,7 +724,6 @@ void SignatureHandler::setNSSDir(const GooString &nssDir)
                 NSS_NoDB_Init(nullptr);
             }
         }
-        delete certDBPath;
     }
 
     if (initSuccess) {


More information about the poppler mailing list