1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-10 18:51:21 +03:00

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
This commit is contained in:
abv 2014-05-29 15:05:47 +04:00 committed by apn
parent cae42e78e5
commit 2a54ebbf29
10 changed files with 87 additions and 112 deletions

View File

@ -20,13 +20,11 @@
#include <Standard_ErrorHandler.hxx> #include <Standard_ErrorHandler.hxx>
#include <ExprIntrp_SyntaxError.hxx> #include <ExprIntrp_SyntaxError.hxx>
TCollection_AsciiString ExprIntrp_thestring; static TCollection_AsciiString ExprIntrp_thestring;
Standard_Integer ExprIntrp_thecurchar=0;
Standard_Boolean ExprIntrp::Parse(const Handle(ExprIntrp_Generator)& gen, const TCollection_AsciiString& str) Standard_Boolean ExprIntrp::Parse(const Handle(ExprIntrp_Generator)& gen, const TCollection_AsciiString& str)
{ {
ExprIntrp_Recept.SetMaster(gen); ExprIntrp_Recept.SetMaster(gen);
ExprIntrp_thecurchar = 0;
if (str.Length() == 0) return Standard_False; if (str.Length() == 0) return Standard_False;
ExprIntrp_thestring = str; ExprIntrp_thestring = str;
ExprIntrp_start_string(ExprIntrp_thestring.ToCString()); ExprIntrp_start_string(ExprIntrp_thestring.ToCString());

View File

@ -19,18 +19,16 @@
%{ %{
#include <ExprIntrp.tab.h> #include <ExprIntrp.tab.h>
#include <ExprIntrp_yaccintrf.hxx>
#define YY_SKIP_YYWRAP #define YY_SKIP_YYWRAP
static YY_BUFFER_STATE ExprIntrp_bufstring; static YY_BUFFER_STATE ExprIntrp_bufstring;
void ExprIntrp_SetResult(); void ExprIntrp_start_string(const char* str)
void ExprIntrp_SetDegree();
int ExprIntrlex (void);
void ExprIntrp_start_string(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() void ExprIntrp_stop_string()
@ -39,11 +37,14 @@ void ExprIntrp_stop_string()
ExprIntrp_bufstring = (YY_BUFFER_STATE) 0; ExprIntrp_bufstring = (YY_BUFFER_STATE) 0;
} }
int yywrap() static int yywrap()
{ {
return 1; return 1;
} }
// provide safe error handler (exception instead of exit())
#define YY_FATAL_ERROR(msg) ExprIntrperror(msg)
#ifdef _MSC_VER #ifdef _MSC_VER
// add includes for flex 2.91 (Linux version) // add includes for flex 2.91 (Linux version)
#include <stdlib.h> #include <stdlib.h>
@ -52,8 +53,14 @@ int yywrap()
// Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35) // Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35)
#define YY_NO_UNISTD_H #define YY_NO_UNISTD_H
// disable MSVC warnings in flex 2.89 code // disable MSVC warnings in flex 2.89 and 2.5.35 code
#pragma warning(disable:4131 4244 4273 4127) // 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 #endif
#ifdef __GNUC__ #ifdef __GNUC__

View File

@ -15,6 +15,8 @@
*/ */
%{ %{
#include <ExprIntrp_yaccintrf.hxx>
extern void ExprIntrp_EndOfFuncDef(); extern void ExprIntrp_EndOfFuncDef();
extern void ExprIntrp_EndOfRelation(); extern void ExprIntrp_EndOfRelation();
extern void ExprIntrp_AssignVariable(); extern void ExprIntrp_AssignVariable();
@ -59,9 +61,6 @@ extern void ExprIntrp_VariableIdentifier();
extern void ExprIntrp_Productor(); extern void ExprIntrp_Productor();
extern void ExprIntrp_EndOfEqual(); extern void ExprIntrp_EndOfEqual();
extern void ExprIntrperror(char* msg);
extern int ExprIntrplex(void);
// disable MSVC warnings in bison code // disable MSVC warnings in bison code
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable:4131 4244 4127 4702) #pragma warning(disable:4131 4244 4127 4702)

View File

@ -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;

View File

@ -12,9 +12,6 @@
// Alternatively, this file may be used under the terms of Open CASCADE // Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement. // commercial license or contractual agreement.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ExprIntrp_yaccintrf.hxx> #include <ExprIntrp_yaccintrf.hxx>
#include <ExprIntrp_yaccanal.hxx> #include <ExprIntrp_yaccanal.hxx>
#include <Expr_GeneralExpression.hxx> #include <Expr_GeneralExpression.hxx>
@ -49,39 +46,28 @@
#include <Expr_SystemRelation.hxx> #include <Expr_SystemRelation.hxx>
#include <Expr_UnknownIterator.hxx> #include <Expr_UnknownIterator.hxx>
#include <Expr_FunctionDerivative.hxx> #include <Expr_FunctionDerivative.hxx>
#include <Expr.hxx> // ATTENTION POUR PROTECTION BUG STACK #include <Expr.hxx>
#include <Expr_SequenceOfGeneralExpression.hxx> #include <Expr_SequenceOfGeneralExpression.hxx>
#include <Expr_Operators.hxx> #include <Expr_Operators.hxx>
#include <ExprIntrp_SyntaxError.hxx> #include <ExprIntrp_SyntaxError.hxx>
#include <Expr_Array1OfNamedUnknown.hxx> #include <Expr_Array1OfNamedUnknown.hxx>
#include <Expr_Array1OfGeneralExpression.hxx> #include <Expr_Array1OfGeneralExpression.hxx>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
extern char yysbuf[]; static TCollection_AsciiString ExprIntrp_assname;
extern char *yysptr; static TCollection_AsciiString ExprIntrp_funcdefname;
static char ExprIntrp_assname[30];
TCollection_AsciiString ExprIntrp_funcdefname;
static Standard_Integer ExprIntrp_nbargs; static Standard_Integer ExprIntrp_nbargs;
static Standard_Integer ExprIntrp_nbdiff; static Standard_Integer ExprIntrp_nbdiff;
extern "C" void ExprIntrp_StartFunction() extern "C" void ExprIntrp_StartFunction()
{ {
char funcname[100]; const TCollection_AsciiString& name = ExprIntrp_GetResult();
ExprIntrp_GetResult(funcname);
TCollection_AsciiString name(funcname);
ExprIntrp_Recept.PushName(name); ExprIntrp_Recept.PushName(name);
ExprIntrp_nbargs = 0; ExprIntrp_nbargs = 0;
} }
extern "C" void ExprIntrp_StartDerivate() extern "C" void ExprIntrp_StartDerivate()
{ {
char funcname[100]; const TCollection_AsciiString& name = ExprIntrp_GetResult();
ExprIntrp_GetResult(funcname);
TCollection_AsciiString name(funcname);
ExprIntrp_Recept.PushName(name); ExprIntrp_Recept.PushName(name);
} }
@ -95,9 +81,7 @@ extern "C" void ExprIntrp_EndDerivate()
extern "C" void ExprIntrp_Derivation() extern "C" void ExprIntrp_Derivation()
{ {
ExprIntrp_Recept.PushValue(1); ExprIntrp_Recept.PushValue(1);
char name[100]; const TCollection_AsciiString& thename = ExprIntrp_GetResult();
ExprIntrp_GetResult(name);
TCollection_AsciiString thename(name);
Handle(Expr_NamedExpression) namexp = ExprIntrp_Recept.GetNamed(thename); Handle(Expr_NamedExpression) namexp = ExprIntrp_Recept.GetNamed(thename);
if (namexp.IsNull()) { if (namexp.IsNull()) {
namexp = new Expr_NamedUnknown(thename); namexp = new Expr_NamedUnknown(thename);
@ -110,11 +94,9 @@ extern "C" void ExprIntrp_Derivation()
extern "C" void ExprIntrp_DerivationValue() extern "C" void ExprIntrp_DerivationValue()
{ {
char num[30]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
ExprIntrp_GetResult(num); ExprIntrp_Recept.PopValue();
Standard_Integer degree = ExprIntrp_Recept.PopValue(); ExprIntrp_Recept.PushValue(aStr.IntegerValue());
degree = atoi(num);
ExprIntrp_Recept.PushValue(degree);
} }
extern "C" void ExprIntrp_EndDerivation() extern "C" void ExprIntrp_EndDerivation()
@ -134,16 +116,11 @@ extern "C" void ExprIntrp_StartDifferential()
extern "C" void ExprIntrp_DiffDegreeVar() extern "C" void ExprIntrp_DiffDegreeVar()
{ {
char name[100]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
ExprIntrp_GetResult(name); const char* s = aStr.ToCString();
#ifndef WNT if ( *s != 'X' && *s != 'x' ) {
if (strncasecmp(name,"X",1)) {
#else
if ( name[ 0 ] != 'X' && name[ 0 ] != 'x' ) {
#endif // WNT
ExprIntrp_SyntaxError::Raise(); ExprIntrp_SyntaxError::Raise();
} }
char* s = name;
s++; s++;
Standard_Integer rank = atoi(s); Standard_Integer rank = atoi(s);
ExprIntrp_Recept.PushValue(rank); ExprIntrp_Recept.PushValue(rank);
@ -158,17 +135,15 @@ extern "C" void ExprIntrp_DiffVar()
extern "C" void ExprIntrp_DiffDegree() extern "C" void ExprIntrp_DiffDegree()
{ {
char name[100]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
ExprIntrp_GetResult(name); Standard_Integer deg = aStr.IntegerValue();
Standard_Integer deg = atoi(name);
ExprIntrp_Recept.PushValue(deg); ExprIntrp_Recept.PushValue(deg);
} }
extern "C" void ExprIntrp_VerDiffDegree() extern "C" void ExprIntrp_VerDiffDegree()
{ {
char name[100]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
ExprIntrp_GetResult(name); Standard_Integer deg = aStr.IntegerValue();
Standard_Integer deg = atoi(name);
Standard_Integer thedeg = ExprIntrp_Recept.PopValue(); Standard_Integer thedeg = ExprIntrp_Recept.PopValue();
if (deg != thedeg) { if (deg != thedeg) {
ExprIntrp_SyntaxError::Raise(); ExprIntrp_SyntaxError::Raise();
@ -457,9 +432,7 @@ extern "C" void ExprIntrp_UnaryMinusOperator()
extern "C" void ExprIntrp_VariableIdentifier() extern "C" void ExprIntrp_VariableIdentifier()
{ {
char name[30]; const TCollection_AsciiString& thename = ExprIntrp_GetResult();
ExprIntrp_GetResult(name);
TCollection_AsciiString thename(name);
Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename); Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename);
if (nameexp.IsNull()) { if (nameexp.IsNull()) {
nameexp = new Expr_NamedUnknown(thename); nameexp = new Expr_NamedUnknown(thename);
@ -470,24 +443,20 @@ extern "C" void ExprIntrp_VariableIdentifier()
extern "C" void ExprIntrp_NumValue() extern "C" void ExprIntrp_NumValue()
{ {
char num[30]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
int nbcar; Standard_Real value = aStr.RealValue();
nbcar = ExprIntrp_GetResult(num);
Standard_Real value = Atof(num);
Handle(Expr_NumericValue) nval = new Expr_NumericValue(value); Handle(Expr_NumericValue) nval = new Expr_NumericValue(value);
ExprIntrp_Recept.Push(nval); ExprIntrp_Recept.Push(nval);
} }
extern "C" void ExprIntrp_AssignVariable() extern "C" void ExprIntrp_AssignVariable()
{ {
ExprIntrp_GetResult(ExprIntrp_assname); ExprIntrp_assname = ExprIntrp_GetResult();
} }
extern "C" void ExprIntrp_Deassign() extern "C" void ExprIntrp_Deassign()
{ {
char name[100]; const TCollection_AsciiString& thename = ExprIntrp_GetResult();
ExprIntrp_GetResult(name);
TCollection_AsciiString thename(name);
Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename); Handle(Expr_NamedExpression) nameexp = ExprIntrp_Recept.GetNamed(thename);
if (nameexp.IsNull()) { if (nameexp.IsNull()) {
ExprIntrp_SyntaxError::Raise(); ExprIntrp_SyntaxError::Raise();
@ -599,19 +568,15 @@ extern "C" void ExprIntrp_EndOfFuncDef()
extern "C" void ExprIntrp_ConstantIdentifier() extern "C" void ExprIntrp_ConstantIdentifier()
{ {
char name[100]; const TCollection_AsciiString& thename = ExprIntrp_GetResult();
ExprIntrp_GetResult(name);
TCollection_AsciiString thename(name);
ExprIntrp_Recept.PushName(thename); ExprIntrp_Recept.PushName(thename);
} }
extern "C" void ExprIntrp_ConstantDefinition() extern "C" void ExprIntrp_ConstantDefinition()
{ {
TCollection_AsciiString name = ExprIntrp_Recept.PopName(); TCollection_AsciiString name = ExprIntrp_Recept.PopName();
char num[30]; const TCollection_AsciiString& aStr = ExprIntrp_GetResult();
int nbcar; Standard_Real val = aStr.RealValue();
nbcar = ExprIntrp_GetResult(num);
Standard_Real val = Atof(num);
Handle(Expr_NamedConstant) theconst = new Expr_NamedConstant(name,val); Handle(Expr_NamedConstant) theconst = new Expr_NamedConstant(name,val);
ExprIntrp_Recept.Use(theconst); ExprIntrp_Recept.Use(theconst);
ExprIntrp_Recept.Push(theconst); ExprIntrp_Recept.Push(theconst);

View File

@ -17,17 +17,32 @@
#ifndef ExprIntrp_yaccintrf_HeaderFile #ifndef ExprIntrp_yaccintrf_HeaderFile
#define ExprIntrp_yaccintrf_HeaderFile #define ExprIntrp_yaccintrf_HeaderFile
#ifdef __cplusplus
extern "C" {
#endif
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 <TCollection_AsciiString.hxx> #include <TCollection_AsciiString.hxx>
extern "C" int ExprIntrpparse(); const TCollection_AsciiString& ExprIntrp_GetResult ();
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_GetDegree();
int ExprIntrp_GetResult(char *s);
#endif #endif
#endif

View File

@ -17,7 +17,7 @@
#include <string.h> #include <string.h>
#include <ExprIntrp_yaccintrf.hxx> #include <ExprIntrp_yaccintrf.hxx>
static char ExprIntrp_curres[255]; static TCollection_AsciiString ExprIntrp_curres;
static int ExprIntrp_degree; static int ExprIntrp_degree;
#ifndef WNT #ifndef WNT
@ -29,7 +29,7 @@ extern "C" char* ExprIntrptext;
extern "C" void ExprIntrp_SetResult() extern "C" void ExprIntrp_SetResult()
{ {
strcpy(ExprIntrp_curres,ExprIntrptext); ExprIntrp_curres = ExprIntrptext;
} }
extern "C" void ExprIntrp_SetDegree() extern "C" void ExprIntrp_SetDegree()
@ -42,8 +42,7 @@ int ExprIntrp_GetDegree()
return ExprIntrp_degree; return ExprIntrp_degree;
} }
int ExprIntrp_GetResult(char *s) const TCollection_AsciiString& ExprIntrp_GetResult ()
{ {
strcpy(s,ExprIntrp_curres); return ExprIntrp_curres;
return (int)strlen(ExprIntrp_curres);
} }

View File

@ -1,4 +1,3 @@
ExprIntrp_CMPLRS.edl
ExprIntrp.lex ExprIntrp.lex
ExprIntrp.yacc ExprIntrp.yacc
ExprIntrp_yaccanal.hxx ExprIntrp_yaccanal.hxx

View File

@ -44,7 +44,12 @@ void rec_typarg(int argtype);
#ifdef _MSC_VER #ifdef _MSC_VER
// disable MSVC warnings in flex code // 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) #pragma warning(disable:4131 4244 4273 4267 4127)
#endif
// Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35) // Avoid includion of unistd.h if parser is generated on Linux (flex 2.5.35)
#define YY_NO_UNISTD_H #define YY_NO_UNISTD_H

11
tests/bugs/fclasses/bug24897 Executable file
View File

@ -0,0 +1,11 @@
puts "============"
puts "OCC24897"
puts "============"
puts ""
#######################################################################
# Possibility of uncontrolled exit if scanner fails in ExprIntrp
#######################################################################
pload QAcommands
CR23403 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa