Coverity issue fixes.

Issues #332032, #332086, #332157, and #332171.
This commit is contained in:
Wayne Stambaugh 2021-07-09 10:56:17 -04:00
parent f55f9a522b
commit 6c3ba1c20b
4 changed files with 61 additions and 42 deletions

View File

@ -420,6 +420,7 @@ bool COMPILER::lexDefault( T_TOKEN& aToken )
retval.value.str = nullptr; retval.value.str = nullptr;
retval.value.num = 0.0; retval.value.num = 0.0;
retval.value.idx = -1;
retval.token = G_ENDS; retval.token = G_ENDS;
if( m_tokenizer.Done() ) if( m_tokenizer.Done() )

View File

@ -31,6 +31,16 @@ PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( PROJECT* aProject, const wxStrin
JSON_SETTINGS( aFilename, SETTINGS_LOC::PROJECT, projectLocalSettingsVersion, JSON_SETTINGS( aFilename, SETTINGS_LOC::PROJECT, projectLocalSettingsVersion,
/* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false, /* aCreateIfMissing = */ true, /* aCreateIfDefault = */ false,
/* aWriteFile = */ true ), /* aWriteFile = */ true ),
m_ActiveLayer( UNDEFINED_LAYER ),
m_ContrastModeDisplay( HIGH_CONTRAST_MODE::NORMAL ),
m_NetColorMode( NET_COLOR_MODE::RATSNEST ),
m_RatsnestMode( RATSNEST_MODE::ALL ),
m_AutoTrackWidth( true ),
m_ZoneDisplayMode( ZONE_DISPLAY_MODE::SHOW_FILLED ),
m_TrackOpacity( 1.0 ),
m_ViaOpacity( 1.0 ),
m_PadOpacity( 1.0 ),
m_ZoneOpacity( 0.6 ),
m_SelectionFilter(), m_SelectionFilter(),
m_project( aProject ) m_project( aProject )
{ {
@ -362,6 +372,7 @@ void PROJECT_LOCAL_SETTINGS::SaveFileState( const wxString& aFileName,
PROJECT_FILE_STATE fileState; PROJECT_FILE_STATE fileState;
fileState.fileName = aFileName; fileState.fileName = aFileName;
fileState.open = false; fileState.open = false;
fileState.window.maximized = false;
m_files.push_back( fileState ); m_files.push_back( fileState );

View File

@ -1,7 +1,7 @@
/* /*
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2020 CERN * Copyright (C) 2020 CERN
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -38,8 +38,7 @@
#include <math/vector2d.h> #include <math/vector2d.h>
/** /**
* DRC_RTREE - * Implement an R-tree for fast spatial and layer indexing of connectable items.
* Implements an R-tree for fast spatial and layer indexing of connectable items.
* Non-owning. * Non-owning.
*/ */
class DRC_RTREE class DRC_RTREE
@ -82,12 +81,11 @@ public:
} }
/** /**
* Function Insert() * Insert an item into the tree on a particular layer with an optional worst clearance.
* Inserts an item into the tree on a particular layer with an optional worst clearance.
*/ */
void Insert( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aWorstClearance = 0 ) void Insert( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aWorstClearance = 0 )
{ {
wxASSERT( aLayer != UNDEFINED_LAYER ); wxCHECK( aLayer != UNDEFINED_LAYER, /* void */ );
if( aItem->Type() == PCB_FP_TEXT_T && !static_cast<FP_TEXT*>( aItem )->IsVisible() ) if( aItem->Type() == PCB_FP_TEXT_T && !static_cast<FP_TEXT*>( aItem )->IsVisible() )
return; return;
@ -117,8 +115,7 @@ public:
} }
/** /**
* Function RemoveAll() * Remove all items from the RTree.
* Removes all items from the RTree
*/ */
void clear() void clear()
{ {
@ -406,8 +403,9 @@ public:
} }
/** /**
* Returns the number of items in the tree * Return the number of items in the tree.
* @return number of elements in the tree; *
* @return number of elements in the tree.
*/ */
size_t size() const size_t size() const
{ {

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es> * Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
* Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -24,7 +24,7 @@
/** /**
* @file python_scripting.cpp * @file python_scripting.cpp
* @brief methods to add scripting capabilities inside pcbnew * @brief methods to add scripting capabilities inside Pcbnew
*/ */
#include <python_scripting.h> #include <python_scripting.h>
@ -53,24 +53,32 @@
#include <config.h> #include <config.h>
SCRIPTING::SCRIPTING() SCRIPTING::SCRIPTING()
{ {
scriptingSetup(); scriptingSetup();
pybind11::initialize_interpreter(); pybind11::initialize_interpreter();
// Save the current Python thread state and release the // Save the current Python thread state and release the Global Interpreter Lock.
// Global Interpreter Lock.
m_python_thread_state = PyEval_SaveThread(); m_python_thread_state = PyEval_SaveThread();
} }
SCRIPTING::~SCRIPTING() SCRIPTING::~SCRIPTING()
{ {
PyEval_RestoreThread( m_python_thread_state ); PyEval_RestoreThread( m_python_thread_state );
try
{
pybind11::finalize_interpreter(); pybind11::finalize_interpreter();
}
catch( const std::runtime_error& exc )
{
wxLogError( wxT( "Run time error '%s' occurred closing Python scripting" ), exc.what() );
}
} }
bool SCRIPTING::IsWxAvailable() bool SCRIPTING::IsWxAvailable()
{ {
#ifdef KICAD_SCRIPTING_WXPYTHON #ifdef KICAD_SCRIPTING_WXPYTHON
@ -80,6 +88,7 @@ bool SCRIPTING::IsWxAvailable()
#endif #endif
} }
bool SCRIPTING::IsModuleLoaded( std::string& aModule ) bool SCRIPTING::IsModuleLoaded( std::string& aModule )
{ {
PyLOCK lock; PyLOCK lock;
@ -97,15 +106,17 @@ if modulename in sys.modules:
return locals["loaded"].cast<bool>(); return locals["loaded"].cast<bool>();
} }
bool SCRIPTING::scriptingSetup() bool SCRIPTING::scriptingSetup()
{ {
#if defined( __WINDOWS__ ) #if defined( __WINDOWS__ )
#ifdef _MSC_VER #ifdef _MSC_VER
// Under vcpkg/msvc, we need to explicitly set the python home // Under vcpkg/msvc, we need to explicitly set the python home or else it'll start consuming
// or else it'll start consuming system python registry keys and the like instead // system python registry keys and the like instead of the Python distributed with KiCad.
// We are going to follow the "unix" layout for the msvc/vcpkg distributions so exes in /root/bin // We are going to follow the "unix" layout for the msvc/vcpkg distributions so executable
// And the python lib in /root/lib/python3(/Lib,/DLLs) // files are in the /root/bin path and the Python library files are in the
// /root/lib/python3(/Lib,/DLLs) path(s).
wxFileName pyHome; wxFileName pyHome;
pyHome.Assign( Pgm().GetExecutablePath() ); pyHome.Assign( Pgm().GetExecutablePath() );
@ -143,7 +154,6 @@ bool SCRIPTING::scriptingSetup()
wxSetEnv( wxT( "PATH" ), kipython ); wxSetEnv( wxT( "PATH" ), kipython );
} }
#endif #endif
#elif defined( __WXMAC__ ) #elif defined( __WXMAC__ )
// Add default paths to PYTHONPATH // Add default paths to PYTHONPATH
@ -153,7 +163,7 @@ bool SCRIPTING::scriptingSetup()
pypath += PATHS::GetOSXKicadDataDir() + wxT( "/scripting" ); pypath += PATHS::GetOSXKicadDataDir() + wxT( "/scripting" );
// $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i // $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
if( wxGetenv("KICAD_PATH") != NULL ) if( wxGetenv("KICAD_PATH") != nullptr )
{ {
pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") ); pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
} }
@ -163,7 +173,7 @@ bool SCRIPTING::scriptingSetup()
pypath += wxT( ":" ) + Pgm().GetExecutablePath() + wxT( OSX_BUNDLE_PYTHON_SITE_PACKAGES_DIR ); pypath += wxT( ":" ) + Pgm().GetExecutablePath() + wxT( OSX_BUNDLE_PYTHON_SITE_PACKAGES_DIR );
// Original content of $PYTHONPATH // Original content of $PYTHONPATH
if( wxGetenv( wxT( "PYTHONPATH" ) ) != NULL ) if( wxGetenv( wxT( "PYTHONPATH" ) ) != nullptr )
{ {
pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath; pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
} }
@ -178,13 +188,12 @@ bool SCRIPTING::scriptingSetup()
// set $PYTHONHOME // set $PYTHONHOME
wxSetEnv( "PYTHONHOME", pyhome ); wxSetEnv( "PYTHONHOME", pyhome );
#else #else
wxString pypath; wxString pypath;
if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) ) if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
{ {
// When running from build dir, python module gets built next to pcbnew binary // When running from build dir, python module gets built next to Pcbnew binary
pypath = Pgm().GetExecutablePath() + wxT( "../pcbnew" ); pypath = Pgm().GetExecutablePath() + wxT( "../pcbnew" );
} }
else else
@ -200,7 +209,7 @@ bool SCRIPTING::scriptingSetup()
#endif #endif
wxFileName path( PyPluginsPath( true ) + wxT("/") ); wxFileName path( PyPluginsPath( true ) + wxT( "/" ) );
// Ensure the user plugin path exists, and create it if not. // Ensure the user plugin path exists, and create it if not.
// However, if it cannot be created, this is not a fatal error. // However, if it cannot be created, this is not a fatal error.
@ -212,7 +221,7 @@ bool SCRIPTING::scriptingSetup()
/** /**
* Run a python method from the pcbnew module. * Run a python method from the Pcbnew module.
* *
* @param aMethodName is the name of the method (like "pcbnew.myfunction" ) * @param aMethodName is the name of the method (like "pcbnew.myfunction" )
* @param aNames will contain the returned string * @param aNames will contain the returned string
@ -246,13 +255,13 @@ static void RunPythonMethodWithReturnedString( const char* aMethodName, wxString
if( pobj ) if( pobj )
{ {
PyObject* str = PyDict_GetItemString(localDict, "result" ); PyObject* str = PyDict_GetItemString(localDict, "result" );
const char* str_res = NULL; const char* str_res = nullptr;
if(str) if(str)
{ {
PyObject* temp_bytes = PyUnicode_AsEncodedString( str, "UTF-8", "strict" ); PyObject* temp_bytes = PyUnicode_AsEncodedString( str, "UTF-8", "strict" );
if( temp_bytes != NULL ) if( temp_bytes != nullptr )
{ {
str_res = PyBytes_AS_STRING( temp_bytes ); str_res = PyBytes_AS_STRING( temp_bytes );
aNames = FROM_UTF8( str_res ); aNames = FROM_UTF8( str_res );
@ -260,7 +269,7 @@ static void RunPythonMethodWithReturnedString( const char* aMethodName, wxString
} }
else else
{ {
wxLogMessage( "cannot encode unicode python string" ); wxLogMessage( "cannot encode Unicode python string" );
} }
} }
else else
@ -302,7 +311,7 @@ void UpdatePythonEnvVar( const wxString& aVar, const wxString& aValue )
{ {
char cmd[1024]; char cmd[1024];
// Ensure the interpreter is initialized before we try to interact with it // Ensure the interpreter is initialized before we try to interact with it.
if( !Py_IsInitialized() ) if( !Py_IsInitialized() )
return; return;
@ -313,7 +322,7 @@ void UpdatePythonEnvVar( const wxString& aVar, const wxString& aValue )
wxString escapedVal = PyEscapeString( aValue ); wxString escapedVal = PyEscapeString( aValue );
snprintf( cmd, sizeof( cmd ), snprintf( cmd, sizeof( cmd ),
"# coding=utf-8\n" // The values could potentially be UTF8 "# coding=utf-8\n" // The values could potentially be UTF8.
"import os\n" "import os\n"
"os.environ[\"%s\"]=\"%s\"\n", "os.environ[\"%s\"]=\"%s\"\n",
TO_UTF8( escapedVar ), TO_UTF8( escapedVar ),
@ -335,10 +344,10 @@ wxString PyStringToWx( PyObject* aString )
if( !aString ) if( !aString )
return ret; return ret;
const char* str_res = NULL; const char* str_res = nullptr;
PyObject* temp_bytes = PyUnicode_AsEncodedString( aString, "UTF-8", "strict" ); PyObject* temp_bytes = PyUnicode_AsEncodedString( aString, "UTF-8", "strict" );
if( temp_bytes != NULL ) if( temp_bytes != nullptr )
{ {
str_res = PyBytes_AS_STRING( temp_bytes ); str_res = PyBytes_AS_STRING( temp_bytes );
ret = FROM_UTF8( str_res ); ret = FROM_UTF8( str_res );
@ -346,7 +355,7 @@ wxString PyStringToWx( PyObject* aString )
} }
else else
{ {
wxLogMessage( "cannot encode unicode python string" ); wxLogMessage( "cannot encode Unicode python string" );
} }
return ret; return ret;
@ -368,10 +377,10 @@ wxArrayString PyArrayStringToWx( PyObject* aArrayString )
if( element ) if( element )
{ {
const char* str_res = NULL; const char* str_res = nullptr;
PyObject* temp_bytes = PyUnicode_AsEncodedString( element, "UTF-8", "strict" ); PyObject* temp_bytes = PyUnicode_AsEncodedString( element, "UTF-8", "strict" );
if( temp_bytes != NULL ) if( temp_bytes != nullptr )
{ {
str_res = PyBytes_AS_STRING( temp_bytes ); str_res = PyBytes_AS_STRING( temp_bytes );
ret.Add( FROM_UTF8( str_res ), 1 ); ret.Add( FROM_UTF8( str_res ), 1 );
@ -379,7 +388,7 @@ wxArrayString PyArrayStringToWx( PyObject* aArrayString )
} }
else else
{ {
wxLogMessage( "cannot encode unicode python string" ); wxLogMessage( "cannot encode Unicode python string" );
} }
} }
} }
@ -403,7 +412,7 @@ wxString PyErrStringWithTraceback()
PyErr_NormalizeException( &type, &value, &traceback ); PyErr_NormalizeException( &type, &value, &traceback );
if( traceback == NULL ) if( traceback == nullptr )
{ {
traceback = Py_None; traceback = Py_None;
Py_INCREF( traceback ); Py_INCREF( traceback );