aboutsummaryrefslogtreecommitdiff
path: root/engines
diff options
context:
space:
mode:
authorFilippos Karapetis2010-08-23 12:56:41 +0000
committerFilippos Karapetis2010-08-23 12:56:41 +0000
commit23d4177d207c4797106a7a0ddb2a9dd5c8d5c539 (patch)
treee159e56053a62b8e5d860af52a03d33a46616ac6 /engines
parenteeff13d7474e9fa288528a61a5068852ae337598 (diff)
downloadscummvm-rg350-23d4177d207c4797106a7a0ddb2a9dd5c8d5c539.tar.gz
scummvm-rg350-23d4177d207c4797106a7a0ddb2a9dd5c8d5c539.tar.bz2
scummvm-rg350-23d4177d207c4797106a7a0ddb2a9dd5c8d5c539.zip
SCI: Check for EOF and errors when reading from a file. Fixes bug #3049360 - "SCI: Bug in file read/write operations"
svn-id: r52298
Diffstat (limited to 'engines')
-rw-r--r--engines/sci/engine/kfile.cpp29
1 files changed, 24 insertions, 5 deletions
diff --git a/engines/sci/engine/kfile.cpp b/engines/sci/engine/kfile.cpp
index 5a38e16dff..76ac6254ac 100644
--- a/engines/sci/engine/kfile.cpp
+++ b/engines/sci/engine/kfile.cpp
@@ -218,18 +218,22 @@ reg_t kFPuts(EngineState *s, int argc, reg_t *argv) {
return s->r_acc;
}
-static void fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) {
+static bool fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) {
FileHandle *f = getFileFromHandle(s, handle);
if (!f)
- return;
+ return false;
if (!f->_in) {
error("fgets_wrapper: Trying to read from file '%s' opened for writing", f->_name.c_str());
- return;
+ return false;
}
if (maxsize > 1) {
memset(dest, 0, maxsize);
f->_in->readLine(dest, maxsize);
+
+ if (f->_in->eos() || f->_in->err())
+ return false;
+
// The returned string must not have an ending LF
int strSize = strlen(dest);
if (strSize > 0) {
@@ -238,9 +242,14 @@ static void fgets_wrapper(EngineState *s, char *dest, int maxsize, int handle) {
}
} else {
*dest = f->_in->readByte();
+
+ if (f->_in->eos() || f->_in->err())
+ return false;
}
debugC(2, kDebugLevelFile, " -> FGets'ed \"%s\"", dest);
+
+ return true;
}
reg_t kFGets(EngineState *s, int argc, reg_t *argv) {
@@ -249,7 +258,9 @@ reg_t kFGets(EngineState *s, int argc, reg_t *argv) {
int handle = argv[2].toUint16();
debugC(2, kDebugLevelFile, "kFGets(%d, %d)", handle, maxsize);
- fgets_wrapper(s, buf, maxsize, handle);
+ if (!fgets_wrapper(s, buf, maxsize, handle))
+ *buf = 0;
+
s->_segMan->memcpy(argv[0], (const byte*)buf, maxsize);
return argv[0];
}
@@ -773,6 +784,12 @@ reg_t kFileIOReadRaw(EngineState *s, int argc, reg_t *argv) {
FileHandle *f = getFileFromHandle(s, handle);
if (f) {
bytesRead = f->_in->read(buf, size);
+
+ if (f->_in->eos() || f->_in->err()) {
+ *buf = 0;
+ bytesRead = 0;
+ }
+
s->_segMan->memcpy(argv[1], (const byte*)buf, size);
}
@@ -841,7 +858,9 @@ reg_t kFileIOReadString(EngineState *s, int argc, reg_t *argv) {
int handle = argv[2].toUint16();
debugC(2, kDebugLevelFile, "kFileIO(readString): %d, %d", handle, size);
- fgets_wrapper(s, buf, size, handle);
+ if (!fgets_wrapper(s, buf, size, handle))
+ *buf = 0;
+
s->_segMan->memcpy(argv[0], (const byte*)buf, size);
delete[] buf;
return argv[0];