aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Hesse2007-08-11 20:59:08 +0000
committerSven Hesse2007-08-11 20:59:08 +0000
commitf1cbf3f9b636edf0360bb4287d987d85f7aa2cab (patch)
tree9eba1f8035c3e11bd77374692bb4bc7706a662eb
parent771741647ddc6a577b00b553165c721ca64e77c5 (diff)
downloadscummvm-rg350-f1cbf3f9b636edf0360bb4287d987d85f7aa2cab.tar.gz
scummvm-rg350-f1cbf3f9b636edf0360bb4287d987d85f7aa2cab.tar.bz2
scummvm-rg350-f1cbf3f9b636edf0360bb4287d987d85f7aa2cab.zip
Added a small safety check to avoid accidently overflowing buffers when reading from files in STKs
svn-id: r28536
-rw-r--r--engines/gob/dataio.cpp33
1 files changed, 17 insertions, 16 deletions
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<int32>(_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 {