[glom] Add checks for null after dynamic_casts<>.



commit fda2c0a02602da4381f3b3316a89469d5f4d94a6
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Dec 4 19:08:59 2014 +0100

    Add checks for null after dynamic_casts<>.
    
    These are not expected to ever fail,
    but this change stops Coverity Scan from warning about a possible
    "Null pointer dereference".
    Actually, there are probably many more possible null pointer
    dereferences, but dynamic_cast<> is the only one that Coverity
    can be sure is capable of producing a null.

 glom/appwindow.cc                                |   24 +++++++++++++++++
 glom/frame_glom.cc                               |    6 ++++
 glom/mode_data/datawidget/cellrenderer_dblist.cc |   30 +++++++++++----------
 3 files changed, 46 insertions(+), 14 deletions(-)
---
diff --git a/glom/appwindow.cc b/glom/appwindow.cc
index 6787c20..680585a 100644
--- a/glom/appwindow.cc
+++ b/glom/appwindow.cc
@@ -1385,6 +1385,12 @@ void AppWindow::existing_or_new_new()
 
   //Check that the document was given a location:
   Document* document = dynamic_cast<Document*>(get_document());
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document is null." << std::endl;
+    return;
+  }
+
   if(!document->get_file_uri().empty())
   {
     //Get details from the extended save dialog:
@@ -1877,6 +1883,12 @@ void AppWindow::fill_menu_tables()
   }
 
   Document* document = dynamic_cast<Document*>(get_document());
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document is null." << std::endl;
+    return;
+  }
+
   const Document::type_listTableInfo tables = document->get_tables();
   for(Document::type_listTableInfo::const_iterator iter = tables.begin(); iter != tables.end(); ++iter)
   {
@@ -1927,6 +1939,12 @@ void AppWindow::fill_menu_reports(const Glib::ustring& table_name)
   m_refNavReportsActionGroup = Gio::SimpleActionGroup::create();
 
   Document* document = dynamic_cast<Document*>(get_document());
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document is null." << std::endl;
+    return;
+  }
+
   const std::vector<Glib::ustring> reports = document->get_report_names(table_name);
   for(std::vector<Glib::ustring>::const_iterator iter = reports.begin(); iter != reports.end(); ++iter)
   {
@@ -2005,6 +2023,12 @@ void AppWindow::fill_menu_print_layouts(const Glib::ustring& table_name)
   m_refNavPrintLayoutsActionGroup = Gio::SimpleActionGroup::create();
 
   Document* document = dynamic_cast<Document*>(get_document());
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document is null." << std::endl;
+    return;
+  }
+
   const std::vector<Glib::ustring> tables = document->get_print_layout_names(table_name);
 
   // TODO_clientonly: Should this be available in client only mode? We need to
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 17cd18f..a59b13c 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -2267,6 +2267,12 @@ bool Frame_Glom::connection_request_password_and_attempt(bool& database_not_foun
   }
 
   AppWindow* app = dynamic_cast<AppWindow*>(get_app_window());
+  if(!app)
+  {
+    std::cerr << G_STRFUNC << ": app is null." << std::endl;
+    return false;
+  }
+
   app->clear_progress_message();
 
   //Only ask for the password if we are shared on the network, or we are using a centrally hosted server.
diff --git a/glom/mode_data/datawidget/cellrenderer_dblist.cc 
b/glom/mode_data/datawidget/cellrenderer_dblist.cc
index ec785cc..1b7938d 100644
--- a/glom/mode_data/datawidget/cellrenderer_dblist.cc
+++ b/glom/mode_data/datawidget/cellrenderer_dblist.cc
@@ -103,20 +103,22 @@ void CellRendererDbList::repack_cells_fixed(Gtk::CellLayout* combobox)
   {
     //Get the default column, created by set_text_column():
     Gtk::CellRendererText* cell = dynamic_cast<Gtk::CellRendererText*>(combobox->get_first_cell());
+    if (cell)
+    {
+      //Unpack and repack it with expand=false instead of expand=true:
+      //We don't expand the first column, so we can align the other columns.
+      cell->reference();
+      combobox->clear();
+      combobox->pack_start(*cell, false);
+      cell->unreference();
 
-    //Unpack and repack it with expand=false instead of expand=true:
-    //We don't expand the first column, so we can align the other columns.
-    cell->reference();
-    combobox->clear();
-    combobox->pack_start(*cell, false);
-    cell->unreference();
-
-    //Make the renderer render the column:
-    combobox->add_attribute(*cell, "text", get_fixed_model_text_column());
+      //Make the renderer render the column:
+      combobox->add_attribute(*cell, "text", get_fixed_model_text_column());
 
-    cell->property_xalign() = 0.0f;
+      cell->property_xalign() = 0.0f;
 
-    m_repacked_first_cell = true; //Avoid doing this again.
+      m_repacked_first_cell = true; //Avoid doing this again.
+    }
   }
 
   //Add extra cells:


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