1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-10 18:51:21 +03:00

0028257: XCAFPrs_Style - uninitialized memory usage within ::HashCode()

XCAFPrs_Style::HashCode() function has been corrected.
XCAFPrs_Style::SetVisibility() does not reset assigned colors anymore.
This commit is contained in:
kgv 2017-01-25 20:26:27 +03:00 committed by apn
parent 2a4611e19f
commit 650efe05be
2 changed files with 99 additions and 190 deletions

View File

@ -13,163 +13,56 @@
// Alternatively, this file may be used under the terms of Open CASCADE // Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement. // commercial license or contractual agreement.
#include <Quantity_Color.hxx>
#include <XCAFPrs_Style.hxx> #include <XCAFPrs_Style.hxx>
//======================================================================= //=======================================================================
//function : XCAFPrs_Style //function : XCAFPrs_Style
//purpose : //purpose :
//======================================================================= //=======================================================================
XCAFPrs_Style::XCAFPrs_Style () : XCAFPrs_Style::XCAFPrs_Style()
defColorSurf(Standard_False), : myHasColorSurf(Standard_False),
defColorCurv(Standard_False), myHasColorCurv(Standard_False),
myVisibility(Standard_True) myIsVisible (Standard_True)
{ {
} //
//=======================================================================
//function : IsSetColorSurf
//purpose :
//=======================================================================
Standard_Boolean XCAFPrs_Style::IsSetColorSurf () const
{
return defColorSurf;
}
//=======================================================================
//function : GetColorSurf
//purpose :
//=======================================================================
Quantity_Color XCAFPrs_Style::GetColorSurf () const
{
return myColorSurf;
} }
//======================================================================= //=======================================================================
//function : SetColorSurf //function : SetColorSurf
//purpose : //purpose :
//======================================================================= //=======================================================================
void XCAFPrs_Style::SetColorSurf (const Quantity_Color& theColor)
void XCAFPrs_Style::SetColorSurf (const Quantity_Color &col)
{ {
myColorSurf = col; myColorSurf = theColor;
defColorSurf = Standard_True; myHasColorSurf = Standard_True;
} }
//======================================================================= //=======================================================================
//function : UnSetColorSurf //function : UnSetColorSurf
//purpose : //purpose :
//======================================================================= //=======================================================================
void XCAFPrs_Style::UnSetColorSurf()
void XCAFPrs_Style::UnSetColorSurf ()
{ {
defColorSurf = Standard_False; myHasColorSurf = Standard_False;
myColorSurf.SetValues ( Quantity_NOC_YELLOW ); myColorSurf.SetValues (Quantity_NOC_YELLOW);
}
//=======================================================================
//function : IsSetColorCurv
//purpose :
//=======================================================================
Standard_Boolean XCAFPrs_Style::IsSetColorCurv () const
{
return defColorCurv;
}
//=======================================================================
//function : GetColorCurv
//purpose :
//=======================================================================
Quantity_Color XCAFPrs_Style::GetColorCurv () const
{
return myColorCurv;
} }
//======================================================================= //=======================================================================
//function : SetColorCurv //function : SetColorCurv
//purpose : //purpose :
//======================================================================= //=======================================================================
void XCAFPrs_Style::SetColorCurv (const Quantity_Color& theColor)
void XCAFPrs_Style::SetColorCurv (const Quantity_Color &col)
{ {
myColorCurv = col; myColorCurv = theColor;
defColorCurv = Standard_True; myHasColorCurv = Standard_True;
} }
//======================================================================= //=======================================================================
//function : UnSetColorCurv //function : UnSetColorCurv
//purpose : //purpose :
//======================================================================= //=======================================================================
void XCAFPrs_Style::UnSetColorCurv()
void XCAFPrs_Style::UnSetColorCurv ()
{ {
defColorCurv = Standard_False; myHasColorCurv = Standard_False;
myColorCurv.SetValues ( Quantity_NOC_YELLOW ); myColorCurv.SetValues (Quantity_NOC_YELLOW);
}
//=======================================================================
//function : SetVisibility
//purpose :
//=======================================================================
void XCAFPrs_Style::SetVisibility (const Standard_Boolean visibility)
{
myVisibility = visibility;
if ( ! visibility ) { UnSetColorSurf(); UnSetColorCurv(); } // for hash codes
}
//=======================================================================
//function : IsVisible
//purpose :
//=======================================================================
Standard_Boolean XCAFPrs_Style::IsVisible () const
{
return myVisibility;
}
//=======================================================================
//function : IsEqual
//purpose :
//=======================================================================
Standard_Boolean XCAFPrs_Style::IsEqual (const XCAFPrs_Style &other) const
{
return myVisibility == other.myVisibility &&
( ! myVisibility ||
( defColorSurf == other.defColorSurf &&
defColorCurv == other.defColorCurv &&
( ! defColorSurf || myColorSurf == other.myColorSurf ) &&
( ! defColorCurv || myColorCurv == other.myColorCurv ) ) );
}
//=======================================================================
//function : HashCode
//purpose :
//=======================================================================
Standard_Integer XCAFPrs_Style::HashCode (const XCAFPrs_Style& S, const Standard_Integer Upper)
{
int *meintPtr = (int*)&S;
Standard_Integer aHashCode=0, i, aSize = sizeof(S) / sizeof(int);
for (i = 0; i < aSize; i++, meintPtr++) {
aHashCode = aHashCode ^ *meintPtr;
}
return ::HashCode( aHashCode, Upper);
}
//=======================================================================
//function : IsEqual
//purpose :
//=======================================================================
Standard_Boolean XCAFPrs_Style::IsEqual (const XCAFPrs_Style& S1, const XCAFPrs_Style& S2)
{
return S1.IsEqual(S2);
} }

