From f1cbf3f9b636edf0360bb4287d987d85f7aa2cab Mon Sep 17 00:00:00 2001 From: Sven Hesse Date: Sat, 11 Aug 2007 20:59:08 +0000 Subject: Added a small safety check to avoid accidently overflowing buffers when reading from files in STKs svn-id: r28536 --- engines/gob/dataio.cpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'engines/gob/dataio.cpp') diff --git a/engines/gob/dataio.cpp b/engines/gob/dataio.cpp index 361627caf4..7fd461b93c 100644 --- a/engines/gob/dataio.cpp +++ b/engines/gob/dataio.cpp @@ -273,28 +273,27 @@ int32 DataIO::readChunk(int16 handle, byte *buf, uint16 size) { file = (handle - 50) / 10; slot = (handle - 50) % 10; - if (!_isCurrentSlot[file * MAX_SLOT_COUNT + slot]) { + int index = file * MAX_SLOT_COUNT + slot; + + _chunkPos[index] = CLIP(_chunkPos[index], 0, _chunkSize[index]); + + if (!_isCurrentSlot[index]) { for (i = 0; i < MAX_SLOT_COUNT; i++) _isCurrentSlot[file * MAX_SLOT_COUNT + i] = false; - offset = _chunkOffset[file * MAX_SLOT_COUNT + slot] + - _chunkPos[file * MAX_SLOT_COUNT + slot]; + offset = _chunkOffset[index] + _chunkPos[index]; - debugC(7, kDebugFileIO, "seek: %d, %d", - _chunkOffset[file * MAX_SLOT_COUNT + slot], - _chunkPos[file * MAX_SLOT_COUNT + slot]); + debugC(7, kDebugFileIO, "seek: %d, %d", _chunkOffset[index], _chunkPos[index]); file_getHandle(_dataFileHandles[file])->seek(offset, SEEK_SET); } - _isCurrentSlot[file * MAX_SLOT_COUNT + slot] = true; - if ((_chunkPos[file * MAX_SLOT_COUNT + slot] + size) > - (_chunkSize[file * MAX_SLOT_COUNT + slot])) - size = _chunkSize[file * MAX_SLOT_COUNT + slot] - - _chunkPos[file * MAX_SLOT_COUNT + slot]; + _isCurrentSlot[index] = true; + if ((_chunkPos[index] + size) > (_chunkSize[index])) + size = _chunkSize[index] - _chunkPos[index]; file_getHandle(_dataFileHandles[file])->read(buf, size); - _chunkPos[file * MAX_SLOT_COUNT + slot] += size; + _chunkPos[index] += size; return size; } @@ -307,13 +306,15 @@ int16 DataIO::seekChunk(int16 handle, int32 pos, int16 from) { file = (handle - 50) / 10; slot = (handle - 50) % 10; - _isCurrentSlot[file * MAX_SLOT_COUNT + slot] = false; + int index = file * MAX_SLOT_COUNT + slot; + + _isCurrentSlot[index] = false; if (from == SEEK_SET) - _chunkPos[file * MAX_SLOT_COUNT + slot] = pos; + _chunkPos[index] = pos; else - _chunkPos[file * MAX_SLOT_COUNT + slot] += pos; + _chunkPos[index] += pos; - return _chunkPos[file * MAX_SLOT_COUNT + slot]; + return _chunkPos[index]; } uint32 DataIO::getChunkPos(int16 handle) const { -- cgit v1.2.3