# HG changeset patch
# User Andreas Weber <octave@josoansi.de>
# Date 1546434839 -3600
#      Wed Jan 02 14:13:59 2019 +0100
# Branch stable
# Node ID 63e6141ffa535c1a5e6c1df5a732ec889bab3512
# Parent  cd44edea6a31c1f15d50bacebcf68d1c9ac06f90
Fix static analyzer detected V668 issues (bug #55347)

This patch adresses all V668 (check return value of "new"
against NULL) except the following files:

file-editor-tab.cc:621 (no longer findable)
utils.cc:188 (keep the check in expression for clarity)

diff -r cd44edea6a31 -r 63e6141ffa53 libgui/src/m-editor/file-editor.cc
--- a/libgui/src/m-editor/file-editor.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libgui/src/m-editor/file-editor.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -446,12 +446,9 @@ namespace octave
     // 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 @@ namespace octave
             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);
                           }
                       }
                   }
diff -r cd44edea6a31 -r 63e6141ffa53 libgui/src/resource-manager.cc
--- a/libgui/src/resource-manager.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libgui/src/resource-manager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -176,13 +176,6 @@ namespace octave
     if (! instance)
       instance = new resource_manager ();
 
-    if (! instance)
-      {
-        error ("unable to create resource_manager object!");
-
-        retval = false;
-      }
-
     return retval;
   }
 
@@ -294,8 +287,7 @@ namespace octave
     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))
       {
diff -r cd44edea6a31 -r 63e6141ffa53 libgui/src/shortcut-manager.cc
--- a/libgui/src/shortcut-manager.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libgui/src/shortcut-manager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -172,13 +172,6 @@ namespace octave
     if (! instance)
       instance = new shortcut_manager ();
 
-    if (! instance)
-      {
-        error ("unable to create shortcut_manager object!");
-
-        retval = false;
-      }
-
     return retval;
   }
 
@@ -695,19 +688,10 @@ namespace octave
 
         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
       {
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/corefcn/display.cc
--- a/libinterp/corefcn/display.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/corefcn/display.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -66,14 +66,9 @@ namespace octave
     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;
   }
 }
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/corefcn/ft-text-renderer.cc
--- a/libinterp/corefcn/ft-text-renderer.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/corefcn/ft-text-renderer.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -149,14 +149,9 @@ namespace octave
       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;
     }
 
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/corefcn/graphics.cc
--- a/libinterp/corefcn/graphics.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/corefcn/graphics.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -11227,9 +11227,7 @@ void
 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
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/corefcn/oct-errno.in.cc
--- a/libinterp/corefcn/oct-errno.in.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/corefcn/oct-errno.in.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -296,14 +296,9 @@ octave_errno::instance_ok (void)
   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;
 }
 
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/corefcn/pager.cc
--- a/libinterp/corefcn/pager.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/corefcn/pager.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -426,13 +426,10 @@ namespace octave
       {
         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);
       }
   }
 
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/dldfcn/__init_fltk__.cc
--- a/libinterp/dldfcn/__init_fltk__.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/dldfcn/__init_fltk__.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -1852,9 +1852,6 @@ public:
     if (! instance)
       instance = new figure_manager ();
 
-    if (! instance)
-      error ("unable to create figure_manager object!");
-
     return retval;
   }
 
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/dldfcn/audiodevinfo.cc
--- a/libinterp/dldfcn/audiodevinfo.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/dldfcn/audiodevinfo.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -2230,9 +2230,6 @@ Undocumented internal function.
 
   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 ());
 
diff -r cd44edea6a31 -r 63e6141ffa53 libinterp/parse-tree/oct-parse.yy
--- a/libinterp/parse-tree/oct-parse.yy	Tue Jan 01 22:22:15 2019 -0800
+++ b/libinterp/parse-tree/oct-parse.yy	Wed Jan 02 14:13:59 2019 +0100
@@ -3345,14 +3345,11 @@ namespace octave
       = 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
diff -r cd44edea6a31 -r 63e6141ffa53 liboctave/numeric/oct-fftw.cc
--- a/liboctave/numeric/oct-fftw.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/liboctave/numeric/oct-fftw.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -112,15 +112,9 @@ namespace octave
     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 @@ namespace octave
     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;
   }
 
diff -r cd44edea6a31 -r 63e6141ffa53 liboctave/numeric/oct-rand.cc
--- a/liboctave/numeric/oct-rand.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/liboctave/numeric/oct-rand.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -63,15 +63,9 @@ namespace octave
     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;
   }
 
diff -r cd44edea6a31 -r 63e6141ffa53 liboctave/numeric/oct-spparms.cc
--- a/liboctave/numeric/oct-spparms.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/liboctave/numeric/oct-spparms.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -43,15 +43,9 @@ octave_sparse_params::instance_ok (void)
   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;
 }
 
diff -r cd44edea6a31 -r 63e6141ffa53 liboctave/system/oct-env.cc
--- a/liboctave/system/oct-env.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/liboctave/system/oct-env.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -85,15 +85,9 @@ namespace octave
       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;
     }
 
diff -r cd44edea6a31 -r 63e6141ffa53 liboctave/util/singleton-cleanup.cc
--- a/liboctave/util/singleton-cleanup.cc	Tue Jan 01 22:22:15 2019 -0800
+++ b/liboctave/util/singleton-cleanup.cc	Wed Jan 02 14:13:59 2019 +0100
@@ -45,9 +45,5 @@ singleton_cleanup_list::instance_ok (voi
   if (! instance)
     instance = new singleton_cleanup_list ();
 
-  if (! instance)
-    (*current_liboctave_error_handler)
-      ("unable to create singleton_cleanup_list object!");
-
   return retval;
 }
