From 1acfbb0ddd9e21f05deb5923615767d9bd31a711 Mon Sep 17 00:00:00 2001 From: Cirilo Bernardo Date: Mon, 16 Feb 2015 12:45:37 -0500 Subject: [PATCH] Coverity scan IDF fixes. * Fix resource leak in idf_parser.cpp * Fix logically dead code in idf_parser.cpp * Fix use after free bug in idf_parser.cpp * Fix unitialized scalar value in idf_parser.cpp * Fix logically dead code in idf_outlines.cpp * Fix unitialized scalar value in idf_outlines.cpp * Fix invalid iterator in idf_common.cpp * Fix unitialized scalar value in vrml_layer.cpp --- utils/idftools/idf_common.cpp | 4 +- utils/idftools/idf_outlines.cpp | 133 ++++++++++++++++++-------------- utils/idftools/idf_outlines.h | 1 - utils/idftools/idf_parser.cpp | 14 ++-- utils/idftools/vrml_layer.cpp | 2 + utils/idftools/vrml_layer.h | 1 - 6 files changed, 85 insertions(+), 70 deletions(-) diff --git a/utils/idftools/idf_common.cpp b/utils/idftools/idf_common.cpp index 948bf23c66..0d0a24bbb0 100644 --- a/utils/idftools/idf_common.cpp +++ b/utils/idftools/idf_common.cpp @@ -950,7 +950,7 @@ void IDF3::GetOutline( std::list& aLines, PrintSeg( *bl ); #endif aOutline.push( *bl ); - aLines.erase( bl ); + bl = aLines.erase( bl ); } continue; @@ -982,7 +982,7 @@ void IDF3::GetOutline( std::list& aLines, printSeg( *bl ); #endif aOutline.push( *bl ); - aLines.erase( bl ); + bl = aLines.erase( bl ); } continue; diff --git a/utils/idftools/idf_outlines.cpp b/utils/idftools/idf_outlines.cpp index bd1e2a3a44..3d2cfa303d 100644 --- a/utils/idftools/idf_outlines.cpp +++ b/utils/idftools/idf_outlines.cpp @@ -377,7 +377,7 @@ void BOARD_OUTLINE::readOutlines( std::ifstream& aBoardFile, IDF3::IDF_VERSION a } // verify winding of previous outline - if( ( loopidx = 0 && !op->IsCCW() ) + if( ( loopidx == 0 && !op->IsCCW() ) || ( loopidx > 0 && op->IsCCW() ) ) { ostringstream ostr; @@ -2232,10 +2232,11 @@ PLACE_OUTLINE::PLACE_OUTLINE( IDF3_BOARD* aParent ) setParent( aParent ); outlineType = OTLN_PLACE; single = true; - thickness = 0.0; + thickness = -1.0; side = LYR_INVALID; } + bool PLACE_OUTLINE::SetSide( IDF3::IDF_LAYER aSide ) { #ifndef DISABLE_IDF_OWNERSHIP @@ -2424,64 +2425,74 @@ void PLACE_OUTLINE::readData( std::ifstream& aBoardFile, const std::string& aHea throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); } - if( !GetIDFString( iline, token, quoted, idx ) ) + if( GetIDFString( iline, token, quoted, idx ) ) { - ostringstream ostr; + std::stringstream teststr; + teststr << token; - ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; - ostr << "* violation: no height specified\n"; - ostr << "* line: '" << iline << "'\n"; - ostr << "* file position: " << pos; + teststr >> thickness; + + if( teststr.fail() ) + { + ostringstream ostr; + + ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; + ostr << "* violation: invalid height\n"; + ostr << "* line: '" << iline << "'\n"; + ostr << "* file position: " << pos; + + throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); + } + + if( thickness < 0.0 ) + { + ostringstream ostr; + + ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; + ostr << "* violation: thickness < 0\n"; + ostr << "* line: '" << iline << "'\n"; + ostr << "* file position: " << pos; + + throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); + } + + if( unit == UNIT_THOU ) + { + thickness *= IDF_THOU_TO_MM; + } + else if( ( aIdfVersion == IDF_V2 ) && ( unit == UNIT_TNM ) ) + { + thickness *= IDF_TNM_TO_MM; + } + else if( unit != UNIT_MM ) + { + ostringstream ostr; + ostr << "\n* BUG: invalid UNIT type: " << unit; + + throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); + } + + if( thickness < 0.0 ) + thickness = 0.0; - throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); } - - std::stringstream teststr; - teststr << token; - - teststr >> thickness; - if( teststr.fail() ) + else { - ostringstream ostr; + // for OTLN_PLACE, thickness may be omitted, but is required for OTLN_PLACE_KEEPOUT + if( outlineType == OTLN_PLACE_KEEPOUT ) + { + ostringstream ostr; - ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; - ostr << "* violation: invalid height\n"; - ostr << "* line: '" << iline << "'\n"; - ostr << "* file position: " << pos; + ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; + ostr << "* violation: missing thickness\n"; + ostr << "* line: '" << iline << "'\n"; + ostr << "* file position: " << pos; - throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); + throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); + } + + thickness = -1.0; } - - if( thickness < 0.0 ) - { - ostringstream ostr; - - ostr << "\n* invalid outline: " << GetOutlineTypeString( outlineType ) << "\n"; - ostr << "* violation: thickness < 0\n"; - ostr << "* line: '" << iline << "'\n"; - ostr << "* file position: " << pos; - - throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); - } - - if( unit == UNIT_THOU ) - { - thickness *= IDF_THOU_TO_MM; - } - else if( ( aIdfVersion == IDF_V2 ) && ( unit == UNIT_TNM ) ) - { - thickness *= IDF_TNM_TO_MM; - } - else if( unit != UNIT_MM ) - { - ostringstream ostr; - ostr << "\n* BUG: invalid UNIT type: " << unit; - - throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); - } - - if( thickness < 0.0 ) - thickness = 0.0; } else { @@ -2574,12 +2585,20 @@ void PLACE_OUTLINE::writeData( std::ofstream& aBoardFile ) break; } - aBoardFile << " "; - - if( unit != UNIT_THOU ) - aBoardFile << setiosflags(ios::fixed) << setprecision(5) << thickness << "\n"; + // thickness is optional for OTLN_PLACE, but mandatory for OTLN_PLACE_KEEPOUT + if( thickness < 0.0 && outlineType == OTLN_PLACE_KEEPOUT) + { + aBoardFile << "\n"; + } else - aBoardFile << setiosflags(ios::fixed) << setprecision(1) << (thickness / IDF_THOU_TO_MM) << "\n"; + { + aBoardFile << " "; + + if( unit != UNIT_THOU ) + aBoardFile << setiosflags(ios::fixed) << setprecision(5) << thickness << "\n"; + else + aBoardFile << setiosflags(ios::fixed) << setprecision(1) << (thickness / IDF_THOU_TO_MM) << "\n"; + } // write RECORD 3 writeOutlines( aBoardFile ); diff --git a/utils/idftools/idf_outlines.h b/utils/idftools/idf_outlines.h index d52c9ff052..33957e7d84 100644 --- a/utils/idftools/idf_outlines.h +++ b/utils/idftools/idf_outlines.h @@ -498,7 +498,6 @@ private: protected: IDF3::IDF_LAYER side; // Board Side [TOP/BOTTOM/BOTH ONLY] (IDF spec) - double height; // Max Height (IDF spec) public: PLACE_OUTLINE( IDF3_BOARD* aParent ); diff --git a/utils/idftools/idf_parser.cpp b/utils/idftools/idf_parser.cpp index cd662b12fc..abf21b721d 100644 --- a/utils/idftools/idf_parser.cpp +++ b/utils/idftools/idf_parser.cpp @@ -1050,9 +1050,7 @@ bool IDF3_COMPONENT::DelDrill( double aDia, double aXpos, double aYpos ) { val = true; delete *itS; - drills.erase( itS ); - itS = drills.begin(); - itE = drills.end(); + itS = drills.erase( itS ); continue; } ++itS; @@ -1341,6 +1339,7 @@ IDF3_BOARD::IDF3_BOARD( IDF3::CAD_TYPE aCadType ) brdFileVersion = 0; libFileVersion = 0; iRefDes = 0; + unit = UNIT_MM; // unlike other outlines which are created as necessary, // the board outline always exists and its parent must @@ -2002,14 +2001,13 @@ void IDF3_BOARD::readBrdSection( std::ifstream& aBoardFile, IDF3::FILE_STATE& aB if( olnOther.insert( pair(op->GetOutlineIdentifier(), op) ).second == false ) { - delete op; - ostringstream ostr; ostr << "invalid IDF file\n"; ostr << "* Violation of specification. Non-unique ID in OTHER_OUTLINE '"; ostr << op->GetOutlineIdentifier() << "'\n"; ostr << "* line: '" << iline << "'\n"; ostr << "* pos: " << pos; + delete op; throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); } @@ -2491,6 +2489,7 @@ void IDF3_BOARD::readLibSection( std::ifstream& aLibFile, IDF3::FILE_STATE& aLib ostr << "* Violation of specification: multiple outlines have the same GEOM and PART name\n"; ostr << "* line: '" << iline << "'\n"; ostr << "* pos: " << pos; + delete pout; throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); } @@ -2504,6 +2503,7 @@ void IDF3_BOARD::readLibSection( std::ifstream& aLibFile, IDF3::FILE_STATE& aLib ostr << "* Expecting .ELECTRICAL or .MECHANICAL, got '" << token << "'\n"; ostr << "* line: '" << iline << "'\n"; ostr << "* pos: " << pos; + delete pout; throw( IDF_ERROR( __FILE__, __FUNCTION__, __LINE__, ostr.str() ) ); } @@ -3429,10 +3429,6 @@ bool IDF3_BOARD::DelBoardDrill( double aDia, double aXpos, double aYpos ) switch( keyo ) { - case UNOWNED: - ostr << "UNOWNED"; - break; - case ECAD: ostr << "ECAD"; break; diff --git a/utils/idftools/vrml_layer.cpp b/utils/idftools/vrml_layer.cpp index 54e8cedd90..8db5fae3a6 100644 --- a/utils/idftools/vrml_layer.cpp +++ b/utils/idftools/vrml_layer.cpp @@ -178,6 +178,8 @@ VRML_LAYER::VRML_LAYER() fix = false; Fault = false; idx = 0; + hidx = 0; + eidx = 0; ord = 0; glcmd = 0; pholes = NULL; diff --git a/utils/idftools/vrml_layer.h b/utils/idftools/vrml_layer.h index 2c7511da71..92b5891b39 100644 --- a/utils/idftools/vrml_layer.h +++ b/utils/idftools/vrml_layer.h @@ -100,7 +100,6 @@ private: bool fix; // when true, no more vertices may be added by the user int idx; // vertex index (number of contained vertices) int ord; // vertex order (number of ordered vertices) - unsigned int idxout; // outline index to first point in 3D outline std::vector vertices; // vertices of all contours std::vector*> contours; // lists of vertices for each contour std::vectorpth; // indicates whether a 'contour' is a PTH or not