summaryrefslogtreecommitdiff
path: root/src/misc_gui.cpp
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-10-22 19:12:10 +0000
committersmatz <smatz@openttd.org>2008-10-22 19:12:10 +0000
commitbc1d994f6c3a1f81b2d5c71a4b656bab4e776d81 (patch)
tree43f6f4d436dced1e168f6a66c84f0027a6db24bd /src/misc_gui.cpp
parent852940b45b6ce895998e8455564d5a47bceddb5b (diff)
downloadopenttd-bc1d994f6c3a1f81b2d5c71a4b656bab4e776d81.tar.xz
(svn r14514) -Codechange: use 'size' instead of 'length' for querystring and textbuf, explicitly say it includes the terminating zero
-Fix: one couldn't rename things with too long default/automatic name -Fix: buffer overflow in console when too long (1024 bytes) command was entered
Diffstat (limited to 'src/misc_gui.cpp')
-rw-r--r--src/misc_gui.cpp68
1 files changed, 36 insertions, 32 deletions
diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp
index e9d374990..76061461f 100644
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -793,8 +793,8 @@ static void DelChar(Textbuf *tb, bool backspace)
}
/* Move the remaining characters over the marker */
- memmove(s, s + len, tb->length - (s - tb->buf) - len + 1);
- tb->length -= len;
+ memmove(s, s + len, tb->size - (s - tb->buf) - len);
+ tb->size -= len;
}
/**
@@ -809,7 +809,7 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode)
if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
DelChar(tb, true);
return true;
- } else if (delmode == WKC_DELETE && tb->caretpos < tb->length) {
+ } else if (delmode == WKC_DELETE && tb->caretpos < tb->size - 1) {
DelChar(tb, false);
return true;
}
@@ -823,9 +823,9 @@ bool DeleteTextBufferChar(Textbuf *tb, int delmode)
*/
void DeleteTextBufferAll(Textbuf *tb)
{
- memset(tb->buf, 0, tb->maxlength);
- tb->length = tb->width = 0;
- tb->caretpos = tb->caretxoffs = 0;
+ memset(tb->buf, 0, tb->maxsize);
+ tb->size = 1;
+ tb->width = tb->caretpos = tb->caretxoffs = 0;
}
/**
@@ -840,11 +840,11 @@ bool InsertTextBufferChar(Textbuf *tb, WChar key)
{
const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
uint16 len = (uint16)Utf8CharLen(key);
- if (tb->length < (tb->maxlength - len) && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
- memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->length - tb->caretpos + 1);
+ if (tb->size + len <= tb->maxsize && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
+ memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->size - tb->caretpos);
Utf8Encode(tb->buf + tb->caretpos, key);
- tb->length += len;
- tb->width += charwidth;
+ tb->size += len;
+ tb->width += charwidth;
tb->caretpos += len;
tb->caretxoffs += charwidth;
@@ -876,7 +876,7 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
break;
case WKC_RIGHT:
- if (tb->caretpos < tb->length) {
+ if (tb->caretpos < tb->size - 1) {
WChar c;
tb->caretpos += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos);
@@ -892,7 +892,7 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
return true;
case WKC_END:
- tb->caretpos = tb->length;
+ tb->caretpos = tb->size - 1;
tb->caretxoffs = tb->width;
return true;
@@ -908,16 +908,18 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
* and the maximum length of this buffer
* @param tb Textbuf type which is getting initialized
* @param buf the buffer that will be holding the data for input
- * @param maxlength maximum length in characters of this buffer
+ * @param maxsize maximum size in bytes, including terminating '\0'
* @param maxwidth maximum length in pixels of this buffer. If reached, buffer
- * cannot grow, even if maxlength would allow because there is space. A length
- * of zero '0' means the buffer is only restricted by maxlength */
-void InitializeTextBuffer(Textbuf *tb, const char *buf, uint16 maxlength, uint16 maxwidth)
+ * cannot grow, even if maxsize would allow because there is space. Width
+ * of zero '0' means the buffer is only restricted by maxsize */
+void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 maxsize, uint16 maxwidth)
{
- tb->buf = (char*)buf;
- tb->maxlength = maxlength;
- tb->maxwidth = maxwidth;
- tb->caret = true;
+ assert(maxsize != 0);
+
+ tb->buf = buf;
+ tb->maxsize = maxsize;
+ tb->maxwidth = maxwidth;
+ tb->caret = true;
UpdateTextBufferSize(tb);
}
@@ -930,17 +932,19 @@ void InitializeTextBuffer(Textbuf *tb, const char *buf, uint16 maxlength, uint16
void UpdateTextBufferSize(Textbuf *tb)
{
const char *buf = tb->buf;
- WChar c = Utf8Consume(&buf);
tb->width = 0;
- tb->length = 0;
+ tb->size = 1; // terminating zero
- for (; c != '\0' && tb->length < (tb->maxlength - 1); c = Utf8Consume(&buf)) {
+ WChar c;
+ while ((c = Utf8Consume(&buf)) != '\0') {
tb->width += GetCharacterWidth(FS_NORMAL, c);
- tb->length += Utf8CharLen(c);
+ tb->size += Utf8CharLen(c);
}
- tb->caretpos = tb->length;
+ assert(tb->size <= tb->maxsize);
+
+ tb->caretpos = tb->size - 1;
tb->caretxoffs = tb->width;
}
@@ -1163,22 +1167,22 @@ static const WindowDesc _query_string_desc = {
/** Show a query popup window with a textbox in it.
* @param str StringID for the text shown in the textbox
* @param caption StringID of text shown in caption of querywindow
- * @param maxlen maximum length in characters allowed
+ * @param maxsize maximum size in bytes (including terminating '\0')
* @param maxwidth maximum width in pixels allowed
* @param parent pointer to a Window that will handle the events (ok/cancel) of this
* window. If NULL, results are handled by global function HandleOnEditText
* @param afilter filters out unwanted character input
* @param flags various flags, @see QueryStringFlags
*/
-void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
+void ShowQueryString(StringID str, StringID caption, uint maxsize, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
{
DeleteWindowById(WC_QUERY_STRING, 0);
DeleteWindowById(WC_SAVELOAD, 0);
- QueryStringWindow *w = new QueryStringWindow(maxlen + 1, &_query_string_desc, parent);
+ QueryStringWindow *w = new QueryStringWindow(maxsize, &_query_string_desc, parent);
- GetString(w->edit_str_buf, str, &w->edit_str_buf[maxlen]);
- w->edit_str_buf[maxlen] = '\0';
+ GetString(w->edit_str_buf, str, &w->edit_str_buf[maxsize - 1]);
+ w->edit_str_buf[maxsize - 1] = '\0';
if ((flags & QSF_ACCEPT_UNCHANGED) == 0) w->orig = strdup(w->edit_str_buf);
@@ -1194,7 +1198,7 @@ void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth,
w->LowerWidget(QUERY_STR_WIDGET_TEXT);
w->caption = caption;
w->afilter = afilter;
- InitializeTextBuffer(&w->text, w->edit_str_buf, maxlen, maxwidth);
+ InitializeTextBuffer(&w->text, w->edit_str_buf, maxsize, maxwidth);
}
@@ -1583,7 +1587,7 @@ struct SaveLoadWindow : public QueryStringBaseWindow {
ShowHeightmapLoad();
} else {
/* SLD_SAVE_GAME, SLD_SAVE_SCENARIO copy clicked name to editbox */
- ttd_strlcpy(this->text.buf, file->title, this->text.maxlength);
+ ttd_strlcpy(this->text.buf, file->title, this->text.maxsize);
UpdateTextBufferSize(&this->text);
this->InvalidateWidget(10);
}