From 66a1d3bc639066f18e1fcb89489cf8b9680fd999 Mon Sep 17 00:00:00 2001 From: Oz Date: Tue, 28 Nov 2023 21:03:21 +0100 Subject: [PATCH] Add check for invalid indices in BitInputArchive's extractTo filesystem --- include/bit7z/bitextractor.hpp | 12 ------------ include/bit7z/bitinputarchive.hpp | 9 ++++++++- src/bitinputarchive.cpp | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/include/bit7z/bitextractor.hpp b/include/bit7z/bitextractor.hpp index 80007392..229ceaf8 100644 --- a/include/bit7z/bitextractor.hpp +++ b/include/bit7z/bitextractor.hpp @@ -169,18 +169,6 @@ class BitExtractor final : public BitAbstractArchiveOpener { } BitInputArchive inputArchive( *this, inArchive ); - uint32_t nItems = inputArchive.itemsCount(); - // Find if any index passed by the user is not in the valid range [0, itemsCount() - 1] - const auto findRes = std::find_if( indices.cbegin(), - indices.cend(), - [ &nItems ]( uint32_t index ) -> bool { - return index >= nItems; - } ); - if ( findRes != indices.cend() ) { - throw BitException( "Cannot extract item at the index " + std::to_string( *findRes ), - make_error_code( BitError::InvalidIndex ) ); - } - inputArchive.extractTo( outDir, indices ); } diff --git a/include/bit7z/bitinputarchive.hpp b/include/bit7z/bitinputarchive.hpp index c604b5bc..ca5db49f 100644 --- a/include/bit7z/bitinputarchive.hpp +++ b/include/bit7z/bitinputarchive.hpp @@ -144,13 +144,20 @@ class BitInputArchive { extractTo( outDir, indices ); } + /** + * @brief Extracts the archive to the chosen directory. + * + * @param outDir the output directory where the extracted files will be put. + */ + void extractTo( const tstring& outDir ) const; + /** * @brief Extracts the specified items to the chosen directory. * * @param outDir the output directory where the extracted files will be put. * @param indices the indices of the files in the archive that must be extracted. */ - void extractTo( const tstring& outDir, const std::vector< uint32_t >& indices = {} ) const; + void extractTo( const tstring& outDir, const std::vector< uint32_t >& indices ) const; BIT7Z_DEPRECATED_MSG("Since v4.0; please, use the extractTo method.") inline void extract( buffer_t& outBuffer, uint32_t index = 0 ) const { diff --git a/src/bitinputarchive.cpp b/src/bitinputarchive.cpp index 8d79b654..9c4869bd 100644 --- a/src/bitinputarchive.cpp +++ b/src/bitinputarchive.cpp @@ -233,7 +233,26 @@ auto BitInputArchive::handler() const noexcept -> const BitAbstractArchiveHandle return mArchiveHandler; } +void BitInputArchive::extractTo( const tstring& outDir ) const { + auto callback = bit7z::make_com< FileExtractCallback, ExtractCallback >( *this, outDir ); + extract_arc( mInArchive, {}, callback ); +} + +inline auto findInvalidIndex( const std::vector< uint32_t >& indices, + uint32_t itemsCount ) -> std::vector< uint32_t >::const_iterator { + return std::find_if( indices.cbegin(), indices.cend(), [&]( uint32_t index ) -> bool { + return index >= itemsCount; + }); +} + void BitInputArchive::extractTo( const tstring& outDir, const std::vector< uint32_t >& indices ) const { + // Find if any index passed by the user is not in the valid range [0, itemsCount() - 1] + const auto invalidIndex = findInvalidIndex( indices, itemsCount() ); + if ( invalidIndex != indices.cend() ) { + throw BitException( "Cannot extract item at the index " + std::to_string( *invalidIndex ), + make_error_code( BitError::InvalidIndex ) ); + } + auto callback = bit7z::make_com< FileExtractCallback, ExtractCallback >( *this, outDir ); extract_arc( mInArchive, indices, callback ); }