changeset 26369:0249ba4c9589

Fix static analyzer detected V668 issues (bug #55347). This patch adresses all V668 (check return value of "new" against NULL) except the following files: utils.cc:188 (keep the check in expression for clarity) * file-editor.cc, resource-manager.cc, shortcut-manager.cc, display.cc, file-editor-tab.cc, ft-text-renderer.cc, graphics.cc, oct-errno.in.cc, pager.cc, __init_fltk__.cc, audiodevinfo.cc, oct-parse.yy, oct-fftw.cc, oct-rand.cc, oct-spparms.cc, oct-env.cc, singleton-cleanup.cc: Remove checking for nullptr when pointer was created with new() which will throw an exception.
author Andreas Weber <octave@josoansi.de>
date Wed, 02 Jan 2019 14:13:59 +0100
parents ddf1cfd62a86
children f1a8a4aac3d4
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor.cc libgui/src/resource-manager.cc libgui/src/shortcut-manager.cc libinterp/corefcn/display.cc libinterp/corefcn/ft-text-renderer.cc libinterp/corefcn/graphics.cc libinterp/corefcn/oct-errno.in.cc libinterp/corefcn/pager.cc libinterp/dldfcn/__init_fltk__.cc libinterp/dldfcn/audiodevinfo.cc libinterp/parse-tree/oct-parse.yy liboctave/numeric/oct-fftw.cc liboctave/numeric/oct-rand.cc liboctave/numeric/oct-spparms.cc liboctave/system/oct-env.cc liboctave/util/singleton-cleanup.cc
diffstat 17 files changed, 109 insertions(+), 203 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -618,9 +618,8 @@
     else
       {
         // Otherwise, delete the newly created lexer and
-        // use the old, exisiting one
-        if (lexer)
-          delete lexer;
+        // use the old, existing one.
+        delete lexer;
       }
   }
 
--- a/libgui/src/m-editor/file-editor.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -446,12 +446,9 @@
     // pass a signal to.  Hence, functionality is here.
 
     file_editor_tab *fileEditorTab = new file_editor_tab (m_ced);
-    if (fileEditorTab)
-      {
-        add_file_editor_tab (fileEditorTab, "");  // new tab with empty title
-        fileEditorTab->new_file (commands);       // title is updated here
-        focus ();                                 // focus editor and new tab
-      }
+    add_file_editor_tab (fileEditorTab, "");  // new tab with empty title
+    fileEditorTab->new_file (commands);       // title is updated here
+    focus ();                                 // focus editor and new tab
   }
 
   void file_editor::request_close_file (bool)
@@ -1327,103 +1324,100 @@
             if (! fileEditorTab)
               fileEditorTab = new file_editor_tab ();
 
