diff --git a/app/exiv2.cpp b/app/exiv2.cpp index 4421c29f..18654f3b 100644 --- a/app/exiv2.cpp +++ b/app/exiv2.cpp @@ -937,10 +937,10 @@ int Params::nonoption(const std::string& argv) { return rc; } // Params::nonoption -static int readFileToBuf(FILE* f, Exiv2::DataBuf& buf) { +static size_t readFileToBuf(FILE* f, Exiv2::DataBuf& buf) { const int buff_size = 4 * 1028; std::vector bytes(buff_size); - int nBytes = 0; + size_t nBytes = 0; bool more{true}; std::array buff; while (more) { diff --git a/include/exiv2/webpimage.hpp b/include/exiv2/webpimage.hpp index 2d56706c..6b84d939 100644 --- a/include/exiv2/webpimage.hpp +++ b/include/exiv2/webpimage.hpp @@ -64,7 +64,7 @@ class EXIV2API WebPImage : public Image { void doWriteMetadata(BasicIo& outIo); //! @name NOT Implemented //@{ - static long getHeaderOffset(const byte* data, long data_size, const byte* header, long header_size); + static long getHeaderOffset(const byte* data, size_t data_size, const byte* header, size_t header_size); static bool equalsWebPTag(Exiv2::DataBuf& buf, const char* str); void debugPrintHex(byte* data, long size); void decodeChunks(long filesize); diff --git a/src/basicio.cpp b/src/basicio.cpp index 361f52aa..27c3d0ec 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -728,7 +728,7 @@ void MemIo::transfer(BasicIo& src) { } size_t MemIo::write(BasicIo& src) { - if (static_cast(this) == &src) + if (this == &src) return 0; if (!src.isopen()) return 0; diff --git a/src/bmffimage.cpp b/src/bmffimage.cpp index a293179c..b71dbad0 100644 --- a/src/bmffimage.cpp +++ b/src/bmffimage.cpp @@ -506,7 +506,7 @@ void BmffImage::parseTiff(uint32_t root_tag, uint64_t length) { if (length > 8) { enforce(length - 8 <= io_->size() - io_->tell(), ErrorCode::kerCorruptedMetadata); enforce(length - 8 <= std::numeric_limits::max(), ErrorCode::kerCorruptedMetadata); - DataBuf data(static_cast(length) - 8); + DataBuf data(static_cast(length - 8u)); const size_t bufRead = io_->read(data.data(), data.size()); if (io_->error()) @@ -514,8 +514,7 @@ void BmffImage::parseTiff(uint32_t root_tag, uint64_t length) { if (bufRead != data.size()) throw Error(ErrorCode::kerInputDataReadFailed); - Internal::TiffParserWorker::decode(exifData(), iptcData(), xmpData(), data.c_data(), - static_cast(data.size()), root_tag, + Internal::TiffParserWorker::decode(exifData(), iptcData(), xmpData(), data.c_data(), data.size(), root_tag, Internal::TiffMapping::findDecoder); } } @@ -526,13 +525,12 @@ void BmffImage::parseXmp(uint64_t length, uint64_t start) { enforce(length <= io_->size() - start, ErrorCode::kerCorruptedMetadata); long restore = io_->tell(); - enforce(start <= std::numeric_limits::max(), ErrorCode::kerCorruptedMetadata); io_->seek(static_cast(start), BasicIo::beg); - enforce(length < std::numeric_limits::max(), ErrorCode::kerCorruptedMetadata); - DataBuf xmp(static_cast(length + 1)); - xmp.write_uint8(static_cast(length), 0); // ensure xmp is null terminated! - if (io_->read(xmp.data(), static_cast(length)) != length) + size_t lengthSizeT = static_cast(length); + DataBuf xmp(lengthSizeT + 1); + xmp.write_uint8(lengthSizeT, 0); // ensure xmp is null terminated! + if (io_->read(xmp.data(), lengthSizeT) != lengthSizeT) throw Error(ErrorCode::kerInputDataReadFailed); if (io_->error()) throw Error(ErrorCode::kerFailedToReadImageData); @@ -554,7 +552,7 @@ void BmffImage::parseCr3Preview(DataBuf& data, std::ostream& out, bool bTrace, u enforce(here >= 0 && here <= std::numeric_limits::max() - static_cast(relative_position), ErrorCode::kerCorruptedMetadata); NativePreview nativePreview; - nativePreview.position_ = here + static_cast(relative_position); + nativePreview.position_ = here + relative_position; nativePreview.width_ = data.read_uint16(width_offset, endian_); nativePreview.height_ = data.read_uint16(height_offset, endian_); nativePreview.size_ = data.read_uint32(size_offset, endian_); diff --git a/src/crwimage_int.cpp b/src/crwimage_int.cpp index e807f9f9..be229e31 100644 --- a/src/crwimage_int.cpp +++ b/src/crwimage_int.cpp @@ -244,7 +244,7 @@ void CiffDirectory::readDirectory(const byte* pData, size_t size, ByteOrder byte std::cout << "Directory at offset " << std::dec << o << ", " << count << " entries \n"; #endif o += 2; - if (static_cast(count) * 10 > size - o) + if (count * 10u > size - o) throw Error(ErrorCode::kerCorruptedMetadata); for (uint16_t i = 0; i < count; ++i) { diff --git a/src/image.cpp b/src/image.cpp index f2d5873e..07a620a7 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -602,7 +602,7 @@ void Image::setComment(std::string_view comment) { void Image::setIccProfile(Exiv2::DataBuf&& iccProfile, bool bTestValid) { if (bTestValid) { - if (iccProfile.size() < static_cast(sizeof(long))) { + if (iccProfile.size() < sizeof(long)) { throw Error(ErrorCode::kerInvalidIccProfile); } const size_t size = iccProfile.read_uint32(0, bigEndian); diff --git a/src/mrwimage.cpp b/src/mrwimage.cpp index aaca5c78..2fc23b42 100644 --- a/src/mrwimage.cpp +++ b/src/mrwimage.cpp @@ -106,7 +106,7 @@ void MrwImage::readMetadata() { io_->read(buf.data(), buf.size()); enforce(!io_->error() && !io_->eof(), ErrorCode::kerFailedToReadImageData); - ByteOrder bo = TiffParser::decode(exifData_, iptcData_, xmpData_, buf.c_data(), static_cast(buf.size())); + ByteOrder bo = TiffParser::decode(exifData_, iptcData_, xmpData_, buf.c_data(), buf.size()); setByteOrder(bo); } // MrwImage::readMetadata diff --git a/src/pgfimage.cpp b/src/pgfimage.cpp index dcbfc3d0..7de1aa66 100644 --- a/src/pgfimage.cpp +++ b/src/pgfimage.cpp @@ -90,13 +90,13 @@ void PgfImage::readMetadata() { // And now, the most interesting, the user data byte array where metadata are stored as small image. enforce(headerSize <= std::numeric_limits::max() - 8, ErrorCode::kerCorruptedMetadata); - long size = static_cast(headerSize) + 8 - io_->tell(); + size_t size = headerSize + 8 - static_cast(io_->tell()); #ifdef EXIV2_DEBUG_MESSAGES std::cout << "Exiv2::PgfImage::readMetadata: Found Image data (" << size << " bytes)\n"; #endif - if (size < 0 || static_cast(size) > io_->size()) + if (size > io_->size()) throw Error(ErrorCode::kerInputDataReadFailed); if (size == 0) return; diff --git a/src/preview.cpp b/src/preview.cpp index 3333b73b..7a33528b 100644 --- a/src/preview.cpp +++ b/src/preview.cpp @@ -31,7 +31,7 @@ bool cmpPreviewProperties(const PreviewProperties &lhs, const PreviewProperties } /// @brief Decode a Hex string. -DataBuf decodeHex(const byte *src, long srcSize); +DataBuf decodeHex(const byte *src, size_t srcSize); /// @brief Decode a Base64 string. DataBuf decodeBase64(const std::string &src); @@ -409,12 +409,12 @@ DataBuf LoaderNative::getData() const { return {data + nativePreview_.position_, nativePreview_.size_}; } if (nativePreview_.filter_ == "hex-ai7thumbnail-pnm") { - const DataBuf ai7thumbnail = decodeHex(data + nativePreview_.position_, static_cast(nativePreview_.size_)); + const DataBuf ai7thumbnail = decodeHex(data + nativePreview_.position_, nativePreview_.size_); const DataBuf rgb = decodeAi7Thumbnail(ai7thumbnail); return makePnm(width_, height_, rgb); } if (nativePreview_.filter_ == "hex-irb") { - const DataBuf psData = decodeHex(data + nativePreview_.position_, static_cast(nativePreview_.size_)); + const DataBuf psData = decodeHex(data + nativePreview_.position_, nativePreview_.size_); const byte *record; uint32_t sizeHdr = 0; uint32_t sizeData = 0; @@ -479,7 +479,7 @@ LoaderExifJpeg::LoaderExifJpeg(PreviewId id, const Image &image, int parIdx) : L } } - if (Safe::add(offset_, size_) > static_cast(image_.io().size())) + if (Safe::add(offset_, size_) > image_.io().size()) return; valid_ = true; @@ -648,7 +648,7 @@ LoaderTiff::LoaderTiff(PreviewId id, const Image &image, int parIdx) : if (offsetCount != pos->value().count()) return; for (size_t i = 0; i < offsetCount; i++) { - size_ += pos->toUint32(static_cast(i)); + size_ += pos->toUint32(i); } if (size_ == 0) @@ -727,7 +727,7 @@ DataBuf LoaderTiff::getData() const { dataValue.setDataArea(base + offset, size); } else { // FIXME: the buffer is probably copied twice, it should be optimized - enforce(size_ <= static_cast(io.size()), ErrorCode::kerCorruptedMetadata); + enforce(size_ <= io.size(), ErrorCode::kerCorruptedMetadata); DataBuf buf(size_); uint32_t idxBuf = 0; for (size_t i = 0; i < sizes.count(); i++) { @@ -792,7 +792,7 @@ LoaderXmpJpeg::LoaderXmpJpeg(PreviewId id, const Image &image, int parIdx) : Loa width_ = widthDatum->toUint32(); height_ = heightDatum->toUint32(); preview_ = decodeBase64(imageDatum->toString()); - size_ = static_cast(preview_.size()); + size_ = preview_.size(); valid_ = true; } @@ -817,7 +817,7 @@ bool LoaderXmpJpeg::readDimensions() { return valid(); } -DataBuf decodeHex(const byte *src, long srcSize) { +DataBuf decodeHex(const byte *src, size_t srcSize) { // create decoding table byte invalid = 16; std::array decodeHexTable; @@ -831,17 +831,17 @@ DataBuf decodeHex(const byte *src, long srcSize) { // calculate dest size long validSrcSize = 0; - for (long srcPos = 0; srcPos < srcSize; srcPos++) { + for (size_t srcPos = 0; srcPos < srcSize; srcPos++) { if (decodeHexTable[src[srcPos]] != invalid) validSrcSize++; } - const long destSize = validSrcSize / 2; + const size_t destSize = validSrcSize / 2; // allocate dest buffer DataBuf dest(destSize); // decode - for (long srcPos = 0, destPos = 0; destPos < destSize; destPos++) { + for (size_t srcPos = 0, destPos = 0; destPos < destSize; destPos++) { byte buffer = 0; for (int bufferPos = 1; bufferPos >= 0 && srcPos < srcSize; srcPos++) { byte srcValue = decodeHexTable[src[srcPos]]; @@ -880,7 +880,7 @@ DataBuf decodeBase64(const std::string &src) { // allocate dest buffer if (destSize > LONG_MAX) return {}; // avoid integer overflow - DataBuf dest(static_cast(destSize)); + DataBuf dest(destSize); // decode for (unsigned long srcPos = 0, destPos = 0; destPos < destSize;) { @@ -901,7 +901,7 @@ DataBuf decodeBase64(const std::string &src) { DataBuf decodeAi7Thumbnail(const DataBuf &src) { const byte *colorTable = src.c_data(); - const long colorTableSize = 256 * 3; + const size_t colorTableSize = 256 * 3; if (src.size() < colorTableSize) { #ifndef SUPPRESS_WARNINGS EXV_WARNING << "Invalid size of AI7 thumbnail: " << src.size() << "\n"; @@ -909,10 +909,10 @@ DataBuf decodeAi7Thumbnail(const DataBuf &src) { return {}; } const byte *imageData = src.c_data(colorTableSize); - const long imageDataSize = static_cast(src.size()) - colorTableSize; + const size_t imageDataSize = src.size() - colorTableSize; const bool rle = (imageDataSize >= 3 && imageData[0] == 'R' && imageData[1] == 'L' && imageData[2] == 'E'); std::string dest; - for (long i = rle ? 3 : 0; i < imageDataSize;) { + for (size_t i = rle ? 3 : 0; i < imageDataSize;) { byte num = 1; byte value = imageData[i++]; if (rle && value == 0xFD) { @@ -1029,8 +1029,8 @@ PreviewPropertiesList PreviewManager::getPreviewProperties() const { auto loader = Loader::create(id, image_); if (loader && loader->readDimensions()) { PreviewProperties props = loader->getProperties(); - DataBuf buf = loader->getData(); // #16 getPreviewImage() - props.size_ = static_cast(buf.size()); // update the size + DataBuf buf = loader->getData(); // #16 getPreviewImage() + props.size_ = buf.size(); // update the size list.push_back(props); } } diff --git a/src/psdimage.cpp b/src/psdimage.cpp index c3b6bebc..7f0c8155 100644 --- a/src/psdimage.cpp +++ b/src/psdimage.cpp @@ -217,7 +217,7 @@ void PsdImage::readResourceBlock(uint16_t resourceId, uint32_t resourceSize) { io_->read(rawIPTC.data(), rawIPTC.size()); if (io_->error() || io_->eof()) throw Error(ErrorCode::kerFailedToReadImageData); - if (IptcParser::decode(iptcData_, rawIPTC.c_data(), static_cast(rawIPTC.size()))) { + if (IptcParser::decode(iptcData_, rawIPTC.c_data(), rawIPTC.size())) { #ifndef SUPPRESS_WARNINGS EXV_WARNING << "Failed to decode IPTC metadata.\n"; #endif @@ -484,8 +484,7 @@ void PsdImage::doWriteMetadata(BasicIo& outIo) { readTotal = 0; while (readTotal < pResourceSize) { /// \todo almost same code as in lines 403-410. Factor out & reuse! - size_t toRead = (pResourceSize - readTotal) < lbuf.size() ? static_cast(pResourceSize - readTotal) - : static_cast(lbuf.size()); + size_t toRead = (pResourceSize - readTotal) < lbuf.size() ? pResourceSize - readTotal : lbuf.size(); if (io_->read(lbuf.data(), toRead) != toRead) { throw Error(ErrorCode::kerNotAnImage, "Photoshop"); } diff --git a/src/tiffcomposite_int.cpp b/src/tiffcomposite_int.cpp index 6f77f8f0..54a76e2c 100644 --- a/src/tiffcomposite_int.cpp +++ b/src/tiffcomposite_int.cpp @@ -217,7 +217,7 @@ void TiffEntryBase::setData(const std::shared_ptr& buf) { size_ = buf->size(); } -void TiffEntryBase::setData(byte* pData, uint32_t size, const std::shared_ptr& storage) { +void TiffEntryBase::setData(byte* pData, size_t size, const std::shared_ptr& storage) { pData_ = pData; size_ = size; storage_ = storage; @@ -352,7 +352,7 @@ uint32_t TiffIfdMakernote::baseOffset() const { return pHeader_->baseOffset(mnOffset_); } -bool TiffIfdMakernote::readHeader(const byte* pData, uint32_t size, ByteOrder byteOrder) { +bool TiffIfdMakernote::readHeader(const byte* pData, size_t size, ByteOrder byteOrder) { if (!pHeader_) return true; return pHeader_->read(pData, size, byteOrder); diff --git a/src/tiffcomposite_int.hpp b/src/tiffcomposite_int.hpp index 7a8b1fed..ce412879 100644 --- a/src/tiffcomposite_int.hpp +++ b/src/tiffcomposite_int.hpp @@ -422,7 +422,7 @@ class TiffEntryBase : public TiffComponent { you should pass std::shared_ptr(), which is essentially a nullptr. */ - void setData(byte* pData, uint32_t size, const std::shared_ptr& storage); + void setData(byte* pData, size_t size, const std::shared_ptr& storage); /*! @brief Set the entry's data buffer. A shared_ptr is used to manage the DataBuf because TiffEntryBase has a clone method so it is possible (in theory) for @@ -1113,7 +1113,7 @@ class TiffIfdMakernote : public TiffComponent { The default implementation simply returns true. */ - bool readHeader(const byte* pData, uint32_t size, ByteOrder byteOrder); + bool readHeader(const byte* pData, size_t size, ByteOrder byteOrder); /*! @brief Set the byte order for the makernote. */ diff --git a/src/tiffimage.cpp b/src/tiffimage.cpp index af047cac..3d80c6d5 100644 --- a/src/tiffimage.cpp +++ b/src/tiffimage.cpp @@ -220,8 +220,7 @@ ByteOrder TiffParser::decode(ExifData& exifData, IptcData& iptcData, XmpData& xm } } - return TiffParserWorker::decode(exifData, iptcData, xmpData, pData, static_cast(size), root, - TiffMapping::findDecoder); + return TiffParserWorker::decode(exifData, iptcData, xmpData, pData, size, root, TiffMapping::findDecoder); } // TiffParser::decode WriteMethod TiffParser::encode(BasicIo& io, const byte* pData, size_t size, ByteOrder byteOrder, diff --git a/src/tiffvisitor_int.cpp b/src/tiffvisitor_int.cpp index 38e1fe09..4b097db7 100644 --- a/src/tiffvisitor_int.cpp +++ b/src/tiffvisitor_int.cpp @@ -310,7 +310,7 @@ void TiffDecoder::decodeIptc(const TiffEntryBase* object) { size_t size = 0; getObjData(pData, size, 0x83bb, ifd0Id, object); if (pData) { - if (0 == IptcParser::decode(iptcData_, pData, static_cast(size))) { + if (0 == IptcParser::decode(iptcData_, pData, size)) { return; } #ifndef SUPPRESS_WARNINGS @@ -516,7 +516,7 @@ void TiffEncoder::encodeIptc() { } else { buf = std::move(rawIptc); // Note: This resets rawIptc } - value->read(buf.data(), static_cast(buf.size()), byteOrder_); + value->read(buf.data(), buf.size(), byteOrder_); Exifdatum iptcDatum(iptcNaaKey, value.get()); exifData_.add(iptcDatum); pos = exifData_.findKey(irbKey); // needed after add() @@ -530,7 +530,7 @@ void TiffEncoder::encodeIptc() { exifData_.erase(pos); if (!irbBuf.empty()) { auto value = Value::create(unsignedByte); - value->read(irbBuf.data(), static_cast(irbBuf.size()), invalidByteOrder); + value->read(irbBuf.data(), irbBuf.size(), invalidByteOrder); Exifdatum iptcDatum(irbKey, value.get()); exifData_.add(iptcDatum); } @@ -559,7 +559,7 @@ void TiffEncoder::encodeXmp() { if (!xmpPacket.empty()) { // Set the XMP Exif tag to the new value auto value = Value::create(unsignedByte); - value->read(reinterpret_cast(&xmpPacket[0]), static_cast(xmpPacket.size()), invalidByteOrder); + value->read(reinterpret_cast(&xmpPacket[0]), xmpPacket.size(), invalidByteOrder); Exifdatum xmpDatum(xmpKey, value.get()); exifData_.add(xmpDatum); } @@ -697,7 +697,7 @@ void TiffEncoder::visitBinaryArrayEnd(TiffBinaryArray* object) { DataBuf buf = cryptFct(object->tag(), pData, size, pRoot_); if (!buf.empty()) { pData = buf.c_data(); - size = static_cast(buf.size()); + size = buf.size(); } if (!object->updOrigDataBuf(pData, size)) { setDirty(); @@ -837,7 +837,7 @@ void TiffEncoder::encodeImageEntry(TiffImageEntry* object, const Exifdatum* datu uint32_t sizeTotal = 0; object->strips_.clear(); for (size_t i = 0; i < pos->count(); ++i) { - uint32_t len = pos->toUint32(static_cast(i)); + uint32_t len = pos->toUint32(i); object->strips_.emplace_back(zero, len); sizeTotal += len; } @@ -1225,12 +1225,12 @@ void TiffReader::visitMnEntry(TiffMnEntry* object) { void TiffReader::visitIfdMakernote(TiffIfdMakernote* object) { object->setImageByteOrder(byteOrder()); // set the byte order for the image - if (!object->readHeader(object->start(), static_cast(pLast_ - object->start()), byteOrder())) { + if (!object->readHeader(object->start(), pLast_ - object->start(), byteOrder())) { #ifndef SUPPRESS_WARNINGS EXV_ERROR << "Failed to read " << groupName(object->ifd_.group()) << " IFD Makernote header.\n"; #ifdef EXIV2_DEBUG_MESSAGES - if (static_cast(pLast_ - object->start()) >= 16) { - hexdump(std::cerr, object->start(), 16); + if (pLast_ - object->start() >= 16u) { + hexdump(std::cerr, object->start(), 16u); } #endif // EXIV2_DEBUG_MESSAGES #endif // SUPPRESS_WARNINGS @@ -1291,10 +1291,10 @@ void TiffReader::readTiffEntry(TiffEntryBase* object) { if (count > std::numeric_limits::max() / typeSize) { throw Error(ErrorCode::kerArithmeticOverflow); } - auto size = static_cast(typeSize * count); + size_t size = typeSize * count; uint32_t offset = getLong(p, byteOrder()); byte* pData = p; - if (size > 4 && (baseOffset() + offset >= size_ || static_cast(baseOffset()) + offset <= 0)) { + if (size > 4 && (baseOffset() + offset >= size_ || baseOffset() + offset <= 0)) { // #1143 if (object->tag() == 0x2001 && std::string(groupName(object->group())) == "Sony1") { // This tag is Exif.Sony1.PreviewImage, which refers to a preview image which is @@ -1335,7 +1335,7 @@ void TiffReader::readTiffEntry(TiffEntryBase* object) { pData = const_cast(pData_) + baseOffset() + offset; // check for size being invalid - if (size > static_cast(pLast_ - pData)) { + if (size > static_cast(pLast_ - pData)) { #ifndef SUPPRESS_WARNINGS EXV_ERROR << "Upper boundary of data for " << "directory " << groupName(object->group()) << ", entry 0x" << std::setw(4) << std::setfill('0') diff --git a/src/value.cpp b/src/value.cpp index 5184c042..7a722ed0 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -155,8 +155,8 @@ DataValue* DataValue::clone_() const { } std::ostream& DataValue::write(std::ostream& os) const { - std::vector::size_type end = value_.size(); - for (std::vector::size_type i = 0; i != end; ++i) { + size_t end = value_.size(); + for (size_t i = 0; i != end; ++i) { os << static_cast(value_.at(i)); if (i < end - 1) os << " "; diff --git a/src/webpimage.cpp b/src/webpimage.cpp index 020e6975..0c4cb505 100644 --- a/src/webpimage.cpp +++ b/src/webpimage.cpp @@ -184,10 +184,8 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { const uint32_t size_u32 = Exiv2::getULong(size_buff, littleEndian); // Check that `size_u32` is safe to cast to `long`. - enforce(size_u32 <= static_cast(std::numeric_limits::max()), - Exiv2::ErrorCode::kerCorruptedMetadata); - const auto size = static_cast(size_u32); - DataBuf payload(size); + enforce(size_u32 <= std::numeric_limits::max(), Exiv2::ErrorCode::kerCorruptedMetadata); + DataBuf payload(size_u32); io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata); if (payload.size() % 2) { byte c = 0; @@ -200,7 +198,7 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { has_vp8x = true; } if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X) && !has_size) { - enforce(size >= 10, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 10, Exiv2::ErrorCode::kerCorruptedMetadata); has_size = true; byte size_buf[WEBP_TAG_SIZE]; @@ -229,7 +227,7 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { } #endif if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8) && !has_size) { - enforce(size >= 10, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 10, Exiv2::ErrorCode::kerCorruptedMetadata); has_size = true; byte size_buf[2]; @@ -247,13 +245,13 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { /* Chunk with lossless image data. */ if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8L) && !has_alpha) { - enforce(size >= 5, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 5, Exiv2::ErrorCode::kerCorruptedMetadata); if ((payload.read_uint8(4) & WEBP_VP8X_ALPHA_BIT) == WEBP_VP8X_ALPHA_BIT) { has_alpha = true; } } if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8L) && !has_size) { - enforce(size >= 5, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 5, Exiv2::ErrorCode::kerCorruptedMetadata); has_size = true; byte size_buf_w[2]; byte size_buf_h[3]; @@ -277,13 +275,13 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { /* Chunk with animation frame. */ if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANMF) && !has_alpha) { - enforce(size >= 6, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 6, Exiv2::ErrorCode::kerCorruptedMetadata); if ((payload.read_uint8(5) & 0x2) == 0x2) { has_alpha = true; } } if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANMF) && !has_size) { - enforce(size >= 12, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 12, Exiv2::ErrorCode::kerCorruptedMetadata); has_size = true; byte size_buf[WEBP_TAG_SIZE]; @@ -317,17 +315,15 @@ void WebPImage::doWriteMetadata(BasicIo& outIo) { const uint32_t size_u32 = Exiv2::getULong(size_buff, littleEndian); // Check that `size_u32` is safe to cast to `long`. - enforce(size_u32 <= static_cast(std::numeric_limits::max()), - Exiv2::ErrorCode::kerCorruptedMetadata); - const auto size = static_cast(size_u32); + enforce(size_u32 <= std::numeric_limits::max(), Exiv2::ErrorCode::kerCorruptedMetadata); - DataBuf payload(size); - io_->readOrThrow(payload.data(), size, Exiv2::ErrorCode::kerCorruptedMetadata); + DataBuf payload(size_u32); + io_->readOrThrow(payload.data(), size_u32, Exiv2::ErrorCode::kerCorruptedMetadata); if (io_->tell() % 2) io_->seek(+1, BasicIo::cur); // skip pad if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X)) { - enforce(size >= 1, Exiv2::ErrorCode::kerCorruptedMetadata); + enforce(size_u32 >= 1, Exiv2::ErrorCode::kerCorruptedMetadata); if (has_icc) { const uint8_t x = payload.read_uint8(0); payload.write_uint8(0, x | WEBP_VP8X_ICC_BIT); @@ -643,26 +639,22 @@ void WebPImage::decodeChunks(long filesize) { bool s_header = false; bool le_header = false; bool be_header = false; - long pos = getHeaderOffset(payload.c_data(), static_cast(payload.size()), - reinterpret_cast(&exifLongHeader), 4); + long pos = getHeaderOffset(payload.c_data(), payload.size(), reinterpret_cast(&exifLongHeader), 4); if (pos == -1) { - pos = getHeaderOffset(payload.c_data(), static_cast(payload.size()), - reinterpret_cast(&exifLongHeader), 6); + pos = getHeaderOffset(payload.c_data(), payload.size(), reinterpret_cast(&exifLongHeader), 6); if (pos != -1) { s_header = true; } } if (pos == -1) { - pos = getHeaderOffset(payload.c_data(), static_cast(payload.size()), - reinterpret_cast(&exifTiffLEHeader), 3); + pos = getHeaderOffset(payload.c_data(), payload.size(), reinterpret_cast(&exifTiffLEHeader), 3); if (pos != -1) { le_header = true; } } if (pos == -1) { - pos = getHeaderOffset(payload.c_data(), static_cast(payload.size()), - reinterpret_cast(&exifTiffBEHeader), 4); + pos = getHeaderOffset(payload.c_data(), payload.size(), reinterpret_cast(&exifTiffBEHeader), 4); if (pos != -1) { be_header = true; } @@ -835,14 +827,14 @@ void WebPImage::inject_VP8X(BasicIo& iIo, bool has_xmp, bool has_exif, bool has_ } } -long WebPImage::getHeaderOffset(const byte* data, long data_size, const byte* header, long header_size) { +long WebPImage::getHeaderOffset(const byte* data, size_t data_size, const byte* header, size_t header_size) { if (data_size < header_size) { return -1; } long pos = -1; - for (long i = 0; i < data_size - header_size; i++) { + for (size_t i = 0; i < data_size - header_size; i++) { if (memcmp(header, &data[i], header_size) == 0) { - pos = i; + pos = static_cast(i); break; } } diff --git a/unitTests/test_bmpimage.cpp b/unitTests/test_bmpimage.cpp index 88511f98..31307dfe 100644 --- a/unitTests/test_bmpimage.cpp +++ b/unitTests/test_bmpimage.cpp @@ -85,7 +85,7 @@ TEST(BmpImage, readMetadataReadsImageDimensionsWhenDataIsAvailable) { 0x20, 0x03, 0x00, 0x00, // The bitmap height in pixels (unsigned 16 bit) off:22, size:4 }; - auto memIo = std::make_unique(header.data(), static_cast(header.size())); + auto memIo = std::make_unique(header.data(), header.size()); BmpImage bmp(std::move(memIo)); ASSERT_NO_THROW(bmp.readMetadata()); ASSERT_EQ(1280, bmp.pixelWidth()); @@ -104,7 +104,7 @@ TEST(BmpImage, readMetadataThrowsWhenImageIsNotBMP) { 0x20, 0x03, 0x00, 0x00, // The bitmap height in pixels (unsigned 16 bit) off:22, size:4 }; - auto memIo = std::make_unique(header.data(), static_cast(header.size())); + auto memIo = std::make_unique(header.data(), header.size()); BmpImage bmp(std::move(memIo)); try { bmp.readMetadata(); @@ -117,7 +117,7 @@ TEST(BmpImage, readMetadataThrowsWhenImageIsNotBMP) { TEST(BmpImage, readMetadataThrowsWhenThereIsNotEnoughInfoToRead) { const std::array header{'B'}; - auto memIo = std::make_unique(header.data(), static_cast(header.size())); + auto memIo = std::make_unique(header.data(), header.size()); BmpImage bmp(std::move(memIo)); try { bmp.readMetadata(); @@ -147,7 +147,7 @@ TEST(newBmpInstance, createsValidInstace) { 0x00, 0x00, // Reserved 0x00, 0x00, 0x00, 0x00 // Offset of the byte where the bitmap image data can be found }; - auto memIo = std::make_unique(bitmapHeader.data(), static_cast(bitmapHeader.size())); + auto memIo = std::make_unique(bitmapHeader.data(), bitmapHeader.size()); auto img = newBmpInstance(std::move(memIo), false); ASSERT_TRUE(img->good()); } @@ -166,7 +166,7 @@ TEST(isBmpType, withValidSignatureReturnsTrue) { 0x00, 0x00, // Reserved 0x00, 0x00, 0x00, 0x00 // Offset of the byte where the bitmap image data can be found }; - MemIo memIo(bitmapHeader.data(), static_cast(bitmapHeader.size())); + MemIo memIo(bitmapHeader.data(), bitmapHeader.size()); ASSERT_TRUE(isBmpType(memIo, false)); } @@ -178,6 +178,6 @@ TEST(isBmpType, withInvalidSignatureReturnsFalse) { 0x00, 0x00, // Reserved 0x00, 0x00, 0x00, 0x00 // Offset of the byte where the bitmap image data can be found }; - MemIo memIo(bitmapHeader.data(), static_cast(bitmapHeader.size())); + MemIo memIo(bitmapHeader.data(), bitmapHeader.size()); ASSERT_FALSE(isBmpType(memIo, false)); } diff --git a/unitTests/test_pngimage.cpp b/unitTests/test_pngimage.cpp index 9dd0b04c..abeb464d 100644 --- a/unitTests/test_pngimage.cpp +++ b/unitTests/test_pngimage.cpp @@ -18,7 +18,7 @@ TEST(PngChunk, keyTxtChunkExtractsKeywordCorrectlyInPresenceOfNullChar) { 0x20, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x20, 0x65, 0x78, 0x69, 0x66, 0x00, 0x00, 0x78}; - DataBuf chunkBuf(data.data(), static_cast(data.size())); + DataBuf chunkBuf(data.data(), data.size()); DataBuf key = Internal::PngChunk::keyTXTChunk(chunkBuf, true); ASSERT_EQ(21, key.size()); @@ -31,13 +31,13 @@ TEST(PngChunk, keyTxtChunkThrowsExceptionWhenThereIsNoNullChar) { 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x20, 0x65, 0x78, 0x69, 0x66, 0x78}; - DataBuf chunkBuf(data.data(), static_cast(data.size())); + DataBuf chunkBuf(data.data(), data.size()); ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error); } TEST(PngChunk, keyTxtChunkThrowsIfSizeIsNotEnough) { const std::array data{0x00, 0x00, 0x22, 0x41}; - DataBuf chunkBuf(data.data(), static_cast(data.size())); + DataBuf chunkBuf(data.data(), data.size()); ASSERT_THROW(Internal::PngChunk::keyTXTChunk(chunkBuf, true), Exiv2::Error); DataBuf emptyChunk(data.data(), 0);