[glom] Surround all Glib::filename_from_uri() calls with try/catch.



commit 0ae7d14da539645addee13dcf567d6edc8c9becb
Author: Murray Cumming <murrayc murrayc com>
Date:   Sun Feb 7 16:12:54 2016 +0100

    Surround all Glib::filename_from_uri() calls with try/catch.
    
    Because this can fail in the real world and it would be nice to know
    as soon as possible:
    https://bugzilla.gnome.org/show_bug.cgi?id=761373

 glom/libglom/connectionpool_backends/mysql_self.cc |   26 +++++++++-
 .../connectionpool_backends/postgres_self.cc       |   53 ++++++++++++++++++--
 glom/libglom/document/document.cc                  |   26 +++++++++-
 3 files changed, 97 insertions(+), 8 deletions(-)
---
diff --git a/glom/libglom/connectionpool_backends/mysql_self.cc 
b/glom/libglom/connectionpool_backends/mysql_self.cc
index 8e2db1e..faf1404 100644
--- a/glom/libglom/connectionpool_backends/mysql_self.cc
+++ b/glom/libglom/connectionpool_backends/mysql_self.cc
@@ -145,7 +145,18 @@ Backend::InitErrors MySQLSelfHosted::initialize(const SlotProgress& slot_progres
   if(file_exists_uri(dbdir_uri))
     return InitErrors::DIRECTORY_ALREADY_EXISTS;
 
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return InitErrors::OTHER;
+  }
+
   //std::cout << "debug: dbdir=" << dbdir << std::endl;
   g_assert(!dbdir.empty());
 
@@ -307,7 +318,18 @@ Backend::StartupErrors MySQLSelfHosted::startup(const SlotProgress& slot_progres
     return StartupErrors::FAILED_NO_MAIN_DIRECTORY;
   }
 
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return StartupErrors::FAILED_UNKNOWN_REASON;
+  }
+
   g_assert(!dbdir.empty());
 
   const auto dbdir_data = Glib::build_filename(dbdir, FILENAME_DATA);
diff --git a/glom/libglom/connectionpool_backends/postgres_self.cc 
b/glom/libglom/connectionpool_backends/postgres_self.cc
index d174d34..68359a8 100644
--- a/glom/libglom/connectionpool_backends/postgres_self.cc
+++ b/glom/libglom/connectionpool_backends/postgres_self.cc
@@ -166,7 +166,18 @@ Backend::InitErrors PostgresSelfHosted::initialize(const SlotProgress& slot_prog
   if(file_exists_uri(dbdir_uri))
     return InitErrors::DIRECTORY_ALREADY_EXISTS;
 
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return InitErrors::OTHER;
+  }
+
   //std::cout << "debug: dbdir=" << dbdir << std::endl;
   g_assert(!dbdir.empty());
 
@@ -247,7 +258,18 @@ Backend::StartupErrors PostgresSelfHosted::startup(const SlotProgress& slot_prog
     return StartupErrors::FAILED_NO_MAIN_DIRECTORY;
   }
 
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return StartupErrors::FAILED_UNKNOWN_REASON;
+  }
+
   g_assert(!dbdir.empty());
 
   const auto dbdir_data = Glib::build_filename(dbdir, FILENAME_DATA);
@@ -374,7 +396,19 @@ bool PostgresSelfHosted::cleanup(const SlotProgress& slot_progress)
     return true; //Don't try to stop it if we have not started it.
 
   const auto dbdir_uri = m_database_directory_uri;
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return false;
+  }
+
   g_assert(!dbdir.empty());
 
   const auto dbdir_data = Glib::build_filename(dbdir, FILENAME_DATA);
@@ -428,7 +462,18 @@ bool PostgresSelfHosted::set_network_shared(const SlotProgress& /* slot_progress
   m_network_shared = network_shared;
 
   const auto dbdir_uri = m_database_directory_uri;
-  const auto dbdir = Glib::filename_from_uri(dbdir_uri);
+
+  std::string dbdir;
+  try
+  {
+    dbdir = Glib::filename_from_uri(dbdir_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return false;
+  }
 
   const std::string dbdir_uri_config = dbdir_uri + "/config";
   const char* default_conf_contents = nullptr;
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 1c2020c..2349242 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -4849,7 +4849,19 @@ Glib::ustring Document::save_backup_file(const Glib::ustring& uri, const SlotPro
   //Save a copy of the .glom document,
   //with the same name as the directory:
   //For instance <path>/chosendirectory/chosendirectory.glom
-  const auto path_dir = Glib::filename_from_uri(uri);
+
+  std::string path_dir;
+  try
+  {
+    path_dir = Glib::filename_from_uri(uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return Glib::ustring();
+  }
+
   const auto basename_dir = Glib::path_get_basename(path_dir);
   const auto filepath_document = Glib::build_filename(path_dir, basename_dir + ".glom");
   const auto uri_document = Glib::filename_to_uri(filepath_document);
@@ -4972,7 +4984,17 @@ Glib::ustring Document::extract_backup_file(const Glib::ustring& backup_uri, std
   backup_path.clear();
 
   // We cannot use an uri here, because we cannot untar remote files.
-  const auto filename_tarball = Glib::filename_from_uri(backup_uri);
+  std::string filename_tarball;
+  try
+  {
+    filename_tarball = Glib::filename_from_uri(backup_uri);
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << G_STRFUNC << "Glib::filename_from_uri() failed: " << ex.what() << std::endl;
+
+    return Glib::ustring();
+  }
 
   struct archive* a = archive_read_new();
   auto scoped = make_unique_ptr_archive(a);


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