1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0023042: Potential mistakes in (s)printf usage

Use PRIuPTR macros for Standard_Size values in printf.
Use STL streams instead of printf when reasonable.
This commit is contained in:
omy
2013-07-12 12:25:44 +04:00
parent bda8360543
commit 64531d9c98
12 changed files with 346 additions and 322 deletions

View File

@@ -24,6 +24,8 @@
#include <NCollection_Map.hxx>
#include <NCollection_List.hxx>
#include <Standard_Mutex.hxx>
#include <fstream>
#include <iomanip>
IMPLEMENT_STANDARD_HANDLE(NCollection_BaseAllocator,MMgt_TShared)
IMPLEMENT_STANDARD_RTTIEXT(NCollection_BaseAllocator,MMgt_TShared)
@@ -254,35 +256,55 @@ void NCollection_BaseAllocator::PrintMemUsageStatistics()
}
Standard_Size aTotAlloc = 0;
Standard_Size aTotLeft = 0;
// print
FILE * ff = fopen("memstat.d", "wt");
if (ff == NULL)
std::ofstream aFileOut ("memstat.d", std::ios_base::trunc | std::ios_base::out);
if (!aFileOut.is_open())
{
cout << "failure writing file memstat.d" << endl;
std::cout << "failure writing file memstat.d" << std::endl;
return;
}
fprintf(ff, "%12s %12s %12s %12s %12s\n",
"BlockSize", "NbAllocated", "NbLeft", "Allocated", "Left");
aFileOut.imbue (std::locale ("C"));
// header
aFileOut << std::setw(20) << "BlockSize" << ' '
<< std::setw(12) << "NbAllocated" << ' '
<< std::setw(12) << "NbLeft" << ' '
<< std::setw(20) << "Allocated" << ' '
<< std::setw(20) << "Left" << '\n';
// body
for (itLst.Init(aColl); itLst.More(); itLst.Next())
{
const StorageInfo& aInfo = itLst.Value();
Standard_Integer nbLeft = aInfo.nbAlloc - aInfo.nbFree;
Standard_Size aSizeAlloc = aInfo.nbAlloc * aInfo.roundSize;
Standard_Size aSizeLeft = nbLeft * aInfo.roundSize;
fprintf(ff, "%12d %12d %12d %12d %12d\n", aInfo.roundSize,
aInfo.nbAlloc, nbLeft, aSizeAlloc, aSizeLeft);
aFileOut << std::setw(20) << aInfo.roundSize << ' '
<< std::setw(12) << aInfo.nbAlloc << ' '
<< std::setw(12) << nbLeft << ' '
<< std::setw(20) << aSizeAlloc << ' '
<< std::setw(20) << aSizeLeft << '\n';
aTotAlloc += aSizeAlloc;
aTotLeft += aSizeLeft;
aTotLeft += aSizeLeft;
}
fprintf(ff, "%12s %12s %12s %12d %12d\n", "Total:", "", "",
aTotAlloc, aTotLeft);
// footer
aFileOut << std::setw(20) << "Total:" << ' '
<< std::setw(12) << "" << ' '
<< std::setw(12) << "" << ' '
<< std::setw(20) << aTotAlloc << ' '
<< std::setw(20) << aTotLeft << '\n';
if (!StorageIDSet().IsEmpty())
{
fprintf(ff, "Alive allocation numbers of size=%d\n", StandardCallBack_CatchSize());
NCollection_Map<Standard_Size>::Iterator itMap1(StorageIDSet());
for (; itMap1.More(); itMap1.Next())
fprintf(ff, "%d\n", itMap1.Key());
aFileOut << "Alive allocation numbers of size=" << StandardCallBack_CatchSize() << '\n';
for (NCollection_Map<Standard_Size>::Iterator itMap1(StorageIDSet()); itMap1.More(); itMap1.Next())
{
aFileOut << itMap1.Key() << '\n';
}
}
fclose(ff);
aFileOut.close();
}

View File

