Skip to content

Commit

Permalink
STYLE: Replace new T[n] with make_unique_for_overwrite in cxx files
Browse files Browse the repository at this point in the history
Replaced `new T[n]` with `make_unique_for_overwrite<T[]>(n)` calls, for the
initialization of local variables in ITK "*.cxx" library files, and removed the
corresponding `delete[]` statements.

Following C++ Core Guidelines, April 10, 2022, "Use `unique_ptr` or
`shared_ptr` to avoid forgetting to `delete` objects created using `new`"
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-smart
  • Loading branch information
N-Dekker committed Aug 30, 2022
1 parent 01e0201 commit 15d09d6
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 195 deletions.
8 changes: 4 additions & 4 deletions Modules/Core/Common/src/itkWin32OutputWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
*=========================================================================*/
#include "itkWin32OutputWindow.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{
Expand Down Expand Up @@ -91,7 +92,7 @@ Win32OutputWindow::DisplayText(const char * text)
}

/** Create a buffer big enough to hold the entire text */
char * buffer = new char[strlen(text) + 1];
const auto buffer = make_unique_for_overwrite<char[]>(strlen(text) + 1);

/** Start at the beginning */
const char * NewLinePos = text;
Expand All @@ -109,14 +110,13 @@ Win32OutputWindow::DisplayText(const char * text)
else
{
int len = NewLinePos - text;
strncpy(buffer, text, len);
strncpy(buffer.get(), text, len);
buffer[len] = 0;
text = NewLinePos + 1;
Win32OutputWindow::AddText(buffer);
Win32OutputWindow::AddText(buffer.get());
Win32OutputWindow::AddText("\r\n");
}
}
delete[] buffer;
}

/** Add some text to the EDIT control. */
Expand Down
10 changes: 4 additions & 6 deletions Modules/Core/Common/src/itkXMLFileOutputWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkXMLFileOutputWindow.h"
#include "itkMakeUniqueForOverwrite.h"
#include <fstream>
#include <cstring>

Expand Down Expand Up @@ -64,18 +65,16 @@ XMLFileOutputWindow::DisplayTag(const char * text)
void
XMLFileOutputWindow::DisplayXML(const char * tag, const char * text)
{
char * xmlText;

if (!text)
{
return;
}

// allocate enough room for the worst case
xmlText = new char[strlen(text) * 6 + 1];
const auto xmlText = make_unique_for_overwrite<char[]>(strlen(text) * 6 + 1);

const char * s = text;
char * x = xmlText;
char * x = xmlText.get();
*x = '\0';

// replace all special characters
Expand Down Expand Up @@ -127,13 +126,12 @@ XMLFileOutputWindow::DisplayXML(const char * tag, const char * text)
{
this->Initialize();
}
*m_Stream << "<" << tag << ">" << xmlText << "</" << tag << ">" << std::endl;
*m_Stream << "<" << tag << ">" << xmlText.get() << "</" << tag << ">" << std::endl;

if (m_Flush)
{
m_Stream->flush();
}
delete[] xmlText;
}

void
Expand Down
10 changes: 5 additions & 5 deletions Modules/IO/BioRad/src/itkBioRadImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "itkBioRadImageIO.h"
#include "itkByteSwapper.h"
#include "itksys/SystemTools.hxx"
#include "itkMakeUniqueForOverwrite.h"

#define BIORAD_HEADER_LENGTH 76
#define BIORAD_NOTE_LENGTH 96
Expand Down Expand Up @@ -480,17 +481,16 @@ BioRadImageIO::Write(const void * buffer)
const auto numberOfBytes = static_cast<SizeValueType>(this->GetImageSizeInBytes());
const auto numberOfComponents = static_cast<SizeValueType>(this->GetImageSizeInComponents());

auto * tempmemory = new char[numberOfBytes];
memcpy(tempmemory, buffer, numberOfBytes);
const auto tempmemory = make_unique_for_overwrite<char[]>(numberOfBytes);
memcpy(tempmemory.get(), buffer, numberOfBytes);
if (this->GetComponentType() == IOComponentEnum::USHORT)
{
ByteSwapper<unsigned short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<unsigned short *>(tempmemory),
ByteSwapper<unsigned short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<unsigned short *>(tempmemory.get()),
numberOfComponents);
}

