[gnome-commander/gcmd-1-12] Harden the behavior when not information via gnomeCmdFile->gFile is available.



commit 49cc88e9e19116be6c8ce2fe46d2ec4ee9d0402a
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Sat Mar 20 23:49:41 2021 +0100

    Harden the behavior when not information via gnomeCmdFile->gFile is available.
    
    Including more null checks, finalize gnomeCmdFile if no gFile member variable
    could be initialized.

 src/gnome-cmd-dir.cc             | 18 ++++++++++++++++++
 src/gnome-cmd-file-collection.cc |  4 ++++
 src/gnome-cmd-file-list.cc       |  1 +
 src/gnome-cmd-file.cc            | 23 ++++++++++++++++++++++-
 4 files changed, 45 insertions(+), 1 deletion(-)
---
diff --git a/src/gnome-cmd-dir.cc b/src/gnome-cmd-dir.cc
index a3603b5a..9b2e115d 100644
--- a/src/gnome-cmd-dir.cc
+++ b/src/gnome-cmd-dir.cc
@@ -238,6 +238,10 @@ GnomeCmdDir *gnome_cmd_dir_new_from_info (GnomeVFSFileInfo *info, GnomeCmdDir *p
 
     GnomeCmdCon *con = gnome_cmd_dir_get_connection (parent);
     GnomeCmdPath *path =  gnome_cmd_dir_get_path (parent)->get_child(info->name);
+    if (!path)
+    {
+        return nullptr;
+    }
 
     GnomeVFSURI *uri = gnome_cmd_con_create_uri (con, path);
     gchar *uri_str = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_PASSWORD);
@@ -626,6 +630,11 @@ GnomeVFSURI *gnome_cmd_dir_get_child_uri (GnomeCmdDir *dir, const gchar *filenam
     g_return_val_if_fail (GNOME_CMD_IS_DIR (dir), nullptr);
 
     GnomeCmdPath *path = dir->priv->path->get_child(filename);
+    if (!path)
+    {
+        return nullptr;
+    }
+
     GnomeVFSURI *uri = gnome_cmd_con_create_uri (dir->priv->con, path);
     delete path;
 
@@ -638,6 +647,10 @@ GFile *gnome_cmd_dir_get_child_gfile (GnomeCmdDir *dir, const gchar *filename)
     g_return_val_if_fail (GNOME_CMD_IS_DIR (dir), nullptr);
 
     GnomeCmdPath *path = dir->priv->path->get_child(filename);
+    if (!path)
+    {
+        return nullptr;
+    }
     GFile *gFile = gnome_cmd_con_create_gfile (dir->priv->con, path);
     delete path;
 
@@ -720,6 +733,11 @@ void gnome_cmd_dir_file_created (GnomeCmdDir *dir, const gchar *uri_str)
     else
         f = gnome_cmd_file_new (info, dir);
 
+    if (!f)
+    {
+        return;
+    }
+
     dir->priv->file_collection->add(f);
     dir->priv->files = dir->priv->file_collection->get_list();
 
diff --git a/src/gnome-cmd-file-collection.cc b/src/gnome-cmd-file-collection.cc
index bbfb23af..6ea5eb46 100644
--- a/src/gnome-cmd-file-collection.cc
+++ b/src/gnome-cmd-file-collection.cc
@@ -34,6 +34,10 @@ void GnomeCmdFileCollection::add(GnomeCmdFile *f)
     list = g_list_append (list, f);
 
     gchar *uri_str = f->get_uri_str();
+    if (!uri_str)
+    {
+         return;
+    }
     g_hash_table_insert (map, uri_str, f);
     f->ref();
 }
diff --git a/src/gnome-cmd-file-list.cc b/src/gnome-cmd-file-list.cc
index bbb146bc..219f7993 100644
--- a/src/gnome-cmd-file-list.cc
+++ b/src/gnome-cmd-file-list.cc
@@ -2830,6 +2830,7 @@ void GnomeCmdFileList::update_style()
 gboolean GnomeCmdFileList::file_is_wanted(GnomeCmdFile *gnomeCmdFile)
 {
     g_return_val_if_fail (gnomeCmdFile != nullptr, FALSE);
+    g_return_val_if_fail (gnomeCmdFile->gFile != nullptr, FALSE);
 
     GError *error;
     error = nullptr;
diff --git a/src/gnome-cmd-file.cc b/src/gnome-cmd-file.cc
index 26ff9729..2c5d71d7 100644
--- a/src/gnome-cmd-file.cc
+++ b/src/gnome-cmd-file.cc
@@ -118,6 +118,10 @@ static void gnome_cmd_file_finalize (GObject *object)
         deleted_files_cnt++;
     }
 
+    if (f->gFile)
+    {
+        g_object_unref(f->gFile);
+    }
     g_free (f->priv);
 
     G_OBJECT_CLASS (gnome_cmd_file_parent_class)->finalize (object);
@@ -157,6 +161,12 @@ GnomeCmdFile *gnome_cmd_file_new (GnomeVFSFileInfo *info, GnomeCmdDir *dir)
 
     gnome_cmd_file_setup (gnomeCmdFile, info, dir);
 
+    if(!gnomeCmdFile->gFile)
+    {
+        g_object_unref(gnomeCmdFile);
+        return nullptr;
+    }
+
     return gnomeCmdFile;
 }
 
@@ -268,7 +278,12 @@ void gnome_cmd_file_setup (GnomeCmdFile *gnomeCmdFile, GnomeVFSFileInfo *info, G
         GNOME_CMD_FILE_BASE (gnomeCmdFile)->gFile = g_file_new_for_path(fUriString);
         gnomeCmdFile->gFile = GNOME_CMD_FILE_BASE (gnomeCmdFile)->gFile;
         g_free(fUriString);
-
+    }
+    // EVERY GnomeCmdFile instance must have a gFile reference
+    if (!gnomeCmdFile->gFile)
+    {
+        gnome_vfs_file_info_unref(gnomeCmdFile->info);
+        return;
     }
 }
 
@@ -418,6 +433,11 @@ gchar *GnomeCmdFile::get_path()
     }
 
     path = gnome_cmd_dir_get_path (::get_parent_dir (this))->get_child(info->name);
+    if (!path)
+    {
+        return nullptr;
+    }
+
     path_str = g_strdup (path->get_path());
     delete path;
 
@@ -942,6 +962,7 @@ void gnome_cmd_file_edit (GnomeCmdFile *f)
 void GnomeCmdFile::update_info(GnomeVFSFileInfo *file_info)
 {
     g_return_if_fail (file_info != nullptr);
+    g_return_if_fail (file_info->name != nullptr);
 
     g_free (collate_key);
     gnome_vfs_file_info_unref (this->info);


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