From 478ee06fb818a5d22a8d20b38cca5052e2a5b947 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 15 Nov 2008 03:16:41 +0000 Subject: Paranoia: Add destination buffer size to Engine::errorString to help avoiding buffer overflows. svn-id: r35072 --- common/util.cpp | 4 ++-- engines/engine.cpp | 4 ++-- engines/engine.h | 2 +- engines/scumm/scumm.cpp | 4 ++-- engines/scumm/scumm.h | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/util.cpp b/common/util.cpp index 13a810e20f..0b11cf0074 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -465,7 +465,7 @@ static void debugHelper(const char *in_buf, bool caret = true) { // Next, give the active engine (if any) a chance to augment the message if (g_engine) { - g_engine->errorString(in_buf, buf); + g_engine->errorString(in_buf, buf, STRINGBUFLEN); } else { strcpy(buf, in_buf); } @@ -583,7 +583,7 @@ void NORETURN error(const char *s, ...) { // Next, give the active engine (if any) a chance to augment the message if (g_engine) { - g_engine->errorString(buf_input, buf_output); + g_engine->errorString(buf_input, buf_output, STRINGBUFLEN); } else { strcpy(buf_output, buf_input); } diff --git a/engines/engine.cpp b/engines/engine.cpp index dcdcbd1d32..2c9835acc5 100644 --- a/engines/engine.cpp +++ b/engines/engine.cpp @@ -251,8 +251,8 @@ bool Engine::shouldPerformAutoSave(int lastSaveTime) { return autosavePeriod != 0 && diff > autosavePeriod * 1000; } -void Engine::errorString(const char *buf1, char *buf2) { - strcpy(buf2, buf1); +void Engine::errorString(const char *buf1, char *buf2, int size) { + strncpy(buf2, buf1, size); } void Engine::pauseEngine(bool pause) { diff --git a/engines/engine.h b/engines/engine.h index 4e16f01335..de8523a1b0 100644 --- a/engines/engine.h +++ b/engines/engine.h @@ -154,7 +154,7 @@ public: /** * Prepare an error string, which is printed by the error() function. */ - virtual void errorString(const char *buf_input, char *buf_output); + virtual void errorString(const char *buf_input, char *buf_output, int buf_output_size); /** * Return the engine's debugger instance, if any. Used by error() to diff --git a/engines/scumm/scumm.cpp b/engines/scumm/scumm.cpp index 0869a3dff6..93605fa29b 100644 --- a/engines/scumm/scumm.cpp +++ b/engines/scumm/scumm.cpp @@ -2338,9 +2338,9 @@ GUI::Debugger *ScummEngine::getDebugger() { return _debugger; } -void ScummEngine::errorString(const char *buf1, char *buf2) { +void ScummEngine::errorString(const char *buf1, char *buf2, int buf2Size) { if (_currentScript != 0xFF) { - sprintf(buf2, "(%d:%d:0x%lX): %s", _roomResource, + snprintf(buf2, buf2Size, "(%d:%d:0x%lX): %s", _roomResource, vm.slot[_currentScript].number, (long)(_scriptPointer - _scriptOrgPointer), buf1); } else { strcpy(buf2, buf1); diff --git a/engines/scumm/scumm.h b/engines/scumm/scumm.h index 1c550f824c..c4697ecbe4 100644 --- a/engines/scumm/scumm.h +++ b/engines/scumm/scumm.h @@ -445,7 +445,7 @@ public: // Engine APIs virtual Common::Error init(); virtual Common::Error go(); - virtual void errorString(const char *buf_input, char *buf_output); + virtual void errorString(const char *buf_input, char *buf_output, int buf_output_size); virtual GUI::Debugger *getDebugger(); virtual bool hasFeature(EngineFeature f) const; virtual void syncSoundSettings(); -- cgit v1.2.3