[Libreoffice-commits] core.git: include/LibreOfficeKit

Mike Kaganski mike.kaganski at collabora.com
Fri Sep 29 16:12:49 UTC 2017


 include/LibreOfficeKit/LibreOfficeKitInit.h |   21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

New commits:
commit 36535c2bef619b3a4090f6f880b975cb6d4dcb11
Author: Mike Kaganski <mike.kaganski at collabora.com>
Date:   Sat Sep 16 22:16:14 2017 +0200

    LibreOfficeKitInit.h: release memory after lok_dlerror() on Windows
    
    When _WIN32 is defined, lok_dlerror() uses FormatMessageA() call with
    FORMAT_MESSAGE_ALLOCATE_BUFFER flag [1] to get returned error string.
    This string is required to be released after use with LocalFree() (or
    HeapFree(), since LocalFree() is not available in recent SDKs).
    
    However, there is no mention in the header that this is required, nor
    lok_dlopen() that uses it does any cleanup. If the calling application
    is expected to be terminated in case of error, then that is no problem;
    but the header might be used in any external client application which
    we know nothing of.
    
    This adds corresponding cleanup function.
    
    Change-Id: I1f14cc5d9e10fe086c3646faaf0e19f2a7a6dd56
    Reviewed-on: https://gerrit.libreoffice.org/42360
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>

diff --git a/include/LibreOfficeKit/LibreOfficeKitInit.h b/include/LibreOfficeKit/LibreOfficeKitInit.h
index 1e34e608d6ba..73e2bbb83dfd 100644
--- a/include/LibreOfficeKit/LibreOfficeKitInit.h
+++ b/include/LibreOfficeKit/LibreOfficeKitInit.h
@@ -57,6 +57,13 @@ extern "C"
         return dlerror();
     }
 
+    // This function must be called to release memory allocated by lok_dlerror()
+    static void lok_dlerror_free(char *pErrMessage)
+    {
+        (void)pErrMessage;
+        // Do nothing for return of dlerror()
+    }
+
     static void extendUnoPath(const char *pPath)
     {
         (void)pPath;
@@ -97,6 +104,12 @@ extern "C"
         return buf;
     }
 
+    // This function must be called to release memory allocated by lok_dlerror()
+    static void lok_dlerror_free(char *pErrMessage)
+    {
+        HeapFree(GetProcessHeap(), 0, pErrMessage);
+    }
+
     static void *lok_dlsym(void *Hnd, const char *pName)
     {
         return reinterpret_cast<void *>(GetProcAddress((HINSTANCE) Hnd, pName));
@@ -198,8 +211,10 @@ static void *lok_dlopen( const char *install_path, char ** _imp_lib )
         struct stat st;
         if (stat(imp_lib, &st) == 0 && st.st_size > 100)
         {
+            char *pErrMessage = lok_dlerror();
             fprintf(stderr, "failed to open library '%s': %s\n",
-                    imp_lib, lok_dlerror());
+                    imp_lib, pErrMessage);
+            lok_dlerror_free(pErrMessage);
             free(imp_lib);
             return NULL;
         }
@@ -209,8 +224,10 @@ static void *lok_dlopen( const char *install_path, char ** _imp_lib )
         dlhandle = lok_loadlib(imp_lib);
         if (!dlhandle)
         {
+            char *pErrMessage = lok_dlerror();
             fprintf(stderr, "failed to open library '%s': %s\n",
-                    imp_lib, lok_dlerror());
+                    imp_lib, pErrMessage);
+            lok_dlerror_free(pErrMessage);
             free(imp_lib);
             return NULL;
         }


More information about the Libreoffice-commits mailing list