]> OCCT Git - occt.git/commitdiff
0029925: Foundation Classes - add missing cast to LowerCase() and UpperCase() arguments
authorkgv <kgv@opencascade.com>
Mon, 2 Jul 2018 21:45:41 +0000 (00:45 +0300)
committerbugmaster <bugmaster@opencascade.com>
Fri, 6 Jul 2018 12:45:01 +0000 (15:45 +0300)
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).

src/QABugs/QABugs_20.cxx
src/Standard/Standard_Character.hxx
tests/bugs/fclasses/bug29925 [new file with mode: 0644]

index 8caea5e40dec4efcd8f85a8ef4904b5e0f02f957..a5c507b1aa81d7c57ff1b4bc087df3d666a495c6 100644 (file)
@@ -62,6 +62,8 @@
 #include <HLRBRep_PolyHLRToShape.hxx>
 #include <HLRBRep_PolyAlgo.hxx>
 
+#include <Standard_Failure.hxx>
+
 #include <limits>
 
 //=======================================================================
@@ -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 <step file name>", __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;
 }
index d34e15635a48c4556373738191a1a57326d52193..b64357935fbb62d2349c7484ef54decbc8146b97 100644 (file)
@@ -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 (file)
index 0000000..1d8f8d1
--- /dev/null
@@ -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