From 4e998121457330ab637ed0e32525e4b737b25d4c Mon Sep 17 00:00:00 2001
From: Wayne Stambaugh <stambaughw@gmail.com>
Date: Sat, 4 Mar 2023 13:02:05 -0500
Subject: [PATCH] Coverity fixes and code cleaning.

---
 common/libeval_compiler/libeval_compiler.cpp  |  5 +-
 eeschema/dialogs/dialog_sim_model.cpp         |  3 ++
 eeschema/sim/sim_model.cpp                    |  4 ++
 eeschema/sim/simulator_frame.cpp              |  2 +
 eeschema/sim/toolbars_simulator_frame.cpp     |  7 ++-
 pcbnew/pcbnew_jobs_handler.cpp                | 51 +++++++++++++------
 pcbnew/router/pns_kicad_iface.cpp             |  4 +-
 pcbnew/router/pns_node.cpp                    |  2 +
 .../common/test_kicad_stroke_font.cpp         |  7 ++-
 9 files changed, 62 insertions(+), 23 deletions(-)

diff --git a/common/libeval_compiler/libeval_compiler.cpp b/common/libeval_compiler/libeval_compiler.cpp
index 099be2c85d..39b211e4a7 100644
--- a/common/libeval_compiler/libeval_compiler.cpp
+++ b/common/libeval_compiler/libeval_compiler.cpp
@@ -2,7 +2,7 @@
  * This file is part of libeval, a simple math expression evaluator
  *
  * Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
- * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -356,10 +356,11 @@ void COMPILER::newString( const wxString& aString )
     m_parseFinished = false;
 }
 
