[gnome-commander] Add nullptr checks to harden the code



commit 6ae155f1ac67a525dd3c620d1760c0c72f53174c
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Sun Nov 28 12:31:59 2021 +0100

    Add nullptr checks to harden the code

 src/gnome-cmd-con-device.cc | 12 +++++++++++-
 src/gnome-cmd-data.cc       |  6 ++++++
 src/gnome-cmd-file.cc       | 16 +++++++++++++---
 3 files changed, 30 insertions(+), 4 deletions(-)
---
diff --git a/src/gnome-cmd-con-device.cc b/src/gnome-cmd-con-device.cc
index 2e40386a..ea5f6c7d 100644
--- a/src/gnome-cmd-con-device.cc
+++ b/src/gnome-cmd-con-device.cc
@@ -206,6 +206,14 @@ static void do_legacy_mount_thread_func(GnomeCmdCon *con)
     do_legacy_mount(con);
 
     auto gFile = gnome_cmd_con_create_gfile(con, con->base_path);
+
+    if (!gFile)
+    {
+        con->open_failed_error = g_error_new(G_IO_ERROR, G_IO_ERROR_FAILED, "Unable to create a GFile object 
for the given path.");
+        set_con_mount_failed(con);
+        g_warning("Unable to create a GFile object for the given path.");
+        return;
+    }
     con->base_gFileInfo = g_file_query_info(gFile,
                               "*",
                               G_FILE_QUERY_INFO_NONE,
@@ -216,7 +224,7 @@ static void do_legacy_mount_thread_func(GnomeCmdCon *con)
     {
         con->open_failed_error = g_error_copy(error);
         set_con_mount_failed(con);
-        g_critical("Unable to mount the volume via legacy mount, error: %s", error->message);
+        g_warning("Unable to mount the volume via legacy mount, error: %s", error->message);
         g_error_free(error);
         return;
     }
@@ -386,6 +394,8 @@ static GFile *dev_create_gfile (GnomeCmdCon *con, GnomeCmdPath *gnomeCmdPath)
     GFile *newGFile = nullptr;
     GnomeCmdConDevice *dev_con = GNOME_CMD_CON_DEVICE (con);
 
+    g_return_val_if_fail (dev_con->priv->gMount != nullptr, nullptr);
+
     if (gnomeCmdPath)
     {
         auto gMountGFile = g_mount_get_default_location (dev_con->priv->gMount);
diff --git a/src/gnome-cmd-data.cc b/src/gnome-cmd-data.cc
index e8bf1c40..ae257382 100644
--- a/src/gnome-cmd-data.cc
+++ b/src/gnome-cmd-data.cc
@@ -1789,6 +1789,8 @@ static void save_tabs(GSettings *gSettings, const char *gSettingsKey)
                 if (GNOME_CMD_FILE_LIST (fl) && gnome_cmd_con_is_local (fl->con))
                 {
                     gchar* realPath = GNOME_CMD_FILE (fl->cwd)->get_real_path();
+                    if (!realPath)
+                        continue;
                     g_variant_builder_add (&gVariantBuilder, GCMD_SETTINGS_FILE_LIST_TAB_FORMAT_STRING,
                                             realPath,
                                             (guchar) fileSelectorId,
@@ -1806,6 +1808,8 @@ static void save_tabs(GSettings *gSettings, const char *gSettingsKey)
                     if (GNOME_CMD_FILE_LIST (fl) && gnome_cmd_con_is_local (fl->con) && 
(fl==gnomeCmdFileSelector.file_list() || fl->locked))
                     {
                         gchar* realPath = GNOME_CMD_FILE (fl->cwd)->get_real_path();
+                        if (!realPath)
+                            continue;
                         g_variant_builder_add (&gVariantBuilder, GCMD_SETTINGS_FILE_LIST_TAB_FORMAT_STRING,
                                                 realPath,
                                                 (guchar) fileSelectorId,
@@ -1821,6 +1825,8 @@ static void save_tabs(GSettings *gSettings, const char *gSettingsKey)
                     if (GNOME_CMD_FILE_LIST (fl) && gnome_cmd_con_is_local (fl->con) && fl->locked)
                     {
                         gchar* realPath = GNOME_CMD_FILE (fl->cwd)->get_real_path();
+                        if (!realPath)
+                            continue;
                         g_variant_builder_add (&gVariantBuilder, GCMD_SETTINGS_FILE_LIST_TAB_FORMAT_STRING,
                                                 realPath,
                                                 (guchar) fileSelectorId,
diff --git a/src/gnome-cmd-file.cc b/src/gnome-cmd-file.cc
index a9ec582b..c9874e12 100644
--- a/src/gnome-cmd-file.cc
+++ b/src/gnome-cmd-file.cc
@@ -64,6 +64,9 @@ G_DEFINE_TYPE (GnomeCmdFile, gnome_cmd_file, GNOME_CMD_TYPE_FILE_BASE)
 
 inline gboolean has_parent_dir (GnomeCmdFile *f)
 {
+    g_return_val_if_fail (f != nullptr, false);
+    g_return_val_if_fail (f->priv != nullptr, false);
+
     return f->priv->dir_handle && f->priv->dir_handle->ref;
 }
 
@@ -504,9 +507,13 @@ gchar *GnomeCmdFile::get_path()
 //ToDo: Try to remove usage of this method.
 gchar *GnomeCmdFile::get_real_path()
 {
-    auto gfile = get_gfile();
-    gchar *path = g_file_get_path (gfile);
-    g_object_unref (gfile);
+    auto gFileTmp = get_gfile();
+
+    if (!gFileTmp)
+        return nullptr;
+
+    gchar *path = g_file_get_path (gFileTmp);
+    g_object_unref (gFileTmp);
 
     return path;
 }
@@ -621,6 +628,9 @@ GFile *GnomeCmdFile::get_gfile(const gchar *name)
         else
             g_assert ("Non directory file without owning directory");
     }
+    if (!gFile)
+        return nullptr;
+
     auto filename = g_file_get_basename(gFile);
     auto childGFile = gnome_cmd_dir_get_child_gfile (::get_parent_dir (this), name ? name : filename);
     g_free(filename);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]