// Write the actual pixel data
file.write(static_cast<const char *>(tempmemory), numberOfBytes);
delete[] tempmemory;
file.write(static_cast<const char *>(tempmemory.get()), numberOfBytes);
file.close();
}

Expand Down
38 changes: 17 additions & 21 deletions Modules/IO/GDCM/src/itkGDCMImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "itksys/SystemTools.hxx"
#include "itksys/Base64.h"
#include "itkMakeUniqueForOverwrite.h"

#include "gdcmImageHelper.h"
#include "gdcmFileExplicitFilter.h"
Expand Down Expand Up @@ -367,7 +368,7 @@ GDCMImageIO::Read(void * pointer)

if (m_SingleBit)
{
auto * copy = new unsigned char[len];
const auto copy = make_unique_for_overwrite<unsigned char[]>(len);
unsigned char * t = reinterpret_cast<unsigned char *>(pointer);
size_t j = 0;
for (size_t i = 0; i < len / 8; ++i)
Expand All @@ -383,8 +384,7 @@ GDCMImageIO::Read(void * pointer)
copy[j + 7] = (c & 0x80) ? 255 : 0;
j += 8;
}
memcpy((char *)pointer, copy, len);
delete[] copy;
memcpy((char *)pointer, copy.get(), len);
}
else
{
Expand All @@ -405,10 +405,9 @@ GDCMImageIO::Read(void * pointer)
r.SetSlope(m_RescaleSlope);
r.SetPixelFormat(pixeltype);
gdcm::PixelFormat outputpt = r.ComputeInterceptSlopePixelType();
auto * copy = new char[len];
memcpy(copy, (char *)pointer, len);
r.Rescale((char *)pointer, copy, len);
delete[] copy;
const auto copy = make_unique_for_overwrite<char[]>(len);
memcpy(copy.get(), (char *)pointer, len);
r.Rescale((char *)pointer, copy.get(), len);
// WARNING: sizeof(Real World Value) != sizeof(Stored Pixel)
len = len * outputpt.GetPixelSize() / pixeltype.GetPixelSize();
}
Expand Down Expand Up @@ -756,15 +755,14 @@ GDCMImageIO::InternalReadImageInformation()
int encodedLengthEstimate = 2 * bv->GetLength();
encodedLengthEstimate = ((encodedLengthEstimate / 4) + 1) * 4;