@@ -17,8 +17,6 @@
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#include <NCollection_HeapAllocator.hxx>
#include <Standard_OutOfMemory.hxx>
#include <Standard_Mutex.hxx>
@@ -35,13 +33,14 @@ void * NCollection_HeapAllocator::Allocate (const Standard_Size theSize)
{
// the size is rounded up to word size.
const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
void * pResult = malloc(aRoundSize);
if (!pResult) {
char buf[128];
sprintf (buf, "Failed to allocate %d bytes in global dynamic heap",theSize);
Standard_OutOfMemory::Raise(&buf[0]);
void* aResult = malloc (aRoundSize);
if (aResult == NULL)
{
char aBuffer[96];
Sprintf (aBuffer, "Failed to allocate %" PRIuPTR " bytes in global dynamic heap", theSize);
Standard_OutOfMemory::Raise (aBuffer);
}
return pResult;
return aResult;
}
//=======================================================================

View File

@@ -17,13 +17,14 @@
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#include <NCollection_IncAllocator.hxx>
#include <NCollection_DataMap.hxx>
#include <NCollection_Map.hxx>
#include <Standard_Mutex.hxx>
#include <Standard_OutOfMemory.hxx>
#include <stdio.h>
#include <fstream>
#include <iomanip>
IMPLEMENT_STANDARD_HANDLE (NCollection_IncAllocator,NCollection_BaseAllocator)
IMPLEMENT_STANDARD_RTTIEXT (NCollection_IncAllocator,NCollection_BaseAllocator)
@@ -134,34 +135,40 @@ static void Debug_Destroy(Standard_Address theAlloc)
Standard_EXPORT void IncAllocator_PrintAlive()
{
if (!StorageIDSet().IsEmpty())
if (StorageIDSet().IsEmpty())
{
FILE * ff = fopen("inc_alive.d", "wt");
if (ff == NULL)
{
cout << "failure writing file inc_alive.d" << endl;
}
else
{
fprintf(ff, "Alive IncAllocators (number, size in Kb)\n");
NCollection_DataMap<Standard_Address, Standard_Size>::Iterator
itMap(StorageIDMap());
Standard_Size aTotSize = 0;
Standard_Integer nbAlloc = 0;
for (; itMap.More(); itMap.Next())
{
NCollection_IncAllocator* anAlloc =
static_cast<NCollection_IncAllocator*>(itMap.Key());
Standard_Size anID = itMap.Value();
Standard_Size aSize = anAlloc->GetMemSize();
aTotSize += aSize;
nbAlloc++;
fprintf(ff, "%-8d %8.1f\n", anID, double(aSize)/1024);
}
fprintf(ff, "Total:\n%-8d %8.1f\n", nbAlloc, double(aTotSize)/1024);
fclose(ff);
}
return;
}
std::ofstream aFileOut ("inc_alive.d", std::ios_base::trunc | std::ios_base::out);
if (!aFileOut.is_open())
{
std::cout << "failure writing file inc_alive.d" << std::endl;
return;
}
aFileOut.imbue (std::locale ("C"));
aFileOut << std::fixed << std::setprecision(1);
aFileOut << "Alive IncAllocators (number, size in Kb)\n";
Standard_Size aTotSize = 0;
Standard_Integer nbAlloc = 0;
for (NCollection_DataMap<Standard_Address, Standard_Size>::Iterator itMap (StorageIDMap());
itMap.More(); itMap.Next())
{
const NCollection_IncAllocator* anAlloc = static_cast<NCollection_IncAllocator*>(itMap.Key());
Standard_Size anID = itMap.Value();
Standard_Size aSize = anAlloc->GetMemSize();
aTotSize += aSize;
nbAlloc++;
aFileOut << std::setw(20) << anID << ' '
<< std::setw(20) << (double(aSize) / 1024.0)
<< '\n';
}
aFileOut << "Total:\n"
<< std::setw(20) << nbAlloc << ' '
<< std::setw(20) << (double(aTotSize) / 1024.0)
<< '\n';
aFileOut.close();
}
//=======================================================================

View File

@@ -17,8 +17,6 @@
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#ifndef NCollection_IncAllocator_HeaderFile
#define NCollection_IncAllocator_HeaderFile
@@ -123,5 +121,4 @@ class NCollection_IncAllocator : public NCollection_BaseAllocator
// Definition of HANDLE object using Standard_DefineHandle.hxx
DEFINE_STANDARD_HANDLE (NCollection_IncAllocator, NCollection_BaseAllocator)
#endif