View File

@ -19,92 +19,108 @@
#include <Standard.hxx> #include <Standard.hxx>
#include <Standard_DefineAlloc.hxx> #include <Standard_DefineAlloc.hxx>
#include <Standard_Handle.hxx> #include <Standard_Handle.hxx>
#include <Quantity_ColorHasher.hxx>
#include <Standard_Boolean.hxx> //! Represents a set of styling settings applicable to a (sub)shape
#include <Quantity_Color.hxx>
#include <Standard_Integer.hxx>
class Quantity_Color;
//! Represents a set of styling settings applicable to
//! a (sub)shape
class XCAFPrs_Style class XCAFPrs_Style
{ {
public: public:
DEFINE_STANDARD_ALLOC DEFINE_STANDARD_ALLOC
//! Empty constructor - colors are unset, visibility is TRUE.
Standard_EXPORT XCAFPrs_Style(); Standard_EXPORT XCAFPrs_Style();
Standard_EXPORT Standard_Boolean IsSetColorSurf() const; //! Return TRUE if surface color has been defined.
Standard_Boolean IsSetColorSurf() const { return myHasColorSurf; }
Standard_EXPORT Quantity_Color GetColorSurf() const;
//! Return surface color.
const Quantity_Color& GetColorSurf() const { return myColorSurf; }
//! Set surface color.
Standard_EXPORT void SetColorSurf (const Quantity_Color& col); Standard_EXPORT void SetColorSurf (const Quantity_Color& col);
//! Manage surface color setting //! Manage surface color setting
Standard_EXPORT void UnSetColorSurf(); Standard_EXPORT void UnSetColorSurf();
Standard_EXPORT Standard_Boolean IsSetColorCurv() const; //! Return TRUE if curve color has been defined.
Standard_Boolean IsSetColorCurv() const { return myHasColorCurv; }
Standard_EXPORT Quantity_Color GetColorCurv() const;
//! Return curve color.
const Quantity_Color& GetColorCurv() const { return myColorCurv; }
//! Set curve color.
Standard_EXPORT void SetColorCurv (const Quantity_Color& col); Standard_EXPORT void SetColorCurv (const Quantity_Color& col);
//! Manage curve color setting //! Manage curve color setting
Standard_EXPORT void UnSetColorCurv(); Standard_EXPORT void UnSetColorCurv();
Standard_EXPORT void SetVisibility (const Standard_Boolean visibility); //! Assign visibility.
void SetVisibility (const Standard_Boolean theVisibility) { myIsVisible = theVisibility; }
//! Manage visibility
//! Note: Setting visibility to False makes colors undefined //! Manage visibility.
//! This is necessary for HashCode Standard_Boolean IsVisible() const { return myIsVisible; }
Standard_EXPORT Standard_Boolean IsVisible() const;
//! Returns True if styles are the same
//! Returs True if styles are the same
//! Methods for using Style as key in maps //! Methods for using Style as key in maps
Standard_EXPORT Standard_Boolean IsEqual (const XCAFPrs_Style& other) const; Standard_Boolean IsEqual (const XCAFPrs_Style& theOther) const
Standard_Boolean operator == (const XCAFPrs_Style& other) const {
{ if (myIsVisible != theOther.myIsVisible)
return IsEqual(other); {
} return false;
}
//! Returns a HasCode value for the Key <K> in the else if (!myIsVisible)
//! range 0..Upper. {
Standard_EXPORT static Standard_Integer HashCode (const XCAFPrs_Style& S, const Standard_Integer Upper); return true;
}
//! Returns True when the two keys are the same. Two
//! same keys must have the same hashcode, the
//! contrary is not necessary.
Standard_EXPORT static Standard_Boolean IsEqual (const XCAFPrs_Style& S1, const XCAFPrs_Style& S2);
return myHasColorSurf == theOther.myHasColorSurf
&& myHasColorCurv == theOther.myHasColorCurv
&& (!myHasColorSurf || myColorSurf == theOther.myColorSurf)
&& (!myHasColorCurv || myColorCurv == theOther.myColorCurv);
}
//! Returns True if styles are the same.
Standard_Boolean operator== (const XCAFPrs_Style& theOther) const
{
return IsEqual (theOther);
}
//! Returns a HasCode value.
static Standard_Integer HashCode (const XCAFPrs_Style& theStyle,
const Standard_Integer theUpper)
{
if (!theStyle.myIsVisible)
{
return 1;
}
int aHashCode = 0;
if (theStyle.myHasColorSurf)
{
aHashCode = aHashCode ^ Quantity_ColorHasher::HashCode (theStyle.myColorSurf, theUpper);
}
if (theStyle.myHasColorCurv)
{
aHashCode = aHashCode ^ Quantity_ColorHasher::HashCode (theStyle.myColorCurv, theUpper);
}
return ((aHashCode & 0x7fffffff) % theUpper) + 1;
}
//! Returns True when the two keys are the same.
static Standard_Boolean IsEqual (const XCAFPrs_Style& theS1, const XCAFPrs_Style& theS2)
{
return theS1.IsEqual (theS2);
}
protected: protected:
Quantity_Color myColorSurf;
Quantity_Color myColorCurv;
Standard_Boolean myHasColorSurf;
Standard_Boolean myHasColorCurv;
private: Standard_Boolean myIsVisible;
Standard_Boolean defColorSurf;
Standard_Boolean defColorCurv;
Standard_Boolean myVisibility;
Quantity_Color myColorSurf;
Quantity_Color myColorCurv;
}; };
#endif // _XCAFPrs_Style_HeaderFile #endif // _XCAFPrs_Style_HeaderFile