-            if (fileEditorTab)
+            fileEditorTab->set_encoding (encoding);
+            QString result = fileEditorTab->load_file (openFileName);
+            if (result == "")
               {
-                fileEditorTab->set_encoding (encoding);
-                QString result = fileEditorTab->load_file (openFileName);
-                if (result == "")
+                // Supply empty title then have the file_editor_tab update
+                // with full or short name.
+                if (! reusing)
+                  add_file_editor_tab (fileEditorTab, "", index);
+                fileEditorTab->update_window_title (false);
+                // file already loaded, add file to mru list here
+                QFileInfo file_info = QFileInfo (openFileName);
+                handle_mru_add_file (file_info.canonicalFilePath (),
+                                     encoding);
+
+                if (line > 0)
                   {
-                    // Supply empty title then have the file_editor_tab update
-                    // with full or short name.
-                    if (! reusing)
-                      add_file_editor_tab (fileEditorTab, "", index);
-                    fileEditorTab->update_window_title (false);
-                    // file already loaded, add file to mru list here
-                    QFileInfo file_info = QFileInfo (openFileName);
-                    handle_mru_add_file (file_info.canonicalFilePath (),
-                                         encoding);
-
-                    if (line > 0)
-                      {
-                        if (insert)
-                          emit fetab_goto_line (fileEditorTab, line);
-
-                        if (debug_pointer)
-                          emit fetab_insert_debugger_pointer (fileEditorTab,
-                                                              line);
-                        if (breakpoint_marker)
-                          emit fetab_do_breakpoint_marker (insert, fileEditorTab,
-                                                           line, cond);
-                      }
+                    if (insert)
+                      emit fetab_goto_line (fileEditorTab, line);
+
+                    if (debug_pointer)
+                      emit fetab_insert_debugger_pointer (fileEditorTab,
+                                                          line);
+                    if (breakpoint_marker)
+                      emit fetab_do_breakpoint_marker (insert, fileEditorTab,
+                                                       line, cond);
+                  }
+              }
+            else
+              {
+                delete fileEditorTab;
+                fileEditorTab = nullptr;
+
+                if (QFile::exists (openFileName))
+                  {
+                    // File not readable:
+                    // create a NonModal message about error.
+                    QMessageBox *msgBox
+                      = new QMessageBox (QMessageBox::Critical,
+                                         tr ("Octave Editor"),
+                                         tr ("Could not open file\n%1\nfor read: %2.").
+                                         arg (openFileName).arg (result),
+                                         QMessageBox::Ok, this);
+
+                    msgBox->setWindowModality (Qt::NonModal);
+                    msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                    msgBox->show ();
                   }
                 else
                   {
-                    delete fileEditorTab;
-                    fileEditorTab = nullptr;
-
-                    if (QFile::exists (openFileName))
+                    // File does not exist, should it be created?
+                    bool create_file = true;
+                    QMessageBox *msgBox;
+
+                    if (! settings->value ("editor/create_new_file", false).toBool ())
                       {
-                        // File not readable:
-                        // create a NonModal message about error.
-                        QMessageBox *msgBox
-                          = new QMessageBox (QMessageBox::Critical,
-                                             tr ("Octave Editor"),
-                                             tr ("Could not open file\n%1\nfor read: %2.").
-                                             arg (openFileName).arg (result),
-                                             QMessageBox::Ok, this);
-
-                        msgBox->setWindowModality (Qt::NonModal);
-                        msgBox->setAttribute (Qt::WA_DeleteOnClose);
-                        msgBox->show ();
+                        msgBox = new QMessageBox (QMessageBox::Question,
+                                                  tr ("Octave Editor"),
+                                                  tr ("File\n%1\ndoes not exist. "
+                                                      "Do you want to create it?").arg (openFileName),
+                                                  QMessageBox::NoButton,nullptr);
+                        QPushButton *create_button =
+                          msgBox->addButton (tr ("Create"), QMessageBox::YesRole);
+                        msgBox->addButton (tr ("Cancel"), QMessageBox::RejectRole);
+                        msgBox->setDefaultButton (create_button);
+                        msgBox->exec ();
+
+                        QAbstractButton *clicked_button = msgBox->clickedButton ();
+                        if (clicked_button != create_button)
+                          create_file = false;
+
+                        delete msgBox;
                       }
-                    else
+
+                    if (create_file)
                       {
-                        // File does not exist, should it be created?
-                        bool create_file = true;
-                        QMessageBox *msgBox;
-
-                        if (! settings->value ("editor/create_new_file", false).toBool ())
+                        // create the file and call the editor again
+                        QFile file (openFileName);
+                        if (! file.open (QIODevice::WriteOnly))
                           {
-                            msgBox = new QMessageBox (QMessageBox::Question,
+                            // error opening the file
+                            msgBox = new QMessageBox (QMessageBox::Critical,
                                                       tr ("Octave Editor"),
-                                                      tr ("File\n%1\ndoes not exist. "
-                                                          "Do you want to create it?").arg (openFileName),
-                                                      QMessageBox::NoButton,nullptr);
-                            QPushButton *create_button =
-                              msgBox->addButton (tr ("Create"), QMessageBox::YesRole);
-                            msgBox->addButton (tr ("Cancel"), QMessageBox::RejectRole);
-                            msgBox->setDefaultButton (create_button);
-                            msgBox->exec ();
-
-                            QAbstractButton *clicked_button = msgBox->clickedButton ();
-                            if (clicked_button != create_button)
-                              create_file = false;
-
-                            delete msgBox;
+                                                      tr ("Could not open file\n%1\nfor write: %2.").
+                                                      arg (openFileName).arg (file.errorString ()),
+                                                      QMessageBox::Ok, this);
+
+                            msgBox->setWindowModality (Qt::NonModal);
+                            msgBox->setAttribute (Qt::WA_DeleteOnClose);
+                            msgBox->show ();
                           }
-
-                        if (create_file)
+                        else
                           {
-                            // create the file and call the editor again
-                            QFile file (openFileName);
-                            if (! file.open (QIODevice::WriteOnly))
-                              {
-                                // error opening the file
-                                msgBox = new QMessageBox (QMessageBox::Critical,
-                                                          tr ("Octave Editor"),
-                                                          tr ("Could not open file\n%1\nfor write: %2.").
-                                                          arg (openFileName).arg (file.errorString ()),
-                                                          QMessageBox::Ok, this);
-
-                                msgBox->setWindowModality (Qt::NonModal);
-                                msgBox->setAttribute (Qt::WA_DeleteOnClose);
-                                msgBox->show ();
-                              }
-                            else
-                              {
-                                file.close ();
-                                request_open_file (openFileName);
-                              }
+                            file.close ();
+                            request_open_file (openFileName);
                           }
                       }
                   }
--- a/libgui/src/resource-manager.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libgui/src/resource-manager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -176,13 +176,6 @@
     if (! instance)
       instance = new resource_manager ();
 
-    if (! instance)
-      {
-        error ("unable to create resource_manager object!");
-
-        retval = false;
-      }
-
     return retval;
   }
 
