[poppler] poppler/GlobalParams.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Feb 21 23:03:23 UTC 2022


 poppler/GlobalParams.cc |   54 +++++++++++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 28 deletions(-)

New commits:
commit 6f2e2fde31825a8cd3256fc918e8b48d7a050be2
Author: Stefan Löffler <st.loeffler at gmail.com>
Date:   Mon Feb 21 23:03:21 2022 +0000

    Fix poppler_localdir for relocatable Windows builds
    
    It was working by pure change, since sizeof(retval) is either 4 or 8 so sizeof(retval)-20 is a negative number and that wrapped to a huge number when cast to unsigned so we were lucky it did not overflow the given char *

diff --git a/poppler/GlobalParams.cc b/poppler/GlobalParams.cc
index 8082a571..f09cef5d 100644
--- a/poppler/GlobalParams.cc
+++ b/poppler/GlobalParams.cc
@@ -122,38 +122,38 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 }
 }
 
-static void get_poppler_localdir(char *retval, const char *suffix)
+static std::string get_poppler_localdir(const std::string &suffix)
 {
-    unsigned char *p;
+    const std::string binSuffix("\\bin");
+    std::string retval(MAX_PATH, '\0');
 
-    if (!GetModuleFileNameA(hmodule, (CHAR *)retval, sizeof(retval) - 20)) {
-        strcpy(retval, POPPLER_DATADIR);
-        return;
+    if (!GetModuleFileNameA(hmodule, retval.data(), retval.size())) {
+        return POPPLER_DATADIR;
     }
 
-    p = _mbsrchr((unsigned char *)retval, '\\');
-    *p = '\0';
-    p = _mbsrchr((unsigned char *)retval, '\\');
-    if (p) {
-        if (stricmp((const char *)(p + 1), "bin") == 0)
-            *p = '\0';
+    const std::string::size_type p = retval.rfind('\\');
+    if (p != std::string::npos) {
+        retval.erase(p);
+        if (retval.size() > binSuffix.size() && stricmp(retval.substr(p - binSuffix.size()).c_str(), binSuffix.c_str()) == 0) {
+            retval.erase(p - binSuffix.size());
+        }
     }
-    strcat(retval, suffix);
+    retval += suffix;
+    retval.shrink_to_fit();
+    return retval;
 }
 
 static const char *get_poppler_datadir(void)
 {
-    static char retval[MAX_PATH];
+    static std::string retval;
     static bool beenhere = false;
 
-    if (beenhere)
-        return retval;
-
-    get_poppler_localdir(retval, "\\share\\poppler");
-
-    beenhere = true;
+    if (!beenhere) {
+        retval = get_poppler_localdir("\\share\\poppler");
+        beenhere = true;
+    }
 
-    return retval;
+    return retval.c_str();
 }
 
 #    undef POPPLER_DATADIR
@@ -161,17 +161,15 @@ static const char *get_poppler_datadir(void)
 
 static const char *get_poppler_fontsdir(void)
 {
-    static char retval[MAX_PATH];
+    static std::string retval;
     static bool beenhere = false;
 
-    if (beenhere)
-        return retval;
-
-    get_poppler_localdir(retval, "\\share\\fonts");
-
-    beenhere = true;
+    if (!beenhere) {
+        retval = get_poppler_localdir("\\share\\fonts");
+        beenhere = true;
+    }
 
-    return retval;
+    return retval.c_str();
 }
 #    undef POPPLER_FONTSDIR
 #    define POPPLER_FONTSDIR get_poppler_fontsdir()


More information about the poppler mailing list