Skip to content

Commit

Permalink
misc sonarlint cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Aug 5, 2022
1 parent 62d07ec commit 8909dd0
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 216 deletions.
10 changes: 4 additions & 6 deletions src/basicio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,11 @@ void FileIo::transfer(BasicIo& src) {
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::stat") << "\n";
#endif
}
if (statOk && origStMode != buf2.st_mode) {
// Set original file permissions
if (::chmod(pf, origStMode) == -1) {
// Set original file permissions
if (statOk && origStMode != buf2.st_mode && ::chmod(pf, origStMode) == -1) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::chmod") << "\n";
EXV_WARNING << Error(ErrorCode::kerCallFailed, pf, strError(), "::chmod") << "\n";
#endif
}
}
}
} // if (fileIo)
Expand Down Expand Up @@ -1316,7 +1314,7 @@ byte* RemoteIo::mmap(bool /*isWriteable*/) {
size_t blocks = (p_->size_ + blockSize - 1) / blockSize;
bigBlock_ = new byte[blocks * blockSize];
for (size_t block = 0; block < blocks; block++) {
void* p = p_->blocksMap_[block].getData();
auto p = p_->blocksMap_[block].getData();
if (p) {
size_t nRead = block == (blocks - 1) ? p_->size_ - nRealData : blockSize;
memcpy(bigBlock_ + (block * blockSize), p, nRead);
Expand Down
39 changes: 21 additions & 18 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ std::string BmffImage::uuidName(Exiv2::DataBuf& uuid) {
const char* uuidCano = "\x85\xC0\xB6\x87\x82\xF\x11\xE0\x81\x11\xF4\xCE\x46\x2B\x6A\x48";
const char* uuidXmp = "\xBE\x7A\xCF\xCB\x97\xA9\x42\xE8\x9C\x71\x99\x94\x91\xE3\xAF\xAC";
const char* uuidCanp = "\xEA\xF4\x2B\x5E\x1C\x98\x4B\x88\xB9\xFB\xB7\xDC\x40\x6E\x4D\x16";
const char* result = uuid.cmpBytes(0, uuidCano, 16) == 0 ? "cano"
: uuid.cmpBytes(0, uuidXmp, 16) == 0 ? "xmp"
: uuid.cmpBytes(0, uuidCanp, 16) == 0 ? "canp"
: "";
return result;
if (uuid.cmpBytes(0, uuidCano, 16) == 0)
return "cano";
if (uuid.cmpBytes(0, uuidXmp, 16) == 0)
return "xmp";
if (uuid.cmpBytes(0, uuidCanp, 16) == 0)
return "canp";
return "";
}

uint64_t BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintStructureOption option /* = kpsNone */,
Expand Down Expand Up @@ -345,9 +347,13 @@ uint64_t BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintS
enforce(data.size() - skip >= (version > 2u ? 4u : 2u), Exiv2::ErrorCode::kerCorruptedMetadata);
enforce(data.size() - skip >= step, Exiv2::ErrorCode::kerCorruptedMetadata);
uint32_t ID = version > 2 ? data.read_uint32(skip, endian_) : data.read_uint16(skip, endian_);
uint32_t offset = step == 14 || step == 16 ? data.read_uint32(skip + step - 8, endian_)
: step == 18 ? data.read_uint32(skip + 4, endian_)
: 0;
auto offset = [=] {
if (step == 14 || step == 16)
return data.read_uint32(skip + step - 8, endian_);
if (step == 18)
return data.read_uint32(skip + 4, endian_);
return 0u;
}();

uint32_t ldata = data.read_uint32(skip + step - 4, endian_);
if (bTrace) {
Expand Down Expand Up @@ -388,7 +394,7 @@ uint64_t BmffImage::boxHandler(std::ostream& out /* = std::cout*/, Exiv2::PrintS
uint8_t meth = data.read_uint8(skip + 0);
uint8_t prec = data.read_uint8(skip + 1);
uint8_t approx = data.read_uint8(skip + 2);
std::string colour_type = std::string(data.c_str(), 4);
auto colour_type = std::string(data.c_str(), 4);
skip += 4;
if (colour_type == "rICC" || colour_type == "prof") {
DataBuf profile(data.c_data(skip), data.size() - skip);
Expand Down Expand Up @@ -554,14 +560,11 @@ void BmffImage::parseCr3Preview(DataBuf& data, std::ostream& out, bool bTrace, u
nativePreview.height_ = data.read_uint16(height_offset, endian_);
nativePreview.size_ = data.read_uint32(size_offset, endian_);
nativePreview.filter_ = "";
switch (version) {
case 0:
nativePreview.mimeType_ = "image/jpeg";
break;
default:
nativePreview.mimeType_ = "application/octet-stream";
break;
}
nativePreview.mimeType_ = [version] {
if (version == 0)
return "image/jpeg";
return "application/octet-stream";
}();
nativePreviews_.push_back(nativePreview);

if (bTrace) {
Expand Down Expand Up @@ -639,7 +642,7 @@ void BmffImage::printStructure(std::ostream& out, Exiv2::PrintStructureOption op
io_->seek(address, BasicIo::beg);
address = boxHandler(out, option, file_end, depth);
}
}; break;
} break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/canonmn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,7 @@ std::ostream& printCsLensFFFF(std::ostream& os, const Value& value, const ExifDa
return os << "Canon EF-S 24mm f/2.8 STM";
}
} catch (const std::exception&) {
};
}

return EXV_PRINT_TAG(canonCsLensType)(os, value, metadata);
}
Expand Down
108 changes: 45 additions & 63 deletions src/epsimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void findXmp(size_t& xmpPos, size_t& xmpSize, const byte* data, size_t startPos,
if (data[xmpPos] != '\x00' && data[xmpPos] != '<')
continue;
for (auto&& xmpTrailer : xmpTrailers) {
auto [trailer, readOnly] = xmpTrailer;
const auto& [trailer, readOnly] = xmpTrailer;

if (trailerPos + trailer.size() > size)
continue;
Expand Down Expand Up @@ -524,14 +524,12 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
#endif
}
}
if (line == "%%EOF" || line == "%%Trailer") {
if (posPageTrailer == posEndEps) {
posPageTrailer = startPos;
implicitPageTrailer = true;
if ((line == "%%EOF" || line == "%%Trailer") && posPageTrailer == posEndEps) {
posPageTrailer = startPos;
implicitPageTrailer = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit PageTrailer at position: " << startPos << "\n";
EXV_DEBUG << "readWriteEpsMetadata: Found implicit PageTrailer at position: " << startPos << "\n";
#endif
}
}
// remaining explicit comments
if (posEndComments == posEndEps && posLanguageLevel == posEndEps && startsWith(line, "%%LanguageLevel:")) {
Expand Down Expand Up @@ -702,7 +700,7 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
if (posOtherXmp >= posEndPageSetup)
break;
bool isRemovableEmbedding = false;
for (auto&& [r, s] : removableEmbeddings) {
for (const auto& [r, s] : removableEmbeddings) {
if (r <= posOtherXmp && posOtherXmp < s) {
isRemovableEmbedding = true;
break;
Expand Down Expand Up @@ -871,23 +869,20 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
#endif
}
// update and complement DSC comments
if (pos == posLanguageLevel && posLanguageLevel != posEndEps && !deleteXmp && !useFlexibleEmbedding) {
if (line == "%%LanguageLevel:1" || line == "%%LanguageLevel: 1") {
writeTemp(tempIo, "%%LanguageLevel: 2" + lineEnding);
skipPos = posLineEnd;
if (pos == posLanguageLevel && posLanguageLevel != posEndEps && !deleteXmp && !useFlexibleEmbedding &&
(line == "%%LanguageLevel:1" || line == "%%LanguageLevel: 1")) {
writeTemp(tempIo, "%%LanguageLevel: 2" + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
}
if (pos == posContainsXmp && posContainsXmp != posEndEps) {
if (line != containsXmpLine) {
writeTemp(tempIo, containsXmpLine + lineEnding);
skipPos = posLineEnd;
if (pos == posContainsXmp && posContainsXmp != posEndEps && line != containsXmpLine) {
writeTemp(tempIo, containsXmpLine + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
}
if (pos == posExiv2Version && posExiv2Version != posEndEps) {
writeTemp(tempIo, "%Exiv2Version: " + versionNumberHexString() + lineEnding);
Expand Down Expand Up @@ -924,29 +919,23 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
writeTemp(tempIo, "%%EndComments" + lineEnding);
}
}
if (pos == posPage) {
if (!startsWith(line, "%%Page:")) {
writeTemp(tempIo, "%%Page: 1 1" + lineEnding);
writeTemp(tempIo, "%%EndPageComments" + lineEnding);
}
if (pos == posPage && !startsWith(line, "%%Page:")) {
writeTemp(tempIo, "%%Page: 1 1" + lineEnding);
writeTemp(tempIo, "%%EndPageComments" + lineEnding);
}
if (pos == posBeginPageSetup) {
if (line != "%%BeginPageSetup") {
writeTemp(tempIo, "%%BeginPageSetup" + lineEnding);
}
if (pos == posBeginPageSetup && line != "%%BeginPageSetup") {
writeTemp(tempIo, "%%BeginPageSetup" + lineEnding);
}
if (useFlexibleEmbedding) {
// insert XMP metadata into existing flexible embedding
if (pos == xmpPos) {
if (fixBeginXmlPacket) {
writeTemp(tempIo, "%begin_xml_packet: " + toString(xmpPacket.size()) + lineEnding);
}
writeTemp(tempIo, xmpPacket);
skipPos += xmpSize;
// insert XMP metadata into existing flexible embedding
if (useFlexibleEmbedding && pos == xmpPos) {
if (fixBeginXmlPacket) {
writeTemp(tempIo, "%begin_xml_packet: " + toString(xmpPacket.size()) + lineEnding);
}
writeTemp(tempIo, xmpPacket);
skipPos += xmpSize;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
}
if (!useFlexibleEmbedding) {
// remove preceding embedding(s)
Expand Down Expand Up @@ -1009,26 +998,21 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
writeTemp(tempIo, "%Exiv2EndXMP" + lineEnding);
}
}
if (pos == posEndPageSetup) {
if (line != "%%EndPageSetup") {
writeTemp(tempIo, "%%EndPageSetup" + lineEnding);
}
if (pos == posEndPageSetup && line != "%%EndPageSetup") {
writeTemp(tempIo, "%%EndPageSetup" + lineEnding);
}
if (!useFlexibleEmbedding) {
if (pos == posPageTrailer && !deleteXmp) {
if (!implicitPageTrailer) {
skipPos = posLineEnd;
if (!useFlexibleEmbedding && pos == posPageTrailer && !deleteXmp) {
if (!implicitPageTrailer) {
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__
<< "\n";
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
writeTemp(tempIo, "%%PageTrailer" + lineEnding);
writeTemp(tempIo, "%Exiv2BeginXMP: After %%PageTrailer" + lineEnding);
writeTemp(tempIo, "[/EMC Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[/NamespacePop Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "%Exiv2EndXMP" + lineEnding);
}
writeTemp(tempIo, "%%PageTrailer" + lineEnding);
writeTemp(tempIo, "%Exiv2BeginXMP: After %%PageTrailer" + lineEnding);
writeTemp(tempIo, "[/EMC Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[/NamespacePop Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "%Exiv2EndXMP" + lineEnding);
}
// add EOF comment if necessary
if (pos == posEndEps && posEof == posEndEps) {
Expand Down Expand Up @@ -1080,18 +1064,16 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
namespace Exiv2 {
EpsImage::EpsImage(BasicIo::UniquePtr io, bool create) : Image(ImageType::eps, mdXmp, std::move(io)) {
// LogMsg::setLevel(LogMsg::debug);
if (create) {
if (io_->open() == 0) {
if (create && io_->open() == 0) {
#ifdef DEBUG
EXV_DEBUG << "Exiv2::EpsImage:: Creating blank EPS image\n";
EXV_DEBUG << "Exiv2::EpsImage:: Creating blank EPS image\n";
#endif
IoCloser closer(*io_);
if (io_->write(reinterpret_cast<const byte*>(epsBlank.data()), epsBlank.size()) != epsBlank.size()) {
IoCloser closer(*io_);
if (io_->write(reinterpret_cast<const byte*>(epsBlank.data()), epsBlank.size()) != epsBlank.size()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to write blank EPS image.\n";
EXV_WARNING << "Failed to write blank EPS image.\n";
#endif
throw Error(ErrorCode::kerImageWriteFailed);
}
throw Error(ErrorCode::kerImageWriteFailed);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/exif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ std::ostream& Exifdatum::write(std::ostream& os, const ExifData* pMetadata) cons
// cause a std::out_of_range exception to be thrown.
try {
fct(os, value(), pMetadata);
} catch (std::out_of_range&) {
} catch (const std::out_of_range&) {
os << "Bad value";
#ifdef EXIV2_DEBUG_MESSAGES
std::cerr << "Caught std::out_of_range exception in Exifdatum::write().\n";
Expand Down
2 changes: 1 addition & 1 deletion src/fujimn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ constexpr TagDetails fujiContrast[] = {{0, N_("Normal")}, {256, N_("High")}, {76
//! WhiteBalanceFineTune, tag 0x100a
std::ostream& printFujiWhiteBalanceFineTune(std::ostream& os, const Value& value, const ExifData*) {
if (value.typeId() == signedLong && value.size() == 8) {
auto longValue = static_cast<const LongValue&>(value);
auto longValue = dynamic_cast<const LongValue&>(value);
if (longValue.toInt64(0) % 20 == 0 && longValue.toInt64(1) % 20 == 0) {
auto redShift = longValue.toInt64(0) / 20;
auto blueShift = longValue.toInt64(1) / 20;
Expand Down
2 changes: 1 addition & 1 deletion src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ XmpData& Image::xmpData() {

std::string& Image::xmpPacket() {
// Serialize the current XMP
if (xmpData_.count() > 0 && !writeXmpFromPacket()) {
if (!xmpData_.empty() && !writeXmpFromPacket()) {
XmpParser::encode(xmpPacket_, xmpData_, XmpParser::useCompactFormat | XmpParser::omitAllFormatting);
}
return xmpPacket_;
Expand Down
9 changes: 3 additions & 6 deletions src/iptc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,8 @@ const char* IptcData::detectCharset() const {
auto pos = findKey(IptcKey("Iptc.Envelope.CharacterSet"));
if (pos != end()) {
const std::string value = pos->toString();
if (pos->value().ok()) {
if (value == "\033%G")
return "UTF-8";
// other values are probably not practically relevant
}
if (pos->value().ok() && value == "\033%G")
return "UTF-8";
}

bool ascii = true;
Expand All @@ -320,7 +317,7 @@ const char* IptcData::detectCharset() const {
std::string value = pos->toString();
if (pos->value().ok()) {
int seqCount = 0;
for (auto&& c : value) {
for (auto c : value) {
if (seqCount) {
if ((c & 0xc0) != 0x80) {
utf8 = false;
Expand Down
6 changes: 2 additions & 4 deletions src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,10 @@ void Jp2Image::doWriteMetadata(BasicIo& outIo) {
}
}

if (!writeXmpFromPacket()) {
if (XmpParser::encode(xmpPacket_, xmpData_) > 1) {
if (!writeXmpFromPacket() && XmpParser::encode(xmpPacket_, xmpData_) > 1) {
#ifndef SUPPRESS_WARNINGS
EXV_ERROR << "Failed to encode XMP metadata." << std::endl;
EXV_ERROR << "Failed to encode XMP metadata." << std::endl;
#endif
}
}
if (!xmpPacket_.empty()) {
// Update Xmp data to a new UUID box
Expand Down
9 changes: 4 additions & 5 deletions src/jpgimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void JpegBase::readMetadata() {
// Read the beginning of the next segment
try {
marker = advanceToMarker(ErrorCode::kerFailedToReadImageData);
} catch (Error&) {
} catch (const Error&) {
rc = 5;
break;
}
Expand Down Expand Up @@ -742,12 +742,11 @@ void JpegBase::doWriteMetadata(BasicIo& outIo) {
--search;
}
}
if (!writeXmpFromPacket()) {
if (XmpParser::encode(xmpPacket_, xmpData_, XmpParser::useCompactFormat | XmpParser::omitAllFormatting) > 1) {
if (!writeXmpFromPacket() &&
XmpParser::encode(xmpPacket_, xmpData_, XmpParser::useCompactFormat | XmpParser::omitAllFormatting) > 1) {
#ifndef SUPPRESS_WARNINGS
EXV_ERROR << "Failed to encode XMP metadata.\n";
EXV_ERROR << "Failed to encode XMP metadata.\n";
#endif
}
}
if (!xmpPacket_.empty()) {
std::array<byte, 33> tmpBuf;
Expand Down
5 changes: 2 additions & 3 deletions src/makernote_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string getExiv2ConfigPath() {
currentPath = buffer;
}
#else
struct passwd* pw = getpwuid(getuid());
auto pw = getpwuid(getuid());
currentPath = std::string(pw ? pw->pw_dir : "");
#endif
return (currentPath / inifile).string();
Expand Down Expand Up @@ -815,9 +815,8 @@ TiffComponent* newSony2Mn2(uint16_t tag, IfdId group, IfdId mnGroup) {

TiffComponent* newCasioMn(uint16_t tag, IfdId group, IfdId /* mnGroup*/, const byte* pData, size_t size,
ByteOrder /* byteOrder */) {
if (size > 6 && std::string(reinterpret_cast<const char*>(pData), 6) == std::string("QVC\0\0\0", 6)) {
if (size > 6 && std::string(reinterpret_cast<const char*>(pData), 6) == std::string("QVC\0\0\0", 6))
return newCasio2Mn2(tag, group, IfdId::casio2Id);
};
// Require at least an IFD with 1 entry, but not necessarily a next pointer
if (size < 14)
return nullptr;
Expand Down
Loading

0 comments on commit 8909dd0

Please sign in to comment.