@@ -294,8 +287,7 @@
     delete m_settings;
     m_settings = new QSettings (file, QSettings::IniFormat);
 
-    if (! (m_settings
-           && QFile::exists (m_settings->fileName ())
+    if (! (QFile::exists (m_settings->fileName ())
            && m_settings->isWritable ()
            && m_settings->status () == QSettings::NoError))
       {
--- a/libgui/src/shortcut-manager.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libgui/src/shortcut-manager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -172,13 +172,6 @@
     if (! instance)
       instance = new shortcut_manager ();
 
-    if (! instance)
-      {
-        error ("unable to create shortcut_manager object!");
-
-        retval = false;
-      }
-
     return retval;
   }
 
@@ -695,19 +688,10 @@
 
         QSettings *osc_settings = new QSettings (file, QSettings::IniFormat);
 
-        if (! osc_settings)
-          {
-            qWarning () << tr ("Failed to open %1 as Octave shortcut file")
-                        .arg (file);
-            return false;
-          }
-        else
-          {
-            if (action == OSC_IMPORT)
-              import_shortcuts (osc_settings);   // import (special action)
-            else if (action == OSC_EXPORT)
-              do_write_shortcuts (osc_settings, false); // export, (save settings)
-          }
+        if (action == OSC_IMPORT)
+          import_shortcuts (osc_settings);   // import (special action)
+        else if (action == OSC_EXPORT)
+          do_write_shortcuts (osc_settings, false); // export, (save settings)
       }
     else
       {
--- a/libinterp/corefcn/display.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/corefcn/display.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -66,14 +66,9 @@
     if (! instance)
       {
         instance = new display_info (query);
-
-        if (instance)
-          singleton_cleanup_list::add (cleanup_instance);
+        singleton_cleanup_list::add (cleanup_instance);
       }
 
-    if (! instance)
-      error ("unable to create display_info object!");
-
     return retval;
   }
 }
--- a/libinterp/corefcn/ft-text-renderer.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/corefcn/ft-text-renderer.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -149,14 +149,9 @@
       if (! instance)
         {
           instance = new ft_manager ();
-
-          if (instance)
-            singleton_cleanup_list::add (cleanup_instance);
+          singleton_cleanup_list::add (cleanup_instance);
         }
 
-      if (! instance)
-        error ("unable to create ft_manager!");
-
       return retval;
     }
 
--- a/libinterp/corefcn/graphics.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/corefcn/graphics.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -11225,9 +11225,7 @@
 gh_manager::create_instance (void)
 {
   instance = new gh_manager ();
-
-  if (instance)
-    singleton_cleanup_list::add (cleanup_instance);
+  singleton_cleanup_list::add (cleanup_instance);
 }
 
 graphics_handle
--- a/libinterp/corefcn/oct-errno.in.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/corefcn/oct-errno.in.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -296,14 +296,9 @@
   if (! instance)
     {
       instance = new octave_errno ();
-
-      if (instance)
-        singleton_cleanup_list::add (cleanup_instance);
+      singleton_cleanup_list::add (cleanup_instance);
     }
 
-  if (! instance)
-    error ("unable to create errno object!");
-
   return retval;
 }
 
--- a/libinterp/corefcn/pager.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/corefcn/pager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -426,13 +426,10 @@
       {
         m_external_pager = new oprocstream (pgr.c_str ());
 
-        if (m_external_pager)
-          {
-            octave::child_list& kids = m_interpreter.get_child_list ();
+        octave::child_list& kids = m_interpreter.get_child_list ();
 
-            kids.insert (m_external_pager->pid (),
-                         pager_event_handler);
-          }
+        kids.insert (m_external_pager->pid (),
+                     pager_event_handler);
       }
   }
 
