[Scummvm-cvs-logs] scummvm master -> 51dceaf1659ecc8ef3371f3a2fa26ca137403447

digitall dgturner at iee.org
Thu Nov 14 16:23:56 CET 2013


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
51dceaf165 GUI: Use Common::String for console history, to ensure buffer safety.


Commit: 51dceaf1659ecc8ef3371f3a2fa26ca137403447
    https://github.com/scummvm/scummvm/commit/51dceaf1659ecc8ef3371f3a2fa26ca137403447
Author: Joel Teichroeb (joel at teichroeb.net)
Date: 2013-11-14T07:23:01-08:00

Commit Message:
GUI: Use Common::String for console history, to ensure buffer safety.

Previously, if the user enters a command that is more than 256
characters, it will overflow the history buffer.

By using a Common::String, this is not possible.

Changed paths:
    gui/console.cpp
    gui/console.h



diff --git a/gui/console.cpp b/gui/console.cpp
index 49e2fcc..7e88b6d 100644
--- a/gui/console.cpp
+++ b/gui/console.cpp
@@ -82,8 +82,6 @@ ConsoleDialog::ConsoleDialog(float widthPercent, float heightPercent)
 	_historyIndex = 0;
 	_historyLine = 0;
 	_historySize = 0;
-	for (int i = 0; i < kHistorySize; i++)
-		_history[i][0] = '\0';
 
 	// Display greetings & prompt
 	print(gScummVMFullVersion);
@@ -274,24 +272,19 @@ void ConsoleDialog::handleKeyDown(Common::KeyState state) {
 
 		if (len > 0) {
 
-			// We have to allocate the string buffer with new, since VC++ sadly does not
-			// comply to the C++ standard, so we can't use a dynamic sized stack array.
-			char *str = new char[len + 1];
+			Common::String str;
 
 			// Copy the user input to str
 			for (i = 0; i < len; i++)
-				str[i] = buffer(_promptStartPos + i);
-			str[len] = '\0';
+				str.insertChar(buffer(_promptStartPos + i), i);
 
 			// Add the input to the history
 			addToHistory(str);
 
 			// Pass it to the input callback, if any
 			if (_callbackProc)
-				keepRunning = (*_callbackProc)(this, str, _callbackRefCon);
+				keepRunning = (*_callbackProc)(this, str.c_str(), _callbackRefCon);
 
-				// Get rid of the string buffer
-			delete[] str;
 		}
 
 		print(PROMPT);
@@ -575,8 +568,8 @@ void ConsoleDialog::killLastWord() {
 	}
 }
 
-void ConsoleDialog::addToHistory(const char *str) {
-	strcpy(_history[_historyIndex], str);
+void ConsoleDialog::addToHistory(const Common::String &str) {
+	_history[_historyIndex] = str;
 	_historyIndex = (_historyIndex + 1) % kHistorySize;
 	_historyLine = 0;
 	if (_historySize < kHistorySize)
@@ -590,8 +583,7 @@ void ConsoleDialog::historyScroll(int direction) {
 	if (_historyLine == 0 && direction > 0) {
 		int i;
 		for (i = 0; i < _promptEndPos - _promptStartPos; i++)
-			_history[_historyIndex][i] = buffer(_promptStartPos + i);
-		_history[_historyIndex][i] = '\0';
+			_history[_historyIndex].insertChar(buffer(_promptStartPos + i), i);
 	}
 
 	// Advance to the next line in the history
@@ -617,7 +609,8 @@ void ConsoleDialog::historyScroll(int direction) {
 		idx = (_historyIndex - _historyLine + _historySize) % _historySize;
 	else
 		idx = _historyIndex;
-	for (int i = 0; i < kLineBufferSize && _history[idx][i] != '\0'; i++)
+	int length = _history[idx].size();
+	for (int i = 0; i < length; i++)
 		printCharIntern(_history[idx][i]);
 	_promptEndPos = _currentPos;
 
diff --git a/gui/console.h b/gui/console.h
index 50a00a1..194bfd6 100644
--- a/gui/console.h
+++ b/gui/console.h
@@ -23,6 +23,7 @@
 #define CONSOLE_DIALOG_H
 
 #include "gui/dialog.h"
+#include "common/str.h"
 
 namespace GUI {
 
@@ -69,7 +70,6 @@ protected:
 	enum {
 		kBufferSize	= 32768,
 		kCharsPerLine = 128,
-		kLineBufferSize = 256,
 
 		kHistorySize = 20
 	};
@@ -112,7 +112,7 @@ protected:
 	CompletionCallbackProc _completionCallbackProc;
 	void *_completionCallbackRefCon;
 
-	char _history[kHistorySize][kLineBufferSize];
+	Common::String _history[kHistorySize];
 	int _historySize;
 	int _historyIndex;
 	int _historyLine;
@@ -184,7 +184,7 @@ protected:
 	void killLastWord();
 
 	// History
-	void addToHistory(const char *str);
+	void addToHistory(const Common::String &str);
 	void historyScroll(int direction);
 };
 






More information about the Scummvm-git-logs mailing list