auto * bin = new char[encodedLengthEstimate];
auto encodedLengthActual =
const auto bin = make_unique_for_overwrite<char[]>(encodedLengthEstimate);
auto encodedLengthActual =
static_cast<unsigned int>(itksysBase64_Encode((const unsigned char *)bv->GetPointer(),
static_cast<SizeValueType>(bv->GetLength()),
(unsigned char *)bin,
(unsigned char *)bin.get(),
0));
std::string encodedValue(bin, encodedLengthActual);
std::string encodedValue(bin.get(), encodedLengthActual);
EncapsulateMetaData<std::string>(dico, tag.PrintAsPipeSeparatedString(), encodedValue);
delete[] bin;
}
}
}
Expand Down Expand Up @@ -883,16 +881,16 @@ GDCMImageIO::Write(const void * buffer)
{
// Custom VR::VRBINARY
// convert value from Base64
auto * bin = new uint8_t[value.size()];
auto decodedLengthActual =
const auto bin = make_unique_for_overwrite<uint8_t[]>(value.size());
auto decodedLengthActual =
static_cast<unsigned int>(itksysBase64_Decode((const unsigned char *)value.c_str(),
static_cast<SizeValueType>(0),
(unsigned char *)bin,
(unsigned char *)bin.get(),
static_cast<SizeValueType>(value.size())));
if (/*tag.GetGroup() != 0 ||*/ tag.GetElement() != 0) // ?
{
gdcm::DataElement de(tag);
de.SetByteValue((char *)bin, decodedLengthActual);
de.SetByteValue((char *)bin.get(), decodedLengthActual);
de.SetVR(dictEntry.GetVR());
if (tag.GetGroup() == 0x2)
{
Expand All @@ -903,7 +901,6 @@ GDCMImageIO::Write(const void * buffer)
header.Insert(de);
}
}
delete[] bin;
}
else // VRASCII
{
Expand Down Expand Up @@ -1297,11 +1294,10 @@ GDCMImageIO::Write(const void * buffer)

image.SetIntercept(m_RescaleIntercept);
image.SetSlope(m_RescaleSlope);
auto * copyBuffer = new char[len];
const auto copyBuffer = make_unique_for_overwrite<char[]>(len);
const auto * inputBuffer = static_cast<const char *>(buffer);
ir.InverseRescale(copyBuffer, inputBuffer, numberOfBytes);
pixeldata.SetByteValue(copyBuffer, static_cast<uint32_t>(len));
delete[] copyBuffer;
ir.InverseRescale(copyBuffer.get(), inputBuffer, numberOfBytes);
pixeldata.SetByteValue(copyBuffer.get(), static_cast<uint32_t>(len));
}
else
{
Expand Down
23 changes: 11 additions & 12 deletions Modules/IO/GIPL/src/itkGiplImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/
#include "itkGiplImageIO.h"
#include "itkByteSwapper.h"
#include "itkMakeUniqueForOverwrite.h"
#include <iostream>
#include "itk_zlib.h"

Expand Down Expand Up @@ -1049,33 +1050,31 @@ GiplImageIO ::Write(const void * buffer)
// Swap bytes if necessary
if (m_ByteOrder == IOByteOrderEnum::LittleEndian)
{
auto * tempBuffer = new char[numberOfBytes];
memcpy(tempBuffer, buffer, numberOfBytes);
SwapBytesIfNecessary(tempBuffer, numberOfComponents);
const auto tempBuffer = make_unique_for_overwrite<char[]>(numberOfBytes);
memcpy(tempBuffer.get(), buffer, numberOfBytes);
SwapBytesIfNecessary(tempBuffer.get(), numberOfComponents);
if (m_IsCompressed)
{
gzwrite(m_Internal->m_GzFile, tempBuffer, numberOfBytes);
gzwrite(m_Internal->m_GzFile, tempBuffer.get(), numberOfBytes);
}
else
{
m_Ofstream.write(tempBuffer, numberOfBytes);
m_Ofstream.write(tempBuffer.get(), numberOfBytes);
}
delete[] tempBuffer;
}
else if (m_ByteOrder == IOByteOrderEnum::BigEndian)
{
auto * tempBuffer = new char[numberOfBytes];
memcpy(tempBuffer, buffer, numberOfBytes);
SwapBytesIfNecessary(tempBuffer, numberOfComponents);
const auto tempBuffer = make_unique_for_overwrite<char[]>(numberOfBytes);
memcpy(tempBuffer.get(), buffer, numberOfBytes);
SwapBytesIfNecessary(tempBuffer.get(), numberOfComponents);
if (m_IsCompressed)
{
gzwrite(m_Internal->m_GzFile, tempBuffer, numberOfBytes);
gzwrite(m_Internal->m_GzFile, tempBuffer.get(), numberOfBytes);
}
else
{
m_Ofstream.write(tempBuffer, numberOfBytes);
m_Ofstream.write(tempBuffer.get(), numberOfBytes);
}
delete[] tempBuffer;
}
else
{
Expand Down
11 changes: 5 additions & 6 deletions Modules/IO/ImageBase/src/itkNumericSeriesFileNames.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkNumericSeriesFileNames.h"
#include "itkMakeUniqueForOverwrite.h"
#include <cstdio>

namespace itk
Expand Down Expand Up @@ -55,19 +56,17 @@ NumericSeriesFileNames::GetFileNames()
// absurdly long integer string.
}
OffsetValueType bufflen = nchars + 1;
auto * temp = new char[bufflen];
OffsetValueType result = snprintf(temp, bufflen, m_SeriesFormat.c_str(), i);
const auto temp = make_unique_for_overwrite<char[]>(bufflen);
OffsetValueType result = snprintf(temp.get(), bufflen, m_SeriesFormat.c_str(), i);
if (result < 0 || result >= bufflen)
{
std::stringstream message_cache;
message_cache << "The filename is too long for temp buffer."
<< " Truncated form: " << temp << "." << std::endl
<< " Truncated form: " << temp.get() << "." << std::endl
<< "nchars: " << nchars << " bufflen: " << bufflen << " result: " << result;
delete[] temp;
itkExceptionMacro(<< message_cache.str());
}
std::string fileName(temp);
delete[] temp;
std::string fileName(temp.get());
m_FileNames.push_back(fileName);
}
return m_FileNames;
Expand Down
19 changes: 9 additions & 10 deletions Modules/IO/ImageBase/src/itkRawImageIOUtilities.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/
#include "itkImageIOBase.h"
#include "itkByteSwapper.h"
#include "itkMakeUniqueForOverwrite.h"

namespace
{
Expand All @@ -34,19 +35,17 @@ _WriteRawBytesAfterSwappingUtility(const void * buffer,
const itk::SizeValueType numberOfPixels = numberOfBytes / (sizeof(TStrongType));
if (byteOrder == itk::IOByteOrderEnum::LittleEndian && InternalByteSwapperType::SystemIsBigEndian())
{
auto * tempBuffer = new TStrongType[numberOfPixels];
memcpy((char *)tempBuffer, buffer, numberOfBytes);
InternalByteSwapperType::SwapRangeFromSystemToLittleEndian((TStrongType *)tempBuffer, numberOfComponents);
file.write((char *)tempBuffer, numberOfBytes);
delete[] tempBuffer;
const auto tempBuffer = itk::make_unique_for_overwrite<TStrongType[]>(numberOfPixels);
memcpy(tempBuffer.get(), buffer, numberOfBytes);
InternalByteSwapperType::SwapRangeFromSystemToLittleEndian(tempBuffer.get(), numberOfComponents);
file.write(reinterpret_cast<char *>(tempBuffer.get()), numberOfBytes);
}
else if (byteOrder == itk::IOByteOrderEnum::BigEndian && InternalByteSwapperType::SystemIsLittleEndian())
{
auto * tempBuffer = new TStrongType[numberOfPixels];
memcpy((char *)tempBuffer, buffer, numberOfBytes);
InternalByteSwapperType::SwapRangeFromSystemToBigEndian((TStrongType *)tempBuffer, numberOfComponents);
file.write((char *)tempBuffer, numberOfBytes);
delete[] tempBuffer;
const auto tempBuffer = itk::make_unique_for_overwrite<TStrongType[]>(numberOfPixels);
memcpy(tempBuffer.get(), buffer, numberOfBytes);
InternalByteSwapperType::SwapRangeFromSystemToBigEndian(tempBuffer.get(), numberOfComponents);
file.write(reinterpret_cast<char *>(tempBuffer.get()), numberOfBytes);
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions Modules/IO/LSM/src/itkLSMImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*=========================================================================*/
#include "itkLSMImageIO.h"
#include "itkByteSwapper.h"
#include "itkMakeUniqueForOverwrite.h"

#include "itk_tiff.h"

Expand Down Expand Up @@ -301,16 +302,15 @@ LSMImageIO::Write(const void * buffer)
{
// if number of scalar components is greater than 3, that means we assume
// there is alpha.
uint16_t extra_samples = scomponents - 3;
auto * sample_info = new uint16_t[scomponents - 3];
uint16_t extra_samples = scomponents - 3;
const auto sample_info = make_unique_for_overwrite<uint16_t[]>(scomponents - 3);
sample_info[0] = EXTRASAMPLE_ASSOCALPHA;
int cc;
for (cc = 1; cc < scomponents - 3; ++cc)
{
sample_info[cc] = EXTRASAMPLE_UNSPECIFIED;
}
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, extra_samples, sample_info);
delete[] sample_info;
TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, extra_samples, sample_info.get());
}

uint16_t compression;
Expand Down
Loading

0 comments on commit 15d09d6

Please sign in to comment.