--- a/libinterp/dldfcn/__init_fltk__.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/dldfcn/__init_fltk__.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -1852,9 +1852,6 @@
     if (! instance)
       instance = new figure_manager ();
 
-    if (! instance)
-      error ("unable to create figure_manager object!");
-
     return retval;
   }
 
--- a/libinterp/dldfcn/audiodevinfo.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/dldfcn/audiodevinfo.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -2230,9 +2230,6 @@
 
   audioplayer *recorder = new audioplayer ();
 
-  if (! recorder)
-    error ("__player_audioplayer__: Couldn't instantiate new audioplayer");
-
   bool is_function = (args(0).is_string () || args(0).is_function_handle ()
                       || args(0).is_inline_function ());
 
--- a/libinterp/parse-tree/oct-parse.yy	Tue Jan 01 22:24:53 2019 -0800
+++ b/libinterp/parse-tree/oct-parse.yy	Wed Jan 02 14:13:59 2019 +0100
@@ -3345,14 +3345,11 @@
       = new octave_user_function (m_lexer.m_symtab_context.curr_scope (),
                                   param_list, nullptr, body);
 
-    if (fcn)
-      {
-        comment_list *tc = m_lexer.m_comment_buf.get_comment ();
-
-        fcn->stash_trailing_comment (tc);
-        fcn->stash_fcn_end_location (end_fcn_stmt->line (),
-                                     end_fcn_stmt->column ());
-      }
+    comment_list *tc = m_lexer.m_comment_buf.get_comment ();
+
+    fcn->stash_trailing_comment (tc);
+    fcn->stash_fcn_end_location (end_fcn_stmt->line (),
+                                 end_fcn_stmt->column ());
 
     // If input is coming from a file, issue a warning if the name of
     // the file does not match the name of the function stated in the
--- a/liboctave/numeric/oct-fftw.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/liboctave/numeric/oct-fftw.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -112,15 +112,9 @@
     if (! instance)
       {
         instance = new fftw_planner ();
-
-        if (instance)
-          singleton_cleanup_list::add (cleanup_instance);
+        singleton_cleanup_list::add (cleanup_instance);
       }
 
-    if (! instance)
-      (*current_liboctave_error_handler)
-        ("unable to create fftw_planner object!");
-
     return retval;
   }
 
@@ -472,15 +466,9 @@
     if (! instance)
       {
         instance = new float_fftw_planner ();
-
-        if (instance)
-          singleton_cleanup_list::add (cleanup_instance);
+        singleton_cleanup_list::add (cleanup_instance);
       }
 
-    if (! instance)
-      (*current_liboctave_error_handler)
-        ("unable to create fftw_planner object!");
-
     return retval;
   }
 
--- a/liboctave/numeric/oct-rand.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/liboctave/numeric/oct-rand.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -63,15 +63,9 @@
     if (! instance)
       {
         instance = new rand ();
-
-        if (instance)
-          singleton_cleanup_list::add (cleanup_instance);
+        singleton_cleanup_list::add (cleanup_instance);
       }
 
-    if (! instance)
-      (*current_liboctave_error_handler)
-        ("unable to create rand object!");
-
     return retval;
   }
 
--- a/liboctave/numeric/oct-spparms.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/liboctave/numeric/oct-spparms.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -43,15 +43,9 @@
   if (! instance)
     {
       instance = new octave_sparse_params ();
-
-      if (instance)
-        singleton_cleanup_list::add (cleanup_instance);
+      singleton_cleanup_list::add (cleanup_instance);
     }
 
-  if (! instance)
-    (*current_liboctave_error_handler)
-      ("unable to create octave_sparse_params object!");
-
   return retval;
 }
 
--- a/liboctave/system/oct-env.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/liboctave/system/oct-env.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -85,15 +85,9 @@
       if (! instance)
         {
           instance = new env ();
-
-          if (instance)
-            singleton_cleanup_list::add (cleanup_instance);
+          singleton_cleanup_list::add (cleanup_instance);
         }
 
-      if (! instance)
-        (*current_liboctave_error_handler)
-          ("unable to create current working directory object!");
-
       return retval;
     }
 
--- a/liboctave/util/singleton-cleanup.cc	Tue Jan 01 22:24:53 2019 -0800
+++ b/liboctave/util/singleton-cleanup.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -45,9 +45,5 @@
   if (! instance)
     instance = new singleton_cleanup_list ();
 
-  if (! instance)
-    (*current_liboctave_error_handler)
-      ("unable to create singleton_cleanup_list object!");
-
   return retval;
 }