+
 T_TOKEN COMPILER::getToken()
 {
     T_TOKEN rv;
-    rv.value.str = nullptr;
+    rv.value = defaultTokenValue;
 
     bool    done = false;
 
diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp
index e33e3860c6..3e30dee3d0 100644
--- a/eeschema/dialogs/dialog_sim_model.cpp
+++ b/eeschema/dialogs/dialog_sim_model.cpp
@@ -1160,6 +1160,9 @@ void DIALOG_SIM_MODEL<T_symbol, T_field>::onDifferentialCheckbox( wxCommandEvent
     if( isIbisLoaded() )
     {
         SIM_MODEL_KIBIS* modelkibis = dynamic_cast<SIM_MODEL_KIBIS*>( &curModel() );
+
+        wxCHECK( modelkibis, /* void */ );
+
         bool             diff = m_differentialCheckbox->GetValue() && modelkibis->CanDifferential();
         modelkibis->SwitchSingleEndedDiff( diff );
     }
diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp
index f0758e8ea6..7790491d9a 100644
--- a/eeschema/sim/sim_model.cpp
+++ b/eeschema/sim/sim_model.cpp
@@ -1071,6 +1071,9 @@ bool SIM_MODEL::requiresSpiceModelLine( const SPICE_ITEM& aItem ) const
             continue;
 
         const SIM_MODEL* baseModel = dynamic_cast<const SIM_MODEL*>( m_baseModel );
+
+        wxCHECK( baseModel, false );
+
         std::string      baseValue = baseModel->m_params[ii].value;
 
         if( param.value == baseValue )
@@ -1726,6 +1729,7 @@ void SIM_MODEL::MigrateSimModel( T_symbol& aSymbol, const PROJECT* aProject )
 
                         if( spiceTypeInfo.m_Text == wxT( "DC" ) && tokenizer.CountTokens() == 1 )
                         {
+                            wxCHECK( valueField, /* void */ );
                             valueField->SetText( tokenizer.GetNextToken() );
                             modelFromValueField = false;
                         }
diff --git a/eeschema/sim/simulator_frame.cpp b/eeschema/sim/simulator_frame.cpp
index a592370f01..45bb044f0c 100644
--- a/eeschema/sim/simulator_frame.cpp
+++ b/eeschema/sim/simulator_frame.cpp
@@ -613,6 +613,8 @@ void SIMULATOR_FRAME::ShowChangedLanguage()
     {
         SIM_PANEL_BASE* plot = dynamic_cast<SIM_PLOT_PANEL*>( m_plotNotebook->GetPage( ii ) );
 
+        wxCHECK( plot, /* void */ );
+
         plot->OnLanguageChanged();
 
         wxString pageTitle( m_simulator->TypeToName( plot->GetType(), true ) );
diff --git a/eeschema/sim/toolbars_simulator_frame.cpp b/eeschema/sim/toolbars_simulator_frame.cpp
index 50d6e71b49..19087d53ae 100644
--- a/eeschema/sim/toolbars_simulator_frame.cpp
+++ b/eeschema/sim/toolbars_simulator_frame.cpp
@@ -37,8 +37,11 @@ void SIMULATOR_FRAME::ReCreateHToolbar()
     }
     else
     {
-        m_toolBar = new ACTION_TOOLBAR( dynamic_cast<EDA_BASE_FRAME*>( this ), wxID_ANY,
-                                        wxDefaultPosition, wxDefaultSize,
+        EDA_BASE_FRAME* parent = dynamic_cast<EDA_BASE_FRAME*>( this );
+
+        wxCHECK( parent, /* void */ );
+
+        m_toolBar = new ACTION_TOOLBAR( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                                         KICAD_AUI_TB_STYLE | wxAUI_TB_HORZ_LAYOUT );
     }
 
diff --git a/pcbnew/pcbnew_jobs_handler.cpp b/pcbnew/pcbnew_jobs_handler.cpp
index 00d80a1289..be0f806dea 100644
--- a/pcbnew/pcbnew_jobs_handler.cpp
+++ b/pcbnew/pcbnew_jobs_handler.cpp
@@ -52,6 +52,7 @@
 
 #include "pcbnew_scripting_helpers.h"
 
+
 PCBNEW_JOBS_HANDLER::PCBNEW_JOBS_HANDLER()
 {
     Register( "step",
@@ -183,10 +184,10 @@ int PCBNEW_JOBS_HANDLER::JobExportDxf( JOB* aJob )
     {
         plotOpts.SetDXFPlotUnits( DXF_UNITS::INCHES );
     }
+
     plotOpts.SetPlotFrameRef( aDxfJob->m_plotBorderTitleBlocks );
     plotOpts.SetPlotValue( aDxfJob->m_plotFootprintValues );
     plotOpts.SetPlotReference( aDxfJob->m_plotRefDes );
-
     plotOpts.SetLayerSelection( aDxfJob->m_printMaskLayer );
 
     DXF_PLOTTER* plotter = (DXF_PLOTTER*) StartPlotBoard(
@@ -353,6 +354,7 @@ void PCBNEW_JOBS_HANDLER::populateGerberPlotOptionsFromJob( PCB_PLOT_PARAMS&
     aPlotOpts.SetPlotReference( aJob->m_plotRefDes );
 
     aPlotOpts.SetSubtractMaskFromSilk( aJob->m_subtractSolderMaskFromSilk );
+
     // Always disable plot pad holes
     aPlotOpts.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
 
@@ -409,6 +411,7 @@ int PCBNEW_JOBS_HANDLER::JobExportGerber( JOB* aJob )
 static DRILL_PRECISION precisionListForInches( 2, 4 );
 static DRILL_PRECISION precisionListForMetric( 3, 3 );
 
+
 int PCBNEW_JOBS_HANDLER::JobExportDrill( JOB* aJob )
 {
     JOB_EXPORT_PCB_DRILL* aDrillJob = dynamic_cast<JOB_EXPORT_PCB_DRILL*>( aJob );
@@ -422,6 +425,7 @@ int PCBNEW_JOBS_HANDLER::JobExportDrill( JOB* aJob )
     BOARD* brd = LoadBoard( aDrillJob->m_filename );
 
     std::unique_ptr<GENDRILL_WRITER_BASE> drillWriter;
+
     if( aDrillJob->m_format == JOB_EXPORT_PCB_DRILL::DRILL_FORMAT::EXCELLON )
     {
         drillWriter = std::make_unique<EXCELLON_WRITER>( brd );
@@ -432,13 +436,14 @@ int PCBNEW_JOBS_HANDLER::JobExportDrill( JOB* aJob )
     }
 
     VECTOR2I offset;
+
     if( aDrillJob->m_drillOrigin == JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::ABSOLUTE )
         offset = VECTOR2I( 0, 0 );
     else
         offset = brd->GetDesignSettings().GetAuxOrigin();
 
-
     PLOT_FORMAT mapFormat = PLOT_FORMAT::PDF;
+
     switch( aDrillJob->m_mapFormat )
     {
     case JOB_EXPORT_PCB_DRILL::MAP_FORMAT::POSTSCRIPT: mapFormat = PLOT_FORMAT::POST; break;
@@ -470,27 +475,36 @@ int PCBNEW_JOBS_HANDLER::JobExportDrill( JOB* aJob )
         }
 
         DRILL_PRECISION precision;
+
         if( aDrillJob->m_drillUnits == JOB_EXPORT_PCB_DRILL::DRILL_UNITS::INCHES )
             precision = precisionListForInches;
         else
             precision = precisionListForMetric;
 
-
         EXCELLON_WRITER* excellonWriter = dynamic_cast<EXCELLON_WRITER*>( drillWriter.get() );
+
+        if( excellonWriter == nullptr )
+            return CLI::EXIT_CODES::ERR_UNKNOWN;
+
         excellonWriter->SetFormat( aDrillJob->m_drillUnits
                                           == JOB_EXPORT_PCB_DRILL::DRILL_UNITS::MILLIMETERS,
-                                  zeroFmt, precision.m_Lhs, precision.m_Rhs );
-        excellonWriter->SetOptions( aDrillJob->m_excellonMirrorY, aDrillJob->m_excellonMinimalHeader,
-                                   offset, aDrillJob->m_excellonCombinePTHNPTH );
+                                   zeroFmt, precision.m_Lhs, precision.m_Rhs );
+        excellonWriter->SetOptions( aDrillJob->m_excellonMirrorY,
+                                    aDrillJob->m_excellonMinimalHeader,
+                                    offset, aDrillJob->m_excellonCombinePTHNPTH );
         excellonWriter->SetRouteModeForOvalHoles( aDrillJob->m_excellonOvalDrillRoute );
         excellonWriter->SetMapFileFormat( mapFormat );
 
         excellonWriter->CreateDrillandMapFilesSet( aDrillJob->m_outputDir, true,
-                                                  aDrillJob->m_generateMap, nullptr );
+                                                   aDrillJob->m_generateMap, nullptr );
     }
     else if( aDrillJob->m_format == JOB_EXPORT_PCB_DRILL::DRILL_FORMAT::GERBER )
     {
         GERBER_WRITER* gerberWriter = dynamic_cast<GERBER_WRITER*>( drillWriter.get() );
+
+        if( gerberWriter == nullptr )
+            return CLI::EXIT_CODES::ERR_UNKNOWN;
+
         // Set gerber precision: only 5 or 6 digits for mantissa are allowed
         // (SetFormat() accept 5 or 6, and any other value set the precision to 5)
         // the integer part precision is always 4, and units always mm
@@ -499,7 +513,7 @@ int PCBNEW_JOBS_HANDLER::JobExportDrill( JOB* aJob )
         gerberWriter->SetMapFileFormat( mapFormat );
 
         gerberWriter->CreateDrillandMapFilesSet( aDrillJob->m_outputDir, true,
-                                                aDrillJob->m_generateMap, nullptr );
+                                                 aDrillJob->m_generateMap, nullptr );
     }
 
     return CLI::EXIT_CODES::OK;
@@ -533,7 +547,8 @@ int PCBNEW_JOBS_HANDLER::JobExportPos( JOB* aJob )
         aPosJob->m_outputFile = fn.GetFullName();
     }
 
-    if( aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::ASCII || aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::CSV )
+    if( aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::ASCII
+      || aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::CSV )
     {
         FILE* file = nullptr;
         file = wxFopen( aPosJob->m_outputFile, wxS( "wt" ) );
@@ -549,12 +564,13 @@ int PCBNEW_JOBS_HANDLER::JobExportPos( JOB* aJob )
         bool backSide = aPosJob->m_side == JOB_EXPORT_PCB_POS::SIDE::BACK
                          || aPosJob->m_side == JOB_EXPORT_PCB_POS::SIDE::BOTH;
 
-        PLACE_FILE_EXPORTER exporter( brd, aPosJob->m_units == JOB_EXPORT_PCB_POS::UNITS::MILLIMETERS,
-                                        aPosJob->m_smdOnly, aPosJob->m_excludeFootprintsWithTh,
-                                        frontSide, backSide,
-                                        aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::CSV,
-                                        aPosJob->m_useDrillPlaceFileOrigin,
-                                        aPosJob->m_negateBottomX );
+        PLACE_FILE_EXPORTER exporter( brd,
+                                      aPosJob->m_units == JOB_EXPORT_PCB_POS::UNITS::MILLIMETERS,
+                                      aPosJob->m_smdOnly, aPosJob->m_excludeFootprintsWithTh,
+                                      frontSide, backSide,
+                                      aPosJob->m_format == JOB_EXPORT_PCB_POS::FORMAT::CSV,
+                                      aPosJob->m_useDrillPlaceFileOrigin,
+                                      aPosJob->m_negateBottomX );
         data = exporter.GenPositionData();
 
         fputs( data.c_str(), file );
@@ -722,6 +738,9 @@ int PCBNEW_JOBS_HANDLER::doFpExportSvg( JOB_FP_EXPORT_SVG* aSvgJob, const FOOTPR
 
     FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aFootprint->Clone() );
 
+    if( fp == nullptr )
+        return CLI::EXIT_CODES::ERR_UNKNOWN;
+
     fp->SetLink( niluuid );
     fp->SetFlags( IS_NEW );
     fp->SetParent( brd.get() );
@@ -760,4 +779,4 @@ int PCBNEW_JOBS_HANDLER::doFpExportSvg( JOB_FP_EXPORT_SVG* aSvgJob, const FOOTPR
 
 
     return CLI::EXIT_CODES::OK;
-}
\ No newline at end of file
+}
diff --git a/pcbnew/router/pns_kicad_iface.cpp b/pcbnew/router/pns_kicad_iface.cpp
index e55d84d4fa..0f306a278a 100644
--- a/pcbnew/router/pns_kicad_iface.cpp
+++ b/pcbnew/router/pns_kicad_iface.cpp
@@ -2,7 +2,7 @@
  * KiRouter - a push-and-(sometimes-)shove PCB router
  *
  * Copyright (C) 2013-2016 CERN
- * Copyright (C) 2016-2022 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  *
  * This program is free software: you can redistribute it and/or modify it
@@ -232,6 +232,8 @@ bool PNS_PCBNEW_RULE_RESOLVER::IsNetTieExclusion( const PNS::ITEM* aItem,
                                                   const VECTOR2I& aCollisionPos,
                                                   const PNS::ITEM* aCollidingItem )
 {
+    wxCHECK( aItem && aCollidingItem, false );
+
     std::shared_ptr<DRC_ENGINE> drcEngine = m_board->GetDesignSettings().m_DRCEngine;
     BOARD_ITEM*                 collidingItem = aCollidingItem->Parent();
 
diff --git a/pcbnew/router/pns_node.cpp b/pcbnew/router/pns_node.cpp
index 43c4c4994e..2c230da72d 100644
--- a/pcbnew/router/pns_node.cpp
+++ b/pcbnew/router/pns_node.cpp
@@ -256,6 +256,7 @@ struct NODE::DEFAULT_OBSTACLE_VISITOR : public OBSTACLE_VISITOR
         obs.m_item = aCandidate;
         obs.m_head = m_item;
         obs.m_distFirst = INT_MAX;
+        obs.m_maxFanoutWidth = 0;
         m_tab.push_back( obs );
 
         m_matchCount++;
@@ -323,6 +324,7 @@ NODE::OPT_OBSTACLE NODE::NearestObstacle( const LINE* aLine, int aKindMask,
         return OPT_OBSTACLE();
 
     OBSTACLE nearest;
+    nearest.m_head = nullptr;
     nearest.m_item = nullptr;
     nearest.m_distFirst = INT_MAX;
     nearest.m_maxFanoutWidth = 0;
diff --git a/qa/unittests/common/test_kicad_stroke_font.cpp b/qa/unittests/common/test_kicad_stroke_font.cpp
index 49a1c1d5ce..cdbe15f16a 100644
--- a/qa/unittests/common/test_kicad_stroke_font.cpp
+++ b/qa/unittests/common/test_kicad_stroke_font.cpp
@@ -1,7 +1,7 @@
 /*
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
- * Copyright (C) 2018 KiCad Developers, see AUTHORS.TXT for contributors.
+ * Copyright (C) 2018, 2023 KiCad Developers, see AUTHORS.TXT for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -74,8 +74,11 @@ BOOST_AUTO_TEST_CASE( TabCheck )
                     VECTOR2I( 1000, 1000 ), VECTOR2I( 0, 0 ), EDA_ANGLE::m_Angle0, false,
                     VECTOR2I( 0, 0 ), 0 );
 
-        BOOST_CHECK_MESSAGE( output1.x == output2.x, "Incorrect tab size for \n\t'" << text1.ToStdString() << "' and\n\t'" << text2.ToStdString() << "'" );
+        BOOST_CHECK_MESSAGE( output1.x == output2.x, "Incorrect tab size for \n\t'" <<
+                             text1.ToStdString() << "' and\n\t'" << text2.ToStdString() << "'" );
     }
+
+    delete font;
 }