]> OCCT Git - occt.git/commitdiff
0032903: Coding Rules - eliminate MSVC warning C26451 on VS2019/C++20
authorddzama <ddzama@opencascade.com>
Tue, 29 Mar 2022 13:32:46 +0000 (16:32 +0300)
committersmoskvin <smoskvin@opencascade.com>
Tue, 26 Apr 2022 21:59:45 +0000 (00:59 +0300)
Put explicit type casting to avoid:
Warning C26451 Arithmetic overflow: Using operator '-' on a 4 byte
value and then casting the result to a 8 byte value.
Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

src/AIS/AIS_ViewController.cxx
src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx
src/Message/Message_ProgressScope.hxx
src/Standard/Standard_Real.hxx

index 171b48e1e84bea676e894ef1de46ccf1ee7d1e46..8ce7822d48baf9a969a1e668133441d710ac96fa 100644 (file)
@@ -1010,7 +1010,8 @@ bool AIS_ViewController::UpdateMousePosition (const Graphic3d_Vec2i& thePoint,
       const double aRotTol = theIsEmulated
                            ? double(myTouchToleranceScale) * myTouchRotationThresholdPx
                            : 0.0;
-      if (double (Abs (aDelta.x()) + Abs (aDelta.y())) > aRotTol)
+      const Graphic3d_Vec2d aDeltaF (aDelta);
+      if (Abs (aDeltaF.x()) + Abs (aDeltaF.y()) > aRotTol)
       {
         const double aRotAccel = myNavigationMode == AIS_NavigationMode_FirstPersonWalk ? myMouseAccel : myOrbitAccel;
         const Graphic3d_Vec2i aRotDelta = thePoint - myMousePressPoint;
@@ -1063,7 +1064,8 @@ bool AIS_ViewController::UpdateMousePosition (const Graphic3d_Vec2i& thePoint,
       const double aPanTol = theIsEmulated
                            ? double(myTouchToleranceScale) * myTouchPanThresholdPx
                            : 0.0;
-      if (double (Abs (aDelta.x()) + Abs (aDelta.y())) > aPanTol)
+      const Graphic3d_Vec2d aDeltaF (aDelta);
+      if (Abs (aDeltaF.x()) + Abs (aDeltaF.y()) > aPanTol)
       {
         if (myUpdateStartPointPan)
         {
@@ -1629,10 +1631,11 @@ void AIS_ViewController::handleZoom (const Handle(V3d_View)& theView,
 
     Graphic3d_Vec2i aWinSize;
     theView->Window()->Size (aWinSize.x(), aWinSize.y());
-    const Graphic3d_Vec2d aPanFromCenterPx (double(theParams.Point.x()) - 0.5 * double(aWinSize.x()),
-                                            double(aWinSize.y() - theParams.Point.y() - 1) - 0.5 * double(aWinSize.y()));
-    aDxy.x() += -aViewDims1.X() * aPanFromCenterPx.x() / double(aWinSize.x());
-    aDxy.y() += -aViewDims1.Y() * aPanFromCenterPx.y() / double(aWinSize.y());
+    const Graphic3d_Vec2d aWinSizeF (aWinSize);
+    const Graphic3d_Vec2d aPanFromCenterPx (double(theParams.Point.x()) - 0.5 * aWinSizeF.x(),
+                                            aWinSizeF.y() - double(theParams.Point.y()) - 1.0 - 0.5 * aWinSizeF.y());
+    aDxy.x() += -aViewDims1.X() * aPanFromCenterPx.x() / aWinSizeF.x();
+    aDxy.y() += -aViewDims1.Y() * aPanFromCenterPx.y() / aWinSizeF.y();
   }
 
   //theView->Translate (aCam, aDxy.x(), aDxy.y());
index f889f4dffceebd5771575e74751d05eb1671daf8..4d1daa5e4908e681f699cce1b23f8cdc724b6d98 100644 (file)
@@ -315,7 +315,7 @@ public:
   void SetVertice (const Standard_Integer theIndex, const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ)
   {
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
-    Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3*> (myAttribs->ChangeData() + myPosStride * (theIndex - 1));
+    Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3*> (myAttribs->ChangeData() + myPosStride * ((Standard_Size)theIndex - 1));
     aVec.x() = theX;
     aVec.y() = theY;
     aVec.z() = theZ;
@@ -343,7 +343,7 @@ public:
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
     if (myColData != NULL)
     {
-      Graphic3d_Vec4ub* aColorPtr = reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
+      Graphic3d_Vec4ub* aColorPtr = reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * ((Standard_Size)theIndex - 1));
       aColorPtr->SetValues (Standard_Byte(theR * 255.0),
                             Standard_Byte(theG * 255.0),
                             Standard_Byte(theB * 255.0), 255);
@@ -360,7 +360,7 @@ public:
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
     if (myColData != NULL)
     {
-      Graphic3d_Vec4ub* aColorPtr =  reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
+      Graphic3d_Vec4ub* aColorPtr =  reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * ((Standard_Size)theIndex - 1));
       (*aColorPtr) = theColor;
     }
     myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
@@ -377,7 +377,7 @@ public:
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
     if (myColData != NULL)
     {
-      *reinterpret_cast<Standard_Integer* >(myColData + myColStride * (theIndex - 1)) = theColor32;
+      *reinterpret_cast<Standard_Integer* >(myColData + myColStride * ((Standard_Size)theIndex - 1)) = theColor32;
     }
   }
 
@@ -399,7 +399,7 @@ public:
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
     if (myNormData != NULL)
     {
-      Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3* >(myNormData + myNormStride * (theIndex - 1));
+      Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3* >(myNormData + myNormStride * ((Standard_Size)theIndex - 1));
       aVec.x() = Standard_ShortReal (theNX);
       aVec.y() = Standard_ShortReal (theNY);
       aVec.z() = Standard_ShortReal (theNZ);
@@ -424,7 +424,7 @@ public:
     Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
     if (myTexData != NULL)
     {
-      Graphic3d_Vec2& aVec = *reinterpret_cast<Graphic3d_Vec2* >(myTexData + myTexStride * (theIndex - 1));
+      Graphic3d_Vec2& aVec = *reinterpret_cast<Graphic3d_Vec2* >(myTexData + myTexStride * ((Standard_Size)theIndex - 1));
       aVec.x() = Standard_ShortReal (theTX);
       aVec.y() = Standard_ShortReal (theTY);
     }
@@ -450,7 +450,7 @@ public:
   {
     theX = theY = theZ = 0.0;
     Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
-    const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3*> (myAttribs->Data() + myPosStride * (theRank - 1));
+    const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3*> (myAttribs->Data() + myPosStride * ((Standard_Size)theRank - 1));
     theX = Standard_Real(aVec.x());
     theY = Standard_Real(aVec.y());
     theZ = Standard_Real(aVec.z());
@@ -473,7 +473,7 @@ public:
                     Graphic3d_Vec4ub&      theColor) const
   {
     Standard_OutOfRange_Raise_if (myColData == NULL || theIndex < 1 || theIndex > myAttribs->NbElements, "BAD VERTEX index");
-    theColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
+    theColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * ((Standard_Size)theIndex - 1));
   }
 
   //! Returns the vertex color values from the vertex table if defined.
@@ -489,7 +489,7 @@ public:
     {
       return;
     }
-    const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * (theRank - 1));
+    const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * ((Standard_Size)theRank - 1));
     theR = Standard_Real(aColor.r()) / 255.0;
     theG = Standard_Real(aColor.g()) / 255.0;
     theB = Standard_Real(aColor.b()) / 255.0;
@@ -503,7 +503,7 @@ public:
     Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
     if (myColData != NULL)
     {
-      theColor = *reinterpret_cast<const Standard_Integer* >(myColData + myColStride * (theRank - 1));
+      theColor = *reinterpret_cast<const Standard_Integer* >(myColData + myColStride * ((Standard_Size)theRank - 1));
     }
   }
 
@@ -528,7 +528,7 @@ public:
     Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
     if (myNormData != NULL)
     {
-      const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3* >(myNormData + myNormStride * (theRank - 1));
+      const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3* >(myNormData + myNormStride * ((Standard_Size)theRank - 1));
       theNX = Standard_Real(aVec.x());
       theNY = Standard_Real(aVec.y());
       theNZ = Standard_Real(aVec.z());
@@ -555,7 +555,7 @@ public:
     Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
     if (myTexData != NULL)
     {
-      const Graphic3d_Vec2& aVec = *reinterpret_cast<const Graphic3d_Vec2* >(myTexData + myTexStride * (theRank - 1));
+      const Graphic3d_Vec2& aVec = *reinterpret_cast<const Graphic3d_Vec2* >(myTexData + myTexStride * ((Standard_Size)theRank - 1));
       theTX = Standard_Real(aVec.x());
       theTY = Standard_Real(aVec.y());
     }
index 5652e6859b6ed6aadffa3b7b9ce813130da2448e..1041f401d1005324394c814b03805a9f01c20560 100644 (file)
@@ -271,7 +271,7 @@ public: //! @name Preparation methods
     if (!theName.IsEmpty())
     {
       myIsOwnName = true;
-      myName = (char* )Standard::Allocate (theName.Length() + 1);
+      myName = (char* )Standard::Allocate (Standard_Size(theName.Length()) + Standard_Size(1));
       char* aName = (char* )myName;
       memcpy (aName, theName.ToCString(), theName.Length());
       aName[theName.Length()] = '\0';
index 767c9c18e35cb009249fa0b5ce4a9278db6a9f04..43a95d717db702798f2670334f86e8f7e9e57172 100644 (file)
@@ -253,14 +253,16 @@ inline  Standard_Real    RealPart (const Standard_Real Value)
 //             If input value is out of valid range for integers,
 //             minimal or maximal possible integer is returned.
 //-------------------------------------------------------------------
-inline  Standard_Integer RealToInt (const Standard_Real Value) 
+inline Standard_Integer RealToInt (const Standard_Real theValue)
 { 
   // Note that on WNT under MS VC++ 8.0 conversion of double value less 
   // than INT_MIN or greater than INT_MAX to integer will cause signal 
   // "Floating point multiple trap" (OCC17861)
-  return Value < INT_MIN ? INT_MIN
-    : Value > INT_MAX ? INT_MAX
-    : (Standard_Integer)Value;
+  return theValue < static_cast<Standard_Real>(INT_MIN)
+       ? static_cast<Standard_Integer>(INT_MIN)
+       : (theValue > static_cast<Standard_Real>(INT_MAX)
+        ? static_cast<Standard_Integer>(INT_MAX)
+        : static_cast<Standard_Integer>(theValue));
 }
 
 // =======================================================================