Cap infoHashLength in .aria2 file

Cap infoHashLength in .aria2 file, and save an extra allocation.
pull/2127/head
Tatsuhiro Tsujikawa 2023-11-02 21:22:37 +09:00
parent d607327ea6
commit 3330110caf
1 changed files with 7 additions and 6 deletions

View File

@ -36,6 +36,7 @@
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <array>
#include "PieceStorage.h" #include "PieceStorage.h"
#include "Piece.h" #include "Piece.h"
@ -263,21 +264,21 @@ void DefaultBtProgressInfoFile::load()
if (version >= 1) { if (version >= 1) {
infoHashLength = ntohl(infoHashLength); infoHashLength = ntohl(infoHashLength);
} }
if (infoHashLength == 0 && infoHashCheckEnabled) { if (infoHashLength > INFO_HASH_LENGTH ||
(infoHashLength != INFO_HASH_LENGTH && infoHashCheckEnabled)) {
throw DL_ABORT_EX(fmt("Invalid info hash length: %d", infoHashLength)); throw DL_ABORT_EX(fmt("Invalid info hash length: %d", infoHashLength));
} }
if (infoHashLength > 0) { if (infoHashLength > 0) {
auto savedInfoHash = make_unique<unsigned char[]>((size_t)infoHashLength); std::array<unsigned char, INFO_HASH_LENGTH> savedInfoHash;
READ_CHECK(fp, savedInfoHash.get(), infoHashLength); READ_CHECK(fp, savedInfoHash.data(), infoHashLength);
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
if (infoHashCheckEnabled) { if (infoHashCheckEnabled) {
const unsigned char* infoHash = bittorrent::getInfoHash(dctx_); const unsigned char* infoHash = bittorrent::getInfoHash(dctx_);
if (infoHashLength != INFO_HASH_LENGTH || if (memcmp(savedInfoHash.data(), infoHash, INFO_HASH_LENGTH) != 0) {
memcmp(savedInfoHash.get(), infoHash, INFO_HASH_LENGTH) != 0) {
throw DL_ABORT_EX( throw DL_ABORT_EX(
fmt("info hash mismatch. expected: %s, actual: %s", fmt("info hash mismatch. expected: %s, actual: %s",
util::toHex(infoHash, INFO_HASH_LENGTH).c_str(), util::toHex(infoHash, INFO_HASH_LENGTH).c_str(),
util::toHex(savedInfoHash.get(), infoHashLength).c_str())); util::toHex(savedInfoHash.data(), infoHashLength).c_str()));
} }
} }
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT