From 5e452c37ee218b0c427645f7555dad7acf5df703 Mon Sep 17 00:00:00 2001 From: msv Date: Thu, 23 Oct 2014 14:19:15 +0400 Subject: [PATCH] 0025348: Method Assign of NCollection containers must not change own allocator of the target Correct according to remarks of reviewer: - Assign() and operator=() should implement equal approach to allocators - Protect all collections against assignment to itself with operator=() Test-case for issue #25348 --- src/NCollection/NCollection_DataMap.hxx | 5 ++-- src/NCollection/NCollection_DoubleMap.hxx | 7 ++--- .../NCollection_IndexedDataMap.hxx | 7 ++--- src/NCollection/NCollection_IndexedMap.hxx | 5 ++-- src/NCollection/NCollection_List.hxx | 11 ++++---- src/NCollection/NCollection_Map.hxx | 7 ++--- src/NCollection/NCollection_Sequence.hxx | 5 ++-- src/QABugs/QABugs_19.cxx | 26 +++++++++++++++++++ tests/bugs/fclasses/bug25348 | 11 ++++++++ 9 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 tests/bugs/fclasses/bug25348 diff --git a/src/NCollection/NCollection_DataMap.hxx b/src/NCollection/NCollection_DataMap.hxx index a8cb2d63e0..b223e3b048 100644 --- a/src/NCollection/NCollection_DataMap.hxx +++ b/src/NCollection/NCollection_DataMap.hxx @@ -151,13 +151,14 @@ class NCollection_DataMap : public NCollection_BaseMap this->exchangeMapsData (theOther); } - //! Assignment + //! Assignment. + //! This method does not change the internal allocator. NCollection_DataMap& Assign (const NCollection_DataMap& theOther) { if (this == &theOther) return *this; - Clear(theOther.myAllocator); + Clear(); ReSize (theOther.Extent()-1); Iterator anIter(theOther); for (; anIter.More(); anIter.Next()) diff --git a/src/NCollection/NCollection_DoubleMap.hxx b/src/NCollection/NCollection_DoubleMap.hxx index 6f2a59ab37..564146e67b 100644 --- a/src/NCollection/NCollection_DoubleMap.hxx +++ b/src/NCollection/NCollection_DoubleMap.hxx @@ -140,13 +140,14 @@ class NCollection_DoubleMap : public NCollection_BaseMap this->exchangeMapsData (theOther); } - //! Assignment + //! Assignment. + //! This method does not change the internal allocator. NCollection_DoubleMap& Assign (const NCollection_DoubleMap& theOther) { if (this == &theOther) return *this; - Clear(theOther.myAllocator); + Clear(); ReSize (theOther.Extent()-1); Iterator anIter(theOther); for (; anIter.More(); anIter.Next()) @@ -167,7 +168,7 @@ class NCollection_DoubleMap : public NCollection_BaseMap //! Assignment operator NCollection_DoubleMap& operator= (const NCollection_DoubleMap& theOther) - { + { return Assign (theOther); } diff --git a/src/NCollection/NCollection_IndexedDataMap.hxx b/src/NCollection/NCollection_IndexedDataMap.hxx index 0654b4cef7..fdaaf77f05 100644 --- a/src/NCollection/NCollection_IndexedDataMap.hxx +++ b/src/NCollection/NCollection_IndexedDataMap.hxx @@ -180,13 +180,14 @@ class NCollection_IndexedDataMap : public NCollection_BaseMap this->exchangeMapsData (theOther); } - //! Assignment + //! Assignment. + //! This method does not change the internal allocator. NCollection_IndexedDataMap& Assign (const NCollection_IndexedDataMap& theOther) { if (this == &theOther) return *this; - Clear(theOther.myAllocator); + Clear(); ReSize (theOther.Extent()-1); Standard_Integer i; for (i=1; i<=theOther.Extent(); i++) @@ -207,7 +208,7 @@ class NCollection_IndexedDataMap : public NCollection_BaseMap //! Assignment operator NCollection_IndexedDataMap& operator= (const NCollection_IndexedDataMap& theOther) - { + { return Assign (theOther); } diff --git a/src/NCollection/NCollection_IndexedMap.hxx b/src/NCollection/NCollection_IndexedMap.hxx index 117732eeef..277a7c9e4e 100644 --- a/src/NCollection/NCollection_IndexedMap.hxx +++ b/src/NCollection/NCollection_IndexedMap.hxx @@ -150,13 +150,14 @@ class NCollection_IndexedMap : public NCollection_BaseMap this->exchangeMapsData (theOther); } - //! Assign + //! Assign. + //! This method does not change the internal allocator. NCollection_IndexedMap& Assign (const NCollection_IndexedMap& theOther) { if (this == &theOther) return *this; - Clear(theOther.myAllocator); + Clear(); ReSize (theOther.Extent()-1); Standard_Integer i, iLength=theOther.Extent(); for (i=1; i<=iLength; i++) diff --git a/src/NCollection/NCollection_List.hxx b/src/NCollection/NCollection_List.hxx index 6ef0b32de5..b525603371 100644 --- a/src/NCollection/NCollection_List.hxx +++ b/src/NCollection/NCollection_List.hxx @@ -73,20 +73,21 @@ public: Standard_Integer Size (void) const { return Extent(); } - //! Replace this list by the items of another list (theOther parameter) - void Assign (const NCollection_List& theOther) + //! Replace this list by the items of another list (theOther parameter). + //! This method does not change the internal allocator. + NCollection_List& Assign (const NCollection_List& theOther) { if (this != &theOther) { - Clear(theOther.myAllocator); + Clear(); appendList(theOther.PFirst()); } + return *this; } //! Replacement operator NCollection_List& operator= (const NCollection_List& theOther) { - Assign (theOther); - return *this; + return Assign (theOther); } //! Clear this list diff --git a/src/NCollection/NCollection_Map.hxx b/src/NCollection/NCollection_Map.hxx index f12a15095d..6424858fe2 100644 --- a/src/NCollection/NCollection_Map.hxx +++ b/src/NCollection/NCollection_Map.hxx @@ -139,13 +139,14 @@ class NCollection_Map : public NCollection_BaseMap this->exchangeMapsData (theOther); } - //! Assign + //! Assign. + //! This method does not change the internal allocator. NCollection_Map& Assign (const NCollection_Map& theOther) { if (this == &theOther) return *this; - Clear(theOther.myAllocator); + Clear(); ReSize (theOther.Extent()-1); Iterator anIter(theOther); for (; anIter.More(); anIter.Next()) @@ -155,7 +156,7 @@ class NCollection_Map : public NCollection_BaseMap //! Assign operator NCollection_Map& operator= (const NCollection_Map& theOther) - { + { return Assign(theOther); } diff --git a/src/NCollection/NCollection_Sequence.hxx b/src/NCollection/NCollection_Sequence.hxx index 98239c0d36..f9f8fed281 100644 --- a/src/NCollection/NCollection_Sequence.hxx +++ b/src/NCollection/NCollection_Sequence.hxx @@ -170,12 +170,13 @@ public: this->myAllocator = theAllocator; } - //! Replace this sequence by the items of theOther + //! Replace this sequence by the items of theOther. + //! This method does not change the internal allocator. NCollection_Sequence& Assign (const NCollection_Sequence& theOther) { if (this == &theOther) return *this; - Clear (theOther.myAllocator); + Clear (); Node * pCur = (Node *) theOther.myFirstItem; while (pCur) { Node* pNew = new (this->myAllocator) Node (pCur->Value()); diff --git a/src/QABugs/QABugs_19.cxx b/src/QABugs/QABugs_19.cxx index 80ea21785f..1f6de33967 100755 --- a/src/QABugs/QABugs_19.cxx +++ b/src/QABugs/QABugs_19.cxx @@ -2883,6 +2883,10 @@ static Standard_Integer OCC7570 (Draw_Interpretor& di, Standard_Integer n, const } #include +//======================================================================= +//function : OCC25340 +//purpose : +//======================================================================= static Standard_Integer OCC25340 (Draw_Interpretor& /*theDI*/, Standard_Integer /*theArgNb*/, const char** /*theArgVec*/) @@ -2936,6 +2940,27 @@ static Standard_Integer OCC25100 (Draw_Interpretor& di, Standard_Integer argc, c return 0; } +//======================================================================= +//function : OCC25348 +//purpose : +//======================================================================= +static Standard_Integer OCC25348 (Draw_Interpretor& theDI, + Standard_Integer /*theArgNb*/, + const char** /*theArgVec*/) +{ + Handle(NCollection_IncAllocator) anAlloc1; + NCollection_List aList1(anAlloc1); + for (int i=0; i < 10; i++) + { + Handle(NCollection_IncAllocator) anAlloc2; + NCollection_List aList2(anAlloc2); + aList2.Append(i); + aList1.Assign(aList2); + } + theDI << "Test complete\n"; + return 0; +} + void QABugs::Commands_19(Draw_Interpretor& theCommands) { const char *group = "QABugs"; @@ -2995,5 +3020,6 @@ void QABugs::Commands_19(Draw_Interpretor& theCommands) { theCommands.Add ("OCC7570", "OCC7570 shape", __FILE__, OCC7570, group); theCommands.Add ("OCC25100", "OCC25100 shape", __FILE__, OCC25100, group); theCommands.Add ("OCC25340", "OCC25340", __FILE__, OCC25340, group); + theCommands.Add ("OCC25348", "OCC25348", __FILE__, OCC25348, group); return; } diff --git a/tests/bugs/fclasses/bug25348 b/tests/bugs/fclasses/bug25348 new file mode 100644 index 0000000000..6fe3a0eb3e --- /dev/null +++ b/tests/bugs/fclasses/bug25348 @@ -0,0 +1,11 @@ +puts "========" +puts "OCC25348" +puts "========" +puts "" +####################################################################################### +# Method Assign of NCollection containers must not change own allocator of the target +####################################################################################### + +pload QAcommands + +OCC25348 -- 2.20.1