Skip to content

Commit

Permalink
Add check for invalid indices in BitInputArchive's extractTo filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Nov 28, 2023
1 parent 6e4dd63 commit 66a1d3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
12 changes: 0 additions & 12 deletions include/bit7z/bitextractor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
9 changes: 8 additions & 1 deletion include/bit7z/bitinputarchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions src/bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 66a1d3b

Please sign in to comment.