Repository: bitcoin/bitcoin
File: src/node/blockstorage.cpp (C++)

Code before:
        filein >> blk_start >> blk_size;

        if (blk_start != GetParams().MessageStart()) {
            LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block",
                pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
            return false;
        }

        if (blk_size > MAX_SIZE) {
            LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
                pos.ToString(), blk_size, MAX_SIZE);
            return false;
        }

        size_t offset = 0;
        if (part_offset.has_value()) {
            if (*part_offset > blk_size) {
                LogError("Read from block file failed: invalid offset: %u", *part_offset);
                return false;
            }
            offset = *part_offset;
        }

        size_t size = blk_size - offset;
        if (part_size.has_value()) {
            if (*part_size > size || *part_size == 0) {
                LogError("Read from block file failed: invalid size: %u", *part_size);
                return false;
            }
            size = *part_size;
        }


        data.resize(size); // Zeroing of memory is intentional here
        filein.seek(offset, SEEK_CUR);
        filein.read(data);
    } catch (const std::exception& e) {
        LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
        return false;
    }

    return true;
}

FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
{
    const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
    FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
    if (pos.IsNull()) {
        LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
        return FlatFilePos();

Review comment (suggestion):
```suggestion
            if (*part_size > size || *part_size == 0) [[unlikely]] {
```


Code after:

        if (blk_start != GetParams().MessageStart()) {
            LogError("Block magic mismatch for %s: %s versus expected %s while reading raw block",
                pos.ToString(), HexStr(blk_start), HexStr(GetParams().MessageStart()));
            return util::Unexpected{ReadRawError::IO};
        }

        if (blk_size > MAX_SIZE) {
            LogError("Block data is larger than maximum deserialization size for %s: %s versus %s while reading raw block",
                pos.ToString(), blk_size, MAX_SIZE);
            return util::Unexpected{ReadRawError::IO};
        }

        if (block_part) {
            const auto [offset, size]{*block_part};
            if (size == 0 || offset >= blk_size || size > blk_size - offset) {
                return util::Unexpected{ReadRawError::BadPartRange}; // Avoid logging - offset/size come from untrusted REST input
            }
            filein.seek(offset, SEEK_CUR);
            blk_size = size;
        }

        std::vector<std::byte> data(blk_size); // Zeroing of memory is intentional here
        filein.read(data);
        return data;
    } catch (const std::exception& e) {
        LogError("Read from block file failed: %s for %s while reading raw block", e.what(), pos.ToString());
        return util::Unexpected{ReadRawError::IO};
    }
}

FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
{
    const unsigned int block_size{static_cast<unsigned int>(GetSerializeSize(TX_WITH_WITNESS(block)))};
    FlatFilePos pos{FindNextBlockPos(block_size + STORAGE_HEADER_BYTES, nHeight, block.GetBlockTime())};
    if (pos.IsNull()) {
        LogError("FindNextBlockPos failed for %s while writing block", pos.ToString());
        return FlatFilePos();
    }
    AutoFile file{OpenBlockFile(pos, /*fReadOnly=*/false)};
    if (file.IsNull()) {
        LogError("OpenBlockFile failed for %s while writing block", pos.ToString());
        m_opts.notifications.fatalError(_("Failed to write block."));
        return FlatFilePos();
    }
    {
        BufferedWriter fileout{file};

        // Write index header
        fileout << GetParams().MessageStart() << block_size;
        pos.nPos += STORAGE_HEADER_BYTES;