From: abv Date: Thu, 29 May 2014 11:05:47 +0000 (+0400) Subject: 0024897: Possibility of uncontrolled exit if scanner fails in ExprIntrp X-Git-Tag: V6_8_0_beta~296 X-Git-Url: http://git.dev.opencascade.org/gitweb/?p=occt.git;a=commitdiff_plain;h=2a54ebbf29872caa7deb6b528e36ea7451dbc0c5 0024897: Possibility of uncontrolled exit if scanner fails in ExprIntrp Macro YY_FATAL_ERROR() is defined in ExprIntrp.lex in order to have exception instead of program exit in case of scanner error. Fixed-length string buffers are replaced by TCollection_AsciiString to avoid buffer overflow. Some code refactoring: declarations of functions moved to header file, unused global declarations and variables removed, compiler warnings fixed or disabled, obsolete EDL file removed. Test case for issue CR24897 Correction of MSVC compiler warning when scanner is generated using Flex 2.5.35 on Linux --- diff --git a/src/ExprIntrp/ExprIntrp.cxx b/src/ExprIntrp/ExprIntrp.cxx index c00b856cce..ef782fcdd7 100644 --- a/src/ExprIntrp/ExprIntrp.cxx +++ b/src/ExprIntrp/ExprIntrp.cxx @@ -20,13 +20,11 @@ #include #include -TCollection_AsciiString ExprIntrp_thestring; -Standard_Integer ExprIntrp_thecurchar=0; +static TCollection_AsciiString ExprIntrp_thestring; Standard_Boolean ExprIntrp::Parse(const Handle(ExprIntrp_Generator)& gen, const TCollection_AsciiString& str) { ExprIntrp_Recept.SetMaster(gen); - ExprIntrp_thecurchar = 0; if (str.Length() == 0) return Standard_False; ExprIntrp_thestring = str; ExprIntrp_start_string(ExprIntrp_thestring.ToCString()); diff --git a/src/ExprIntrp/ExprIntrp.lex b/src/ExprIntrp/ExprIntrp.lex index 1685364e46..c2e2bc749a 100644 --- a/src/ExprIntrp/ExprIntrp.lex +++ b/src/ExprIntrp/ExprIntrp.lex @@ -19,18 +19,16 @@ %{ #include +#include + #define YY_SKIP_YYWRAP static YY_BUFFER_STATE ExprIntrp_bufstring; -void ExprIntrp_SetResult(); -void ExprIntrp_SetDegree(); - -int ExprIntrlex (void); - -void ExprIntrp_start_string(char* str) +void ExprIntrp_start_string(const char* str) { - ExprIntrp_bufstring = ExprIntrp_scan_string(str); + // depending on configuration and generator, yyconst may be defined as const or empty + ExprIntrp_bufstring = ExprIntrp_scan_string((yyconst char*)str); } void ExprIntrp_stop_string() @@ -39,11 +37,14 @@ void ExprIntrp_stop_string() ExprIntrp_bufstring = (YY_BUFFER_STATE) 0; } -int yywrap() +static int yywrap() { return 1; } +// provide safe error handler (exception instead of exit()) +#define YY_FATAL_ERROR(msg) ExprIntrperror(msg) + #ifdef _MSC_VER // add includes for flex 2.91 (Linux version) #include @@ -52,8 +53,14 @@ int yywrap() // Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35) #define YY_NO_UNISTD_H -// disable MSVC warnings in flex 2.89 code -#pragma warning(disable:4131 4244 4273 4127) +// disable MSVC warnings in flex 2.89 and 2.5.35 code +// Note that Intel compiler also defines _MSC_VER but has different warning ids +#if defined(__INTEL_COMPILER) +#pragma warning(disable:177 1786 1736) +#else +#pragma warning(disable:4131 4244 4273 4127 4267) +#endif + #endif #ifdef __GNUC__ diff --git a/src/ExprIntrp/ExprIntrp.yacc b/src/ExprIntrp/ExprIntrp.yacc index c3d7d20d34..a00bc5243c 100644 --- a/src/ExprIntrp/ExprIntrp.yacc +++ b/src/ExprIntrp/ExprIntrp.yacc @@ -15,6 +15,8 @@ */ %{ +#include + extern void ExprIntrp_EndOfFuncDef(); extern void ExprIntrp_EndOfRelation(); extern void ExprIntrp_AssignVariable(); @@ -59,9 +61,6 @@ extern void ExprIntrp_VariableIdentifier(); extern void ExprIntrp_Productor(); extern void ExprIntrp_EndOfEqual(); -extern void ExprIntrperror(char* msg); -extern int ExprIntrplex(void); - // disable MSVC warnings in bison code #ifdef _MSC_VER #pragma warning(disable:4131 4244 4127 4702) diff --git a/src/ExprIntrp/ExprIntrp_CMPLRS.edl b/src/ExprIntrp/ExprIntrp_CMPLRS.edl deleted file mode 100644 index ab9340886b..0000000000 --- a/src/ExprIntrp/ExprIntrp_CMPLRS.edl +++ /dev/null @@ -1,23 +0,0 @@ --- Created by: Prestataire Pascal BABIN --- Copyright (c) 1997-1999 Matra Datavision --- Copyright (c) 1999-2014 OPEN CASCADE SAS --- --- This file is part of Open CASCADE Technology software library. --- --- This library is free software; you can redistribute it and/or modify it under --- the terms of the GNU Lesser General Public License version 2.1 as published --- by the Free Software Foundation, with special exception defined in the file --- OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT --- distribution for complete text of the license and disclaimer of any warranty. --- --- Alternatively, this file may be used under the terms of Open CASCADE --- commercial license or contractual agreement. - -@ifnotdefined ( %ExprIntrp_CMPLRS_EDL) then -@set %ExprIntrp_CMPLRS_EDL = ""; - -@if ( %Station == "hp" ) then - @string %CMPLRS_C_Options = %CMPLRS_C_Options " -Ae"; -@endif; - -@endif; diff --git a/src/ExprIntrp/ExprIntrp_yaccintrf.cxx b/src/ExprIntrp/ExprIntrp_yaccintrf.cxx index 272e989460..760a638aaa 100644 --- a/src/ExprIntrp/ExprIntrp_yaccintrf.cxx +++ b/src/ExprIntrp/ExprIntrp_yaccintrf.cxx @@ -12,9 +12,6 @@ // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. -#ifdef HAVE_CONFIG_H -# include -#endif #include #include #include @@ -49,39 +46,28 @@ #include #include #include -#include // ATTENTION POUR PROTECTION BUG STACK - +#include #include #include #include #include #include -#ifdef HAVE_STRINGS_H -# include -#endif - -extern char yysbuf[]; -extern char *yysptr; -static char ExprIntrp_assname[30]; -TCollection_AsciiString ExprIntrp_funcdefname; + +static TCollection_AsciiString ExprIntrp_assname; +static TCollection_AsciiString ExprIntrp_funcdefname; static Standard_Integer ExprIntrp_nbargs; static Standard_Integer ExprIntrp_nbdiff; - extern "C" void ExprIntrp_StartFunction() { - char funcname[100]; - ExprIntrp_GetResult(funcname); - TCollection_AsciiString name(funcname); + const TCollection_AsciiString& name = ExprIntrp_GetResult(); ExprIntrp_Recept.PushName(name); ExprIntrp_nbargs = 0; } extern "C" void ExprIntrp_StartDerivate() { - char funcname[100]; - ExprIntrp_GetResult(funcname); - TCollection_AsciiString name(funcname); + const TCollection_AsciiString& name = ExprIntrp_GetResult(); ExprIntrp_Recept.PushName(name); } @@ -95,9 +81,7 @@ extern "C" void ExprIntrp_EndDerivate() extern "C" void ExprIntrp_Derivation() { ExprIntrp_Recept.PushValue(1); - char name[100]; - ExprIntrp_GetResult(name); - TCollection_AsciiString thename(name); + const TCollection_AsciiString& thename = ExprIntrp_GetResult(); Handle(Expr_NamedExpression) namexp = ExprIntrp_Recept.GetNamed(thename); if (namexp.IsNull()) { namexp = new Expr_NamedUnknown(thename); @@ -110,11 +94,9 @@ extern "C" void ExprIntrp_Derivation() extern "C" void ExprIntrp_DerivationValue() { - char num[30]; - ExprIntrp_GetResult(num); - Standard_Integer degree = ExprIntrp_Recept.PopValue(); - degree = atoi(num); - ExprIntrp_Recept.PushValue(degree); + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + ExprIntrp_Recept.PopValue(); + ExprIntrp_Recept.PushValue(aStr.IntegerValue()); } extern "C" void ExprIntrp_EndDerivation() @@ -134,16 +116,11 @@ extern "C" void ExprIntrp_StartDifferential() extern "C" void ExprIntrp_DiffDegreeVar() { - char name[100]; - ExprIntrp_GetResult(name); -#ifndef WNT - if (strncasecmp(name,"X",1)) { -#else - if ( name[ 0 ] != 'X' && name[ 0 ] != 'x' ) { -#endif // WNT + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + const char* s = aStr.ToCString(); + if ( *s != 'X' && *s != 'x' ) { ExprIntrp_SyntaxError::Raise(); } - char* s = name; s++; Standard_Integer rank = atoi(s); ExprIntrp_Recept.PushValue(rank); @@ -158,17 +135,15 @@ extern "C" void ExprIntrp_DiffVar() extern "C" void ExprIntrp_DiffDegree() { - char name[100]; - ExprIntrp_GetResult(name); - Standard_Integer deg = atoi(name); + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + Standard_Integer deg = aStr.IntegerValue(); ExprIntrp_Recept.PushValue(deg); } extern "C" void ExprIntrp_VerDiffDegree() { - char name[100]; - ExprIntrp_GetResult(name); - Standard_Integer deg = atoi(name); + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + Standard_Integer deg = aStr.IntegerValue(); Standard_Integer thedeg = ExprIntrp_Recept.PopValue(); if (deg != thedeg) { ExprIntrp_SyntaxError::Raise(); @@ -457,9 +432,7 @@ extern "C" void ExprIntrp_UnaryMinusOperator() extern "C" void ExprIntrp_VariableIdentifier() { - char name[30]; - ExprIntrp_GetResult(name); - TCollection_AsciiString thename(name); + const TCollection_AsciiString& thename = ExprIntrp_GetResult(); Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename); if (nameexp.IsNull()) { nameexp = new Expr_NamedUnknown(thename); @@ -470,24 +443,20 @@ extern "C" void ExprIntrp_VariableIdentifier() extern "C" void ExprIntrp_NumValue() { - char num[30]; - int nbcar; - nbcar = ExprIntrp_GetResult(num); - Standard_Real value = Atof(num); + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + Standard_Real value = aStr.RealValue(); Handle(Expr_NumericValue) nval = new Expr_NumericValue(value); ExprIntrp_Recept.Push(nval); } extern "C" void ExprIntrp_AssignVariable() { - ExprIntrp_GetResult(ExprIntrp_assname); + ExprIntrp_assname = ExprIntrp_GetResult(); } extern "C" void ExprIntrp_Deassign() { - char name[100]; - ExprIntrp_GetResult(name); - TCollection_AsciiString thename(name); + const TCollection_AsciiString& thename = ExprIntrp_GetResult(); Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename); if (nameexp.IsNull()) { ExprIntrp_SyntaxError::Raise(); @@ -599,19 +568,15 @@ extern "C" void ExprIntrp_EndOfFuncDef() extern "C" void ExprIntrp_ConstantIdentifier() { - char name[100]; - ExprIntrp_GetResult(name); - TCollection_AsciiString thename(name); + const TCollection_AsciiString& thename = ExprIntrp_GetResult(); ExprIntrp_Recept.PushName(thename); } extern "C" void ExprIntrp_ConstantDefinition() { TCollection_AsciiString name = ExprIntrp_Recept.PopName(); - char num[30]; - int nbcar; - nbcar = ExprIntrp_GetResult(num); - Standard_Real val = Atof(num); + const TCollection_AsciiString& aStr = ExprIntrp_GetResult(); + Standard_Real val = aStr.RealValue(); Handle(Expr_NamedConstant) theconst = new Expr_NamedConstant(name,val); ExprIntrp_Recept.Use(theconst); ExprIntrp_Recept.Push(theconst); diff --git a/src/ExprIntrp/ExprIntrp_yaccintrf.hxx b/src/ExprIntrp/ExprIntrp_yaccintrf.hxx index 403a6559c4..046c4f2465 100644 --- a/src/ExprIntrp/ExprIntrp_yaccintrf.hxx +++ b/src/ExprIntrp/ExprIntrp_yaccintrf.hxx @@ -17,17 +17,32 @@ #ifndef ExprIntrp_yaccintrf_HeaderFile #define ExprIntrp_yaccintrf_HeaderFile -#include +#ifdef __cplusplus +extern "C" { +#endif -extern "C" int ExprIntrpparse(); -extern "C" void ExprIntrp_start_string(const char* str); -extern "C" void ExprIntrp_stop_string(); -extern TCollection_AsciiString ExprIntrp_thestring; -extern Standard_Integer ExprIntrp_thecurchar; -int ExprIntrp_GetDegree(); -int ExprIntrp_GetResult(char *s); +int ExprIntrpparse(); +void ExprIntrperror(char* msg); + +void ExprIntrp_start_string(const char* str); +void ExprIntrp_stop_string(); +void ExprIntrp_SetResult(); +void ExprIntrp_SetDegree(); + +int ExprIntrplex(void); + +#ifdef __cplusplus +} #endif +#ifdef __cplusplus +#include +const TCollection_AsciiString& ExprIntrp_GetResult (); +int ExprIntrp_GetDegree(); + +#endif + +#endif diff --git a/src/ExprIntrp/ExprIntrp_yacclex.cxx b/src/ExprIntrp/ExprIntrp_yacclex.cxx index 01c98e814a..596ed77136 100644 --- a/src/ExprIntrp/ExprIntrp_yacclex.cxx +++ b/src/ExprIntrp/ExprIntrp_yacclex.cxx @@ -17,7 +17,7 @@ #include #include -static char ExprIntrp_curres[255]; +static TCollection_AsciiString ExprIntrp_curres; static int ExprIntrp_degree; #ifndef WNT @@ -29,7 +29,7 @@ extern "C" char* ExprIntrptext; extern "C" void ExprIntrp_SetResult() { - strcpy(ExprIntrp_curres,ExprIntrptext); + ExprIntrp_curres = ExprIntrptext; } extern "C" void ExprIntrp_SetDegree() @@ -42,8 +42,7 @@ int ExprIntrp_GetDegree() return ExprIntrp_degree; } -int ExprIntrp_GetResult(char *s) +const TCollection_AsciiString& ExprIntrp_GetResult () { - strcpy(s,ExprIntrp_curres); - return (int)strlen(ExprIntrp_curres); + return ExprIntrp_curres; } diff --git a/src/ExprIntrp/FILES b/src/ExprIntrp/FILES index 0ac7f81b3a..43ce623c80 100755 --- a/src/ExprIntrp/FILES +++ b/src/ExprIntrp/FILES @@ -1,4 +1,3 @@ -ExprIntrp_CMPLRS.edl ExprIntrp.lex ExprIntrp.yacc ExprIntrp_yaccanal.hxx diff --git a/src/StepFile/step.lex b/src/StepFile/step.lex index ada8492b97..94179ea56b 100644 --- a/src/StepFile/step.lex +++ b/src/StepFile/step.lex @@ -44,7 +44,12 @@ void rec_typarg(int argtype); #ifdef _MSC_VER // disable MSVC warnings in flex code +// Note that Intel compiler also defines _MSC_VER but has different warning ids +#if defined(__INTEL_COMPILER) +#pragma warning(disable:177 1786 1736) +#else #pragma warning(disable:4131 4244 4273 4267 4127) +#endif // Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35) #define YY_NO_UNISTD_H diff --git a/tests/bugs/fclasses/bug24897 b/tests/bugs/fclasses/bug24897 new file mode 100755 index 0000000000..f0bf337bc8 --- /dev/null +++ b/tests/bugs/fclasses/bug24897 @@ -0,0 +1,11 @@ +puts "============" +puts "OCC24897" +puts "============" +puts "" +####################################################################### +# Possibility of uncontrolled exit if scanner fails in ExprIntrp +####################################################################### + +pload QAcommands + +CR23403 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa