Skip to content

Commit

Permalink
fix some NRVO bugs
Browse files Browse the repository at this point in the history
Found with GCC's -Wnrvo.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Dec 7, 2024
1 parent 1f97a0a commit deb87c3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/iptc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,11 @@ int IptcParser::decode(IptcData& iptcData, const byte* pData, size_t size) {
} // IptcParser::decode

DataBuf IptcParser::encode(const IptcData& iptcData) {
DataBuf buf;
if (iptcData.empty())
return {};
return buf;

DataBuf buf(iptcData.size());
buf = DataBuf(iptcData.size());
byte* pWrite = buf.data();

// Copy the iptc data sets and sort them by record but preserve the order of datasets
Expand Down
4 changes: 2 additions & 2 deletions src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ Jp2Image::Jp2Image(BasicIo::UniquePtr io, bool create) : Image(ImageType::jp2, m
// Obtains the ascii version from the box.type
std::string Jp2Image::toAscii(uint32_t n) {
const auto p = reinterpret_cast<const char*>(&n);
if (isBigEndianPlatform())
return std::string(p, p + 4);
std::string result(p, p + 4);
if (isBigEndianPlatform())
return result;
std::reverse(result.begin(), result.end());
return result;
}
Expand Down
18 changes: 9 additions & 9 deletions src/pngchunk_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ std::string PngChunk::makeUtf8TxtChunk(const std::string& keyword, const std::st
} // PngChunk::makeUtf8TxtChunk

DataBuf PngChunk::readRawProfile(const DataBuf& text, bool iTXt) {
DataBuf info;
if (text.size() <= 1) {
return {};
return info;
}

DataBuf info;
const unsigned char unhex[103] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -488,26 +488,26 @@ DataBuf PngChunk::readRawProfile(const DataBuf& text, bool iTXt) {
const char* eot = text.c_str(text.size() - 1); // end of text

if (sp >= eot) {
return {};
return info;
}

// Look for newline
while (*sp != '\n') {
sp++;
if (sp == eot) {
return {};
return info;
}
}
sp++; // step over '\n'
if (sp == eot) {
return {};
return info;
}

// Look for length
while (*sp == '\0' || *sp == ' ' || *sp == '\n') {
sp++;
if (sp == eot) {
return {};
return info;
}
}

Expand All @@ -519,12 +519,12 @@ DataBuf PngChunk::readRawProfile(const DataBuf& text, bool iTXt) {
length = newlength;
sp++;
if (sp == eot) {
return {};
return info;
}
}
sp++; // step over '\n'
if (sp == eot) {
return {};
return info;
}

enforce(length <= static_cast<size_t>(eot - sp) / 2, Exiv2::ErrorCode::kerCorruptedMetadata);
Expand All @@ -540,7 +540,7 @@ DataBuf PngChunk::readRawProfile(const DataBuf& text, bool iTXt) {
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Exiv2::PngChunk::readRawProfile: Unable To Copy Raw Profile: cannot allocate memory\n";
#endif
return {};
return info;
}

if (info.empty()) // Early return
Expand Down
30 changes: 17 additions & 13 deletions src/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,16 @@ const LoaderTiff::Param LoaderTiff::param_[] = {
};

Loader::UniquePtr Loader::create(PreviewId id, const Image& image) {
Loader::UniquePtr loader;
if (id < 0 || id >= Loader::getNumLoaders())
return nullptr;
return loader;

if (loaderList_[id].imageMimeType_ && std::string(loaderList_[id].imageMimeType_) != image.mimeType())
return nullptr;
return loader;

auto loader = loaderList_[id].create_(id, image, loaderList_[id].parIdx_);

if (loader && !loader->valid())
return nullptr;
loader = loaderList_[id].create_(id, image, loaderList_[id].parIdx_);
if (!loader->valid())
loader = nullptr;

return loader;
}
Expand Down Expand Up @@ -568,11 +568,13 @@ PreviewProperties LoaderExifDataJpeg::getProperties() const {
}

DataBuf LoaderExifDataJpeg::getData() const {
DataBuf buf;

if (!valid())
return {};
return buf;

if (auto pos = image_.exifData().findKey(dataKey_); pos != image_.exifData().end()) {
DataBuf buf = pos->dataArea(); // indirect data
buf = pos->dataArea(); // indirect data

if (buf.empty()) { // direct data
buf = DataBuf(pos->size());
Expand All @@ -583,7 +585,7 @@ DataBuf LoaderExifDataJpeg::getData() const {
return buf;
}

return {};
return buf;
}

bool LoaderExifDataJpeg::readDimensions() {
Expand Down Expand Up @@ -854,6 +856,7 @@ DataBuf decodeHex(const byte* src, size_t srcSize) {
const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

DataBuf decodeBase64(const std::string& src) {
DataBuf dest;
// create decoding table
unsigned long invalid = 64;
auto decodeBase64Table = std::vector<unsigned long>(256, invalid);
Expand All @@ -864,11 +867,11 @@ DataBuf decodeBase64(const std::string& src) {
auto validSrcSize = static_cast<unsigned long>(
std::count_if(src.begin(), src.end(), [&](unsigned char c) { return decodeBase64Table.at(c) != invalid; }));
if (validSrcSize > ULONG_MAX / 3)
return {}; // avoid integer overflow
return dest; // avoid integer overflow
const unsigned long destSize = (validSrcSize * 3) / 4;

// allocate dest buffer
DataBuf dest(destSize);
dest = DataBuf(destSize);

// decode
for (unsigned long srcPos = 0, destPos = 0; destPos < destSize;) {
Expand Down Expand Up @@ -930,18 +933,19 @@ DataBuf decodeAi7Thumbnail(const DataBuf& src) {
}

DataBuf makePnm(size_t width, size_t height, const DataBuf& rgb) {
DataBuf dest;
if (size_t expectedSize = width * height * 3UL; rgb.size() != expectedSize) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Invalid size of preview data. Expected " << expectedSize << " bytes, got " << rgb.size()
<< " bytes.\n";
#endif
return {};
return dest;
}

const std::string header = "P6\n" + std::to_string(width) + " " + std::to_string(height) + "\n255\n";
const auto headerBytes = reinterpret_cast<const byte*>(header.data());

DataBuf dest(header.size() + rgb.size());
dest = DataBuf(header.size() + rgb.size());
std::copy_n(headerBytes, header.size(), dest.begin());
std::copy_n(rgb.c_data(), rgb.size(), dest.begin() + header.size());
return dest;
Expand Down
5 changes: 3 additions & 2 deletions src/tiffimage_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2101,12 +2101,13 @@ WriteMethod TiffParserWorker::encode(BasicIo& io, const byte* pData, size_t size

TiffComponent::UniquePtr TiffParserWorker::parse(const byte* pData, size_t size, uint32_t root,
TiffHeaderBase* pHeader) {
TiffComponent::UniquePtr rootDir;
if (!pData || size == 0)
return nullptr;
return rootDir;
if (!pHeader->read(pData, size) || pHeader->offset() >= size) {
throw Error(ErrorCode::kerNotAnImage, "TIFF");
}
auto rootDir = TiffCreator::create(root, IfdId::ifdIdNotSet);
rootDir = TiffCreator::create(root, IfdId::ifdIdNotSet);
if (rootDir) {
rootDir->setStart(pData + pHeader->offset());
auto state = TiffRwState{pHeader->byteOrder(), 0};
Expand Down

0 comments on commit deb87c3

Please sign in to comment.