From 3978d0754d23c05f9847596c31ea0843ec0c7863 Mon Sep 17 00:00:00 2001 From: joevt Date: Fri, 14 Jul 2023 16:54:26 -0700 Subject: [PATCH] CD-ROM: Add max blocks check. The code does not support more than 2^32 - 2 blocks because of this expression: static_cast(this->size_blocks + 1) --- devices/storage/blockstoragedevice.cpp | 6 +++++- devices/storage/blockstoragedevice.h | 3 ++- devices/storage/cdromdrive.cpp | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/devices/storage/blockstoragedevice.cpp b/devices/storage/blockstoragedevice.cpp index e797aa1..d517367 100644 --- a/devices/storage/blockstoragedevice.cpp +++ b/devices/storage/blockstoragedevice.cpp @@ -25,9 +25,10 @@ along with this program. If not, see . using namespace std; -BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size) { +BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size, const uint64_t max_blocks) { this->block_size = block_size; this->cache_size = cache_blocks * this->block_size; + this->max_blocks = max_blocks; // allocate device cache and fill it with zeroes this->data_cache = std::unique_ptr(new char[this->cache_size] ()); @@ -46,6 +47,9 @@ int BlockStorageDevice::set_host_file(std::string file_path) { this->size_bytes = this->img_file.size(); this->size_blocks = this->size_bytes / this->block_size; + if (this->size_blocks > this->max_blocks) + return -1; + this->set_fpos(0); this->is_ready = true; diff --git a/devices/storage/blockstoragedevice.h b/devices/storage/blockstoragedevice.h index 3a93270..14b8456 100644 --- a/devices/storage/blockstoragedevice.h +++ b/devices/storage/blockstoragedevice.h @@ -32,7 +32,7 @@ along with this program. If not, see . class BlockStorageDevice { public: - BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512); + BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512, const uint64_t max_blocks=0xffffffffffffffff); ~BlockStorageDevice(); void set_block_size(const int blk_size) { this->block_size = blk_size; }; @@ -49,6 +49,7 @@ protected: ImgFile img_file; uint64_t size_bytes = 0; // image file size in bytes uint64_t size_blocks = 0; // image file size in blocks + uint64_t max_blocks = 0; // maximum number of blocks supported uint64_t cur_fpos = 0; // current image file pointer position uint32_t block_size = 512; // physical block size uint32_t cache_size = 0; // cache size diff --git a/devices/storage/cdromdrive.cpp b/devices/storage/cdromdrive.cpp index de8efee..441e4ff 100644 --- a/devices/storage/cdromdrive.cpp +++ b/devices/storage/cdromdrive.cpp @@ -30,7 +30,7 @@ along with this program. If not, see . #include #include -CdromDrive::CdromDrive() : BlockStorageDevice(31, 2048) { +CdromDrive::CdromDrive() : BlockStorageDevice(31, 2048, 0xfffffffe) { this->is_writeable = false; } @@ -133,7 +133,7 @@ uint32_t CdromDrive::request_sense(uint8_t *data_ptr, uint8_t sense_key, } uint32_t CdromDrive::report_capacity(uint8_t *data_ptr) { - WRITE_DWORD_BE_A(data_ptr, this->size_blocks); + WRITE_DWORD_BE_A(data_ptr, static_cast(this->size_blocks)); WRITE_DWORD_BE_A(&data_ptr[4], this->block_size); return 8; }