From 4e1b5fcbf0cd7ec736e4d9c0d5683dd013c752a1 Mon Sep 17 00:00:00 2001 From: ddzama Date: Tue, 29 Mar 2022 16:32:46 +0300 Subject: [PATCH] 0032903: Coding Rules - eliminate MSVC warning C26451 on VS2019/C++20 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 | 15 +++++++----- src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx | 24 +++++++++---------- src/Message/Message_ProgressScope.hxx | 2 +- src/Standard/Standard_Real.hxx | 10 ++++---- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/AIS/AIS_ViewController.cxx b/src/AIS/AIS_ViewController.cxx index 171b48e1e8..8ce7822d48 100644 --- a/src/AIS/AIS_ViewController.cxx +++ b/src/AIS/AIS_ViewController.cxx @@ -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()); diff --git a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx index f889f4dffc..4d1daa5e49 100644 --- a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx +++ b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx @@ -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 (myAttribs->ChangeData() + myPosStride * (theIndex - 1)); + Graphic3d_Vec3& aVec = *reinterpret_cast (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(myColData + myColStride * (theIndex - 1)); + Graphic3d_Vec4ub* aColorPtr = reinterpret_cast(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(myColData + myColStride * (theIndex - 1)); + Graphic3d_Vec4ub* aColorPtr = reinterpret_cast(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(myColData + myColStride * (theIndex - 1)) = theColor32; + *reinterpret_cast(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(myNormData + myNormStride * (theIndex - 1)); + Graphic3d_Vec3& aVec = *reinterpret_cast(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(myTexData + myTexStride * (theIndex - 1)); + Graphic3d_Vec2& aVec = *reinterpret_cast(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 (myAttribs->Data() + myPosStride * (theRank - 1)); + const Graphic3d_Vec3& aVec = *reinterpret_cast (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(myColData + myColStride * (theIndex - 1)); + theColor = *reinterpret_cast(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(myColData + myColStride * (theRank - 1)); + const Graphic3d_Vec4ub& aColor = *reinterpret_cast(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(myColData + myColStride * (theRank - 1)); + theColor = *reinterpret_cast(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(myNormData + myNormStride * (theRank - 1)); + const Graphic3d_Vec3& aVec = *reinterpret_cast(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(myTexData + myTexStride * (theRank - 1)); + const Graphic3d_Vec2& aVec = *reinterpret_cast(myTexData + myTexStride * ((Standard_Size)theRank - 1)); theTX = Standard_Real(aVec.x()); theTY = Standard_Real(aVec.y()); } diff --git a/src/Message/Message_ProgressScope.hxx b/src/Message/Message_ProgressScope.hxx index 5652e6859b..1041f401d1 100644 --- a/src/Message/Message_ProgressScope.hxx +++ b/src/Message/Message_ProgressScope.hxx @@ -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'; diff --git a/src/Standard/Standard_Real.hxx b/src/Standard/Standard_Real.hxx index 767c9c18e3..43a95d717d 100644 --- a/src/Standard/Standard_Real.hxx +++ b/src/Standard/Standard_Real.hxx @@ -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(INT_MIN) + ? static_cast(INT_MIN) + : (theValue > static_cast(INT_MAX) + ? static_cast(INT_MAX) + : static_cast(theValue)); } // =======================================================================