From a73267f24b5d10a5243d3c036fee8ca00bd327db Mon Sep 17 00:00:00 2001 From: msv Date: Thu, 24 Apr 2014 16:25:08 +0400 Subject: [PATCH] 0024834: Allocation of memory for exception message must not throw another exception - Add QA draw command OCC24834 - Make functions allocate_message/deallocate_message use malloc/free instead of operator new/delete - Eliminate compilation error on Linux Test case for issue CR24834 --- src/QABugs/QABugs_19.cxx | 67 +++++++++++++++++++++++++++++++ src/Standard/Standard_Failure.cxx | 13 +++--- tests/bugs/fclasses/bug24834 | 11 +++++ 3 files changed, 85 insertions(+), 6 deletions(-) create mode 100755 tests/bugs/fclasses/bug24834 diff --git a/src/QABugs/QABugs_19.cxx b/src/QABugs/QABugs_19.cxx index c7a58d23ec..edb8b0abce 100755 --- a/src/QABugs/QABugs_19.cxx +++ b/src/QABugs/QABugs_19.cxx @@ -2111,6 +2111,72 @@ static Standard_Integer OCC24755 (Draw_Interpretor& di, Standard_Integer n, cons return 0; } +struct MyStubObject +{ + MyStubObject() : ptr(0L) {} + MyStubObject(void* thePtr) : ptr(thePtr) {} + char overhead[40]; + void* ptr; +}; + +//======================================================================= +//function : OCC24834 +//purpose : +//======================================================================= +static Standard_Integer OCC24834 (Draw_Interpretor& di, Standard_Integer n, const char** a) +{ + if (n != 1) + { + std::cout << "Usage : " << a[0] << "\n"; + return 1; + } + + NCollection_List aList; + const Standard_Integer aSmallBlockSize = 40; + const Standard_Integer aLargeBlockSize = 1500000; + + // quick populate memory with large blocks + try + { + for (;;) + { + aList.Append(MyStubObject(Standard::Allocate(aLargeBlockSize))); + } + } + catch (Standard_Failure) + { + di << "caught out of memory for large blocks: OK\n"; + } + catch (...) + { + di << "skept out of memory for large blocks: Error\n"; + } + + // allocate small blocks + try + { + for (;;) + { + aList.Append(MyStubObject(Standard::Allocate(aSmallBlockSize))); + } + } + catch (Standard_Failure) + { + di << "caught out of memory for small blocks: OK\n"; + } + catch (...) + { + di << "skept out of memory for small blocks: Error\n"; + } + + // release all allocated blocks + for (NCollection_List::Iterator it(aList); it.More(); it.Next()) + { + Standard::Free(it.Value().ptr); + } + return 0; +} + void QABugs::Commands_19(Draw_Interpretor& theCommands) { const char *group = "QABugs"; @@ -2151,5 +2217,6 @@ void QABugs::Commands_19(Draw_Interpretor& theCommands) { theCommands.Add ("OCC24667", "OCC24667 result Wire_spine Profile [Mode [Approx]], no args to get help", __FILE__, OCC24667, group); theCommands.Add ("OCC24565", "OCC24565 FileNameIGS FileNameSTOR", __FILE__, OCC24565, group); theCommands.Add ("OCC24755", "OCC24755", __FILE__, OCC24755, group); + theCommands.Add ("OCC24834", "OCC24834", __FILE__, OCC24834, group); return; } diff --git a/src/Standard/Standard_Failure.cxx b/src/Standard/Standard_Failure.cxx index 3d0fec5e44..4ec1917948 100644 --- a/src/Standard/Standard_Failure.cxx +++ b/src/Standard/Standard_Failure.cxx @@ -25,13 +25,14 @@ static Standard_CString allocate_message(const Standard_CString AString) Standard_CString aStr = 0; if(AString) { const Standard_Size aLen = strlen(AString); - aStr = new Standard_Character[aLen+sizeof(Standard_Integer)+1]; - Standard_PCharacter pStr=(Standard_PCharacter)aStr; - strcpy(pStr+sizeof(Standard_Integer),AString); - *((Standard_Integer*)aStr) = 1; + aStr = (Standard_CString) malloc(aLen+sizeof(Standard_Integer)+1); + if (aStr) { + Standard_PCharacter pStr=(Standard_PCharacter)aStr; + strcpy(pStr+sizeof(Standard_Integer),AString); + *((Standard_Integer*)aStr) = 1; + } } return aStr; - } static Standard_CString copy_message(Standard_CString aMessage) @@ -49,7 +50,7 @@ static void deallocate_message(Standard_CString aMessage) if(aMessage) { (*((Standard_Integer*)aMessage))--; if(*((Standard_Integer*)aMessage)==0) - delete [](Standard_PCharacter)aMessage; + free((void*)aMessage); } } diff --git a/tests/bugs/fclasses/bug24834 b/tests/bugs/fclasses/bug24834 new file mode 100755 index 0000000000..8df2ad5de3 --- /dev/null +++ b/tests/bugs/fclasses/bug24834 @@ -0,0 +1,11 @@ +puts "================" +puts "OCC24834" +puts "================" +puts "" +####################################################################### +# Allocation of memory for exception message must not throw another exception +####################################################################### + +pload QAcommands + +OCC24834