From: kgv Date: Mon, 2 Jul 2018 21:45:41 +0000 (+0300) Subject: 0029925: Foundation Classes - add missing cast to LowerCase() and UpperCase() arguments X-Git-Tag: OCCT_VC2017_73~88 X-Git-Url: http://git.dev.opencascade.org/gitweb/?a=commitdiff_plain;h=d9d3107d8df19cacc3d7a0cd46ba51deee0bf6f2;p=occt.git 0029925: Foundation Classes - add missing cast to LowerCase() and UpperCase() arguments Argument of LowerCase() and UpperCase() is cast to int via unsigned char to avoid passing negative integer in the case if the argument char is in the extended part of ASCII table (which would result in undefined behavior according to C++ standard). --- diff --git a/src/QABugs/QABugs_20.cxx b/src/QABugs/QABugs_20.cxx index 8caea5e40d..a5c507b1aa 100644 --- a/src/QABugs/QABugs_20.cxx +++ b/src/QABugs/QABugs_20.cxx @@ -62,6 +62,8 @@ #include #include +#include + #include //======================================================================= @@ -2889,6 +2891,55 @@ static Standard_Integer OCC29531(Draw_Interpretor&, Standard_Integer, const char return 0; } +//======================================================================= +//function : OCC29925 +//purpose : check safety of functions like IsSpace(), LowerCase(), etc. for all chars +//======================================================================= +static Standard_Integer OCC29925 (Draw_Interpretor& theDI, Standard_Integer, const char**) +{ + // iterate by all valid ASCII chars (including extended) + for (int i = 0; i < 256; i++) + { + Standard_Character c = (char)(unsigned char)i; +// if (c != i) theDI << c << " != " << i << "\n"; + const char* anOp = ""; + try { + anOp = "IsAlphabetic"; + IsAlphabetic (c); + anOp = "IsDigit"; + IsDigit (c); + anOp = "IsXDigit"; + IsXDigit (c); + anOp = "IsAlphanumeric"; + IsAlphanumeric (c); + anOp = "IsControl"; + IsControl (c); + anOp = "IsGraphic"; + IsGraphic (c); + anOp = "IsLowerCase"; + IsLowerCase (c); + anOp = "IsPrintable"; + IsPrintable (c); + anOp = "IsPunctuation"; + IsPunctuation (c); + anOp = "IsSpace"; + IsSpace (c); + anOp = "IsUpperCase"; + IsUpperCase (c); + anOp = "LowerCase"; + LowerCase (c); + anOp = "UpperCase"; + UpperCase (c); + } + catch (const Handle(Standard_Failure)& e) + { + theDI << anOp << "() fails for " << c << " (" << e->DynamicType()->Name() << ")\n"; + } + } + + return 0; +} + void QABugs::Commands_20(Draw_Interpretor& theCommands) { const char *group = "QABugs"; @@ -2925,5 +2976,6 @@ void QABugs::Commands_20(Draw_Interpretor& theCommands) { theCommands.Add("OCC29531", "OCC29531 ", __FILE__, OCC29531, group); theCommands.Add ("OCC29064", "OCC29064: test memory usage by copying empty maps", __FILE__, OCC29064, group); + theCommands.Add ("OCC29925", "OCC29925: check safety of character classification functions", __FILE__, OCC29925, group); return; } diff --git a/src/Standard/Standard_Character.hxx b/src/Standard/Standard_Character.hxx index d34e15635a..b64357935f 100644 --- a/src/Standard/Standard_Character.hxx +++ b/src/Standard/Standard_Character.hxx @@ -123,12 +123,12 @@ inline Standard_Boolean IsUpperCase(const Standard_Character me) // LowerCase : Returns a lowercase character // ================================================================== inline Standard_Character LowerCase(const Standard_Character me) -{ return (Standard_Character)(unsigned char)std::tolower(me); } +{ return (Standard_Character)(unsigned char)std::tolower((unsigned char)me); } // ================================================================== // UpperCase : Returns a uppercase character // ================================================================== inline Standard_Character UpperCase(const Standard_Character me) -{ return (Standard_Character)(unsigned char)std::toupper(me); } +{ return (Standard_Character)(unsigned char)std::toupper((unsigned char)me); } #endif diff --git a/tests/bugs/fclasses/bug29925 b/tests/bugs/fclasses/bug29925 new file mode 100644 index 0000000000..1d8f8d1cc2 --- /dev/null +++ b/tests/bugs/fclasses/bug29925 @@ -0,0 +1,9 @@ +puts "# ======================================================================" +puts "# 0029925: Foundation Classes - add missing cast to LowerCase() and UpperCase() arguments" +puts "# ======================================================================" +puts "" + +pload QAcommands + +puts "Check safety of character classification functions for all valid chars" +OCC29925