1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-06-30 12:14:08 +03:00

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).
This commit is contained in:
ddzama 2022-03-29 16:32:46 +03:00 committed by smoskvin
parent 9416ba5fb0
commit 4e1b5fcbf0
4 changed files with 28 additions and 23 deletions

View File

@ -1010,7 +1010,8 @@ bool AIS_ViewController::UpdateMousePosition (const Graphic3d_Vec2i& thePoint,
const double aRotTol = theIsEmulated const double aRotTol = theIsEmulated
? double(myTouchToleranceScale) * myTouchRotationThresholdPx ? double(myTouchToleranceScale) * myTouchRotationThresholdPx
: 0.0; : 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 double aRotAccel = myNavigationMode == AIS_NavigationMode_FirstPersonWalk ? myMouseAccel : myOrbitAccel;
const Graphic3d_Vec2i aRotDelta = thePoint - myMousePressPoint; const Graphic3d_Vec2i aRotDelta = thePoint - myMousePressPoint;
@ -1063,7 +1064,8 @@ bool AIS_ViewController::UpdateMousePosition (const Graphic3d_Vec2i& thePoint,
const double aPanTol = theIsEmulated const double aPanTol = theIsEmulated
? double(myTouchToleranceScale) * myTouchPanThresholdPx ? double(myTouchToleranceScale) * myTouchPanThresholdPx
: 0.0; : 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) if (myUpdateStartPointPan)
{ {
@ -1629,10 +1631,11 @@ void AIS_ViewController::handleZoom (const Handle(V3d_View)& theView,
Graphic3d_Vec2i aWinSize; Graphic3d_Vec2i aWinSize;
theView->Window()->Size (aWinSize.x(), aWinSize.y()); theView->Window()->Size (aWinSize.x(), aWinSize.y());
const Graphic3d_Vec2d aPanFromCenterPx (double(theParams.Point.x()) - 0.5 * double(aWinSize.x()), const Graphic3d_Vec2d aWinSizeF (aWinSize);
double(aWinSize.y() - theParams.Point.y() - 1) - 0.5 * double(aWinSize.y())); const Graphic3d_Vec2d aPanFromCenterPx (double(theParams.Point.x()) - 0.5 * aWinSizeF.x(),
aDxy.x() += -aViewDims1.X() * aPanFromCenterPx.x() / double(aWinSize.x()); aWinSizeF.y() - double(theParams.Point.y()) - 1.0 - 0.5 * aWinSizeF.y());
aDxy.y() += -aViewDims1.Y() * aPanFromCenterPx.y() / double(aWinSize.y()); aDxy.x() += -aViewDims1.X() * aPanFromCenterPx.x() / aWinSizeF.x();
aDxy.y() += -aViewDims1.Y() * aPanFromCenterPx.y() / aWinSizeF.y();
} }
//theView->Translate (aCam, aDxy.x(), aDxy.y()); //theView->Translate (aCam, aDxy.x(), aDxy.y());

View File

@ -315,7 +315,7 @@ public:
void SetVertice (const Standard_Integer theIndex, const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ) 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"); 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.x() = theX;
aVec.y() = theY; aVec.y() = theY;
aVec.z() = theZ; aVec.z() = theZ;
@ -343,7 +343,7 @@ public:
Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index"); Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
if (myColData != NULL) 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), aColorPtr->SetValues (Standard_Byte(theR * 255.0),
Standard_Byte(theG * 255.0), Standard_Byte(theG * 255.0),
Standard_Byte(theB * 255.0), 255); Standard_Byte(theB * 255.0), 255);
@ -360,7 +360,7 @@ public:
Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index"); Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
if (myColData != NULL) 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; (*aColorPtr) = theColor;
} }
myAttribs->NbElements = Max (theIndex, myAttribs->NbElements); myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
@ -377,7 +377,7 @@ public:
Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index"); Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
if (myColData != NULL) 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"); Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
if (myNormData != NULL) 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.x() = Standard_ShortReal (theNX);
aVec.y() = Standard_ShortReal (theNY); aVec.y() = Standard_ShortReal (theNY);
aVec.z() = Standard_ShortReal (theNZ); aVec.z() = Standard_ShortReal (theNZ);
@ -424,7 +424,7 @@ public:
Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index"); Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
if (myTexData != NULL) 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.x() = Standard_ShortReal (theTX);
aVec.y() = Standard_ShortReal (theTY); aVec.y() = Standard_ShortReal (theTY);
} }
@ -450,7 +450,7 @@ public:
{ {
theX = theY = theZ = 0.0; theX = theY = theZ = 0.0;
Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index"); 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()); theX = Standard_Real(aVec.x());
theY = Standard_Real(aVec.y()); theY = Standard_Real(aVec.y());
theZ = Standard_Real(aVec.z()); theZ = Standard_Real(aVec.z());
@ -473,7 +473,7 @@ public:
Graphic3d_Vec4ub& theColor) const Graphic3d_Vec4ub& theColor) const
{ {
Standard_OutOfRange_Raise_if (myColData == NULL || theIndex < 1 || theIndex > myAttribs->NbElements, "BAD VERTEX index"); 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. //! Returns the vertex color values from the vertex table if defined.
@ -489,7 +489,7 @@ public:
{ {
return; 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; theR = Standard_Real(aColor.r()) / 255.0;
theG = Standard_Real(aColor.g()) / 255.0; theG = Standard_Real(aColor.g()) / 255.0;
theB = Standard_Real(aColor.b()) / 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"); Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
if (myColData != NULL) 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"); Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
if (myNormData != NULL) 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()); theNX = Standard_Real(aVec.x());
theNY = Standard_Real(aVec.y()); theNY = Standard_Real(aVec.y());
theNZ = Standard_Real(aVec.z()); theNZ = Standard_Real(aVec.z());
@ -555,7 +555,7 @@ public:
Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index"); Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
if (myTexData != NULL) 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()); theTX = Standard_Real(aVec.x());
theTY = Standard_Real(aVec.y()); theTY = Standard_Real(aVec.y());
} }

View File

@ -271,7 +271,7 @@ public: //! @name Preparation methods
if (!theName.IsEmpty()) if (!theName.IsEmpty())
{ {
myIsOwnName = true; myIsOwnName = true;
myName = (char* )Standard::Allocate (theName.Length() + 1); myName = (char* )Standard::Allocate (Standard_Size(theName.Length()) + Standard_Size(1));
char* aName = (char* )myName; char* aName = (char* )myName;
memcpy (aName, theName.ToCString(), theName.Length()); memcpy (aName, theName.ToCString(), theName.Length());
aName[theName.Length()] = '\0'; aName[theName.Length()] = '\0';

View File

@ -253,14 +253,16 @@ inline Standard_Real RealPart (const Standard_Real Value)
// If input value is out of valid range for integers, // If input value is out of valid range for integers,
// minimal or maximal possible integer is returned. // 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 // 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 // than INT_MIN or greater than INT_MAX to integer will cause signal
// "Floating point multiple trap" (OCC17861) // "Floating point multiple trap" (OCC17861)
return Value < INT_MIN ? INT_MIN return theValue < static_cast<Standard_Real>(INT_MIN)
: Value > INT_MAX ? INT_MAX ? static_cast<Standard_Integer>(INT_MIN)
: (Standard_Integer)Value; : (theValue > static_cast<Standard_Real>(INT_MAX)
? static_cast<Standard_Integer>(INT_MAX)
: static_cast<Standard_Integer>(theValue));
} }
// ======================================================================= // =======================================================================