1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 00:25:07 +00:00

Kernel/FileSystem/FATFS: Restrict reads to the size of the file

Resolves issue where empty portions of a sector (or empty sectors in
a cluster) would be included with the returned data in a read().
This commit is contained in:
Taj Morton 2023-12-08 23:12:32 -08:00 committed by Andrew Kaster
parent 2995ee5858
commit d6a519e9af

View file

@ -326,9 +326,17 @@ ErrorOr<size_t> FATInode::read_bytes_locked(off_t offset, size_t size, UserOrKer
// FIXME: Read only the needed blocks instead of the whole file
auto blocks = TRY(const_cast<FATInode&>(*this).read_block_list());
TRY(buffer.write(blocks->data() + offset, min(size, m_block_list.size() * fs().m_device_block_size - offset)));
return min(size, m_block_list.size() * fs().m_device_block_size - offset);
// Take the minimum of the:
// 1. User-specified size parameter
// 2. The file size.
// 3. The number of blocks returned for reading.
size_t read_size = min(
min(size, m_metadata.size - offset),
(m_block_list.size() * fs().m_device_block_size) - offset);
TRY(buffer.write(blocks->data() + offset, read_size));
return read_size;
}
InodeMetadata FATInode::metadata() const