[Libreoffice-commits] core.git: Branch 'libreoffice-6-0' - libreofficekit/source

Pranav Kant pranavk at collabora.co.uk
Fri Jan 5 08:52:28 UTC 2018


 libreofficekit/source/gtk/lokdocview.cxx |   42 ++++++++++++++-----------------
 1 file changed, 20 insertions(+), 22 deletions(-)

New commits:
commit 54801ad8e88ea3dafeb4442b9b926404a8caf336
Author: Pranav Kant <pranavk at collabora.co.uk>
Date:   Thu Dec 21 12:47:52 2017 +0530

    lokdocview: use std::string where possible
    
    This also prevents an invalid memory access: we were storing the pointer
    to a temporary in the struct and then using it after temporary was gone.
    
    Change-Id: I2b6d9df16bc24b222095ccbf45c9f0bd9c86ed65
    (cherry picked from commit 9d2e0d60c0381a4bb23fada14c80e032b68bf2a8)
    Reviewed-on: https://gerrit.libreoffice.org/46949
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>

diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
index ca47080c7618..4b0f2e4c4d81 100644
--- a/libreofficekit/source/gtk/lokdocview.cxx
+++ b/libreofficekit/source/gtk/lokdocview.cxx
@@ -76,9 +76,9 @@ struct ViewRectangles
 /// Private struct used by this GObject type
 struct LOKDocViewPrivateImpl
 {
-    const gchar* m_aLOPath;
-    const gchar* m_pUserProfileURL;
-    const gchar* m_aDocPath;
+    std::string m_aLOPath;
+    std::string m_aUserProfileURL;
+    std::string m_aDocPath;
     std::string m_aRenderingArguments;
     gdouble m_nLoadProgress;
     gboolean m_bIsLoading;
@@ -192,10 +192,7 @@ struct LOKDocViewPrivateImpl
     std::map<int, ViewRectangle> m_aViewLockRectangles;
 
     LOKDocViewPrivateImpl()
-        : m_aLOPath(nullptr),
-        m_pUserProfileURL(nullptr),
-        m_aDocPath(nullptr),
-        m_nLoadProgress(0),
+        : m_nLoadProgress(0),
         m_bIsLoading(false),
         m_bCanZoomIn(true),
         m_bCanZoomOut(true),
@@ -1769,7 +1766,7 @@ renderOverlay(LOKDocView* pDocView, cairo_t* pCairo)
     if (priv->m_bEdit && priv->m_bCursorVisible && !isEmptyRectangle(priv->m_aVisibleCursor) && priv->m_aTextSelectionRectangles.empty())
     {
         // Have a cursor, but no selection: we need the middle handle.
-        gchar* handleMiddlePath = g_strconcat (priv->m_aLOPath, CURSOR_HANDLE_DIR, "handle_image_middle.png", nullptr);
+        gchar* handleMiddlePath = g_strconcat (priv->m_aLOPath.c_str(), CURSOR_HANDLE_DIR, "handle_image_middle.png", nullptr);
         if (!priv->m_pHandleMiddle)
         {
             priv->m_pHandleMiddle = cairo_image_surface_create_from_png(handleMiddlePath);
@@ -1797,7 +1794,7 @@ renderOverlay(LOKDocView* pDocView, cairo_t* pCairo)
         if (!isEmptyRectangle(priv->m_aTextSelectionStart))
         {
             // Have a start position: we need a start handle.
-            gchar* handleStartPath = g_strconcat (priv->m_aLOPath, CURSOR_HANDLE_DIR, "handle_image_start.png", nullptr);
+            gchar* handleStartPath = g_strconcat (priv->m_aLOPath.c_str(), CURSOR_HANDLE_DIR, "handle_image_start.png", nullptr);
             if (!priv->m_pHandleStart)
             {
                 priv->m_pHandleStart = cairo_image_surface_create_from_png(handleStartPath);
@@ -1809,7 +1806,7 @@ renderOverlay(LOKDocView* pDocView, cairo_t* pCairo)
         if (!isEmptyRectangle(priv->m_aTextSelectionEnd))
         {
             // Have a start position: we need an end handle.
-            gchar* handleEndPath = g_strconcat (priv->m_aLOPath, CURSOR_HANDLE_DIR, "handle_image_end.png", nullptr);
+            gchar* handleEndPath = g_strconcat (priv->m_aLOPath.c_str(), CURSOR_HANDLE_DIR, "handle_image_end.png", nullptr);
             if (!priv->m_pHandleEnd)
             {
                 priv->m_pHandleEnd = cairo_image_surface_create_from_png(handleEndPath);
@@ -2240,7 +2237,7 @@ openDocumentInThread (gpointer data)
     }
 
     priv->m_pOffice->pClass->registerCallback(priv->m_pOffice, globalCallbackWorker, pDocView);
-    priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, priv->m_aDocPath );
+    priv->m_pDocument = priv->m_pOffice->pClass->documentLoad( priv->m_pOffice, priv->m_aDocPath.c_str() );
     if ( !priv->m_pDocument )
     {
         char *pError = priv->m_pOffice->pClass->getError( priv->m_pOffice );
@@ -2503,16 +2500,17 @@ static void lok_doc_view_set_property (GObject* object, guint propId, const GVal
     switch (propId)
     {
     case PROP_LO_PATH:
-        priv->m_aLOPath = g_value_dup_string (value);
+        priv->m_aLOPath = g_value_get_string (value);
         break;
     case PROP_LO_POINTER:
         priv->m_pOffice = static_cast<LibreOfficeKit*>(g_value_get_pointer(value));
         break;
     case PROP_USER_PROFILE_URL:
-        priv->m_pUserProfileURL = g_value_dup_string(value);
+        if (const gchar* pUserProfile = g_value_get_string(value))
+            priv->m_aUserProfileURL = pUserProfile;
         break;
     case PROP_DOC_PATH:
-        priv->m_aDocPath = g_value_dup_string (value);
+        priv->m_aDocPath = g_value_get_string (value);
         break;
     case PROP_DOC_POINTER:
         priv->m_pDocument = static_cast<LibreOfficeKitDocument*>(g_value_get_pointer(value));
@@ -2564,16 +2562,16 @@ static void lok_doc_view_get_property (GObject* object, guint propId, GValue *va
     switch (propId)
     {
     case PROP_LO_PATH:
-        g_value_set_string (value, priv->m_aLOPath);
+        g_value_set_string (value, priv->m_aLOPath.c_str());
         break;
     case PROP_LO_POINTER:
         g_value_set_pointer(value, priv->m_pOffice);
         break;
     case PROP_USER_PROFILE_URL:
-        g_value_set_string(value, priv->m_pUserProfileURL);
+        g_value_set_string(value, priv->m_aUserProfileURL.c_str());
         break;
     case PROP_DOC_PATH:
-        g_value_set_string (value, priv->m_aDocPath);
+        g_value_set_string (value, priv->m_aDocPath.c_str());
         break;
     case PROP_DOC_POINTER:
         g_value_set_pointer(value, priv->m_pDocument);
@@ -2696,14 +2694,14 @@ static gboolean lok_doc_view_initable_init (GInitable *initable, GCancellable* /
     if (priv->m_pOffice != nullptr)
         return TRUE;
 
-    priv->m_pOffice = lok_init_2(priv->m_aLOPath, priv->m_pUserProfileURL);
+    priv->m_pOffice = lok_init_2(priv->m_aLOPath.c_str(), priv->m_aUserProfileURL.empty() ? nullptr : priv->m_aUserProfileURL.c_str());
 
     if (priv->m_pOffice == nullptr)
     {
         g_set_error (error,
                      g_quark_from_static_string ("LOK initialization error"), 0,
                      "Failed to get LibreOfficeKit context. Make sure path (%s) is correct",
-                     priv->m_aLOPath);
+                     priv->m_aLOPath.c_str());
         return FALSE;
     }
     priv->m_nLOKFeatures |= LOK_FEATURE_PART_IN_INVALIDATION_CALLBACK;
@@ -3289,8 +3287,8 @@ SAL_DLLPUBLIC_EXPORT GtkWidget* lok_doc_view_new_from_widget(LOKDocView* pOldLOK
 {
     LOKDocViewPrivate& pOldPriv = getPrivate(pOldLOKDocView);
     GtkWidget* pNewDocView = GTK_WIDGET(g_initable_new(LOK_TYPE_DOC_VIEW, /*cancellable=*/nullptr, /*error=*/nullptr,
-                                                       "lopath", pOldPriv->m_aLOPath,
-                                                       "userprofileurl", pOldPriv->m_pUserProfileURL,
+                                                       "lopath", pOldPriv->m_aLOPath.c_str(),
+                                                       "userprofileurl", pOldPriv->m_aUserProfileURL.c_str(),
                                                        "lopointer", pOldPriv->m_pOffice,
                                                        "docpointer", pOldPriv->m_pDocument,
                                                        "halign", GTK_ALIGN_CENTER,
@@ -3335,7 +3333,7 @@ lok_doc_view_open_document (LOKDocView* pDocView,
 
     LOEvent* pLOEvent = new LOEvent(LOK_LOAD_DOC);
 
-    priv->m_aDocPath = pPath;
+    g_object_set(G_OBJECT(pDocView), "docpath", pPath, nullptr);
     if (pRenderingArguments)
         priv->m_aRenderingArguments = pRenderingArguments;
     g_task_set_task_data(task, pLOEvent, LOEvent::destroy);


More information about the Libreoffice-commits mailing list