(svn r4301) - Fix: the maxlength parameter of Textbuf is supposed to be the size of the buffer (so length of string + '\0'), but in the code it was a mix of both. It didn't cause any problems though, only an occasionaly one-less character than allowed. (thanks Tron for noticing)
This commit is contained in:
		@@ -224,7 +224,7 @@ void IConsoleInit(void)
 | 
				
			|||||||
	memset(_iconsole_buffer, 0, sizeof(_iconsole_buffer));
 | 
						memset(_iconsole_buffer, 0, sizeof(_iconsole_buffer));
 | 
				
			||||||
	memset(_iconsole_cbuffer, 0, sizeof(_iconsole_cbuffer));
 | 
						memset(_iconsole_cbuffer, 0, sizeof(_iconsole_cbuffer));
 | 
				
			||||||
	_iconsole_cmdline.buf = calloc(ICON_CMDLN_SIZE, sizeof(*_iconsole_cmdline.buf)); // create buffer and zero it
 | 
						_iconsole_cmdline.buf = calloc(ICON_CMDLN_SIZE, sizeof(*_iconsole_cmdline.buf)); // create buffer and zero it
 | 
				
			||||||
	_iconsole_cmdline.maxlength = ICON_CMDLN_SIZE - 1;
 | 
						_iconsole_cmdline.maxlength = ICON_CMDLN_SIZE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IConsolePrintF(13, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
 | 
						IConsolePrintF(13, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
 | 
				
			||||||
	IConsolePrint(12,  "------------------------------------");
 | 
						IConsolePrint(12,  "------------------------------------");
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										15
									
								
								misc_gui.c
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								misc_gui.c
									
									
									
									
									
								
							@@ -808,8 +808,9 @@ void DeleteTextBufferAll(Textbuf *tb)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Insert a character to a textbuffer. If maxlength is zero, we don't care about
 | 
					 * Insert a character to a textbuffer. If maxlength of the Textbuf is zero,
 | 
				
			||||||
 * the screenlength but only about the physical length of the string
 | 
					 * we don't care about the screenlength but only about the physical
 | 
				
			||||||
 | 
					 * length of the string
 | 
				
			||||||
 * @param tb @Textbuf type to be changed
 | 
					 * @param tb @Textbuf type to be changed
 | 
				
			||||||
 * @param key Character to be inserted
 | 
					 * @param key Character to be inserted
 | 
				
			||||||
 * @return Return true on successfull change of Textbuf, or false otherwise
 | 
					 * @return Return true on successfull change of Textbuf, or false otherwise
 | 
				
			||||||
@@ -817,7 +818,7 @@ void DeleteTextBufferAll(Textbuf *tb)
 | 
				
			|||||||
bool InsertTextBufferChar(Textbuf *tb, byte key)
 | 
					bool InsertTextBufferChar(Textbuf *tb, byte key)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const byte charwidth = GetCharacterWidth(key);
 | 
						const byte charwidth = GetCharacterWidth(key);
 | 
				
			||||||
	if (tb->length < tb->maxlength && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
 | 
						if (tb->length < (tb->maxlength - 1) && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
 | 
				
			||||||
		memmove(tb->buf + tb->caretpos + 1, tb->buf + tb->caretpos, (tb->length - tb->caretpos) + 1);
 | 
							memmove(tb->buf + tb->caretpos + 1, tb->buf + tb->caretpos, (tb->length - tb->caretpos) + 1);
 | 
				
			||||||
		tb->buf[tb->caretpos] = key;
 | 
							tb->buf[tb->caretpos] = key;
 | 
				
			||||||
		tb->length++;
 | 
							tb->length++;
 | 
				
			||||||
@@ -875,12 +876,12 @@ bool MoveTextBufferPos(Textbuf *tb, int navmode)
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
void UpdateTextBufferSize(Textbuf *tb)
 | 
					void UpdateTextBufferSize(Textbuf *tb)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const char* buf;
 | 
						const char *buf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tb->length = 0;
 | 
						tb->length = 0;
 | 
				
			||||||
	tb->width = 0;
 | 
						tb->width = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (buf = tb->buf; *buf != '\0' && tb->length <= tb->maxlength; buf++) {
 | 
						for (buf = tb->buf; *buf != '\0' && tb->length < (tb->maxlength - 1); buf++) {
 | 
				
			||||||
		tb->length++;
 | 
							tb->length++;
 | 
				
			||||||
		tb->width += GetCharacterWidth((byte)*buf);
 | 
							tb->width += GetCharacterWidth((byte)*buf);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -1078,7 +1079,7 @@ void ShowQueryString(StringID str, StringID caption, uint maxlen, uint maxwidth,
 | 
				
			|||||||
	WP(w, querystr_d).wnd_class = window_class;
 | 
						WP(w, querystr_d).wnd_class = window_class;
 | 
				
			||||||
	WP(w, querystr_d).wnd_num = window_number;
 | 
						WP(w, querystr_d).wnd_num = window_number;
 | 
				
			||||||
	WP(w, querystr_d).text.caret = false;
 | 
						WP(w, querystr_d).text.caret = false;
 | 
				
			||||||
	WP(w, querystr_d).text.maxlength = realmaxlen - 1;
 | 
						WP(w, querystr_d).text.maxlength = realmaxlen;
 | 
				
			||||||
	WP(w, querystr_d).text.maxwidth = maxwidth;
 | 
						WP(w, querystr_d).text.maxwidth = maxwidth;
 | 
				
			||||||
	WP(w, querystr_d).text.buf = _edit_str_buf;
 | 
						WP(w, querystr_d).text.buf = _edit_str_buf;
 | 
				
			||||||
	UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
						UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
				
			||||||
@@ -1464,7 +1465,7 @@ void ShowSaveLoadDialog(int mode)
 | 
				
			|||||||
	w->resize.height = w->height - 14 * 10; // Minimum of 10 items
 | 
						w->resize.height = w->height - 14 * 10; // Minimum of 10 items
 | 
				
			||||||
	SETBIT(w->click_state, 7);
 | 
						SETBIT(w->click_state, 7);
 | 
				
			||||||
	WP(w,querystr_d).text.caret = false;
 | 
						WP(w,querystr_d).text.caret = false;
 | 
				
			||||||
	WP(w,querystr_d).text.maxlength = lengthof(_edit_str_buf) - 1;
 | 
						WP(w,querystr_d).text.maxlength = lengthof(_edit_str_buf);
 | 
				
			||||||
	WP(w,querystr_d).text.maxwidth = 240;
 | 
						WP(w,querystr_d).text.maxwidth = 240;
 | 
				
			||||||
	WP(w,querystr_d).text.buf = _edit_str_buf;
 | 
						WP(w,querystr_d).text.buf = _edit_str_buf;
 | 
				
			||||||
	UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
						UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -568,7 +568,7 @@ void ShowNetworkGameWindow(void)
 | 
				
			|||||||
		w->vscroll.cap = 12;
 | 
							w->vscroll.cap = 12;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		querystr->text.caret = true;
 | 
							querystr->text.caret = true;
 | 
				
			||||||
		querystr->text.maxlength = MAX_QUERYSTR_LEN - 1;
 | 
							querystr->text.maxlength = MAX_QUERYSTR_LEN;
 | 
				
			||||||
		querystr->text.maxwidth = 120;
 | 
							querystr->text.maxwidth = 120;
 | 
				
			||||||
		querystr->text.buf = _edit_str_buf;
 | 
							querystr->text.buf = _edit_str_buf;
 | 
				
			||||||
		UpdateTextBufferSize(&querystr->text);
 | 
							UpdateTextBufferSize(&querystr->text);
 | 
				
			||||||
@@ -782,7 +782,7 @@ static void ShowNetworkStartServerWindow(void)
 | 
				
			|||||||
	w->vscroll.count = _fios_num+1;
 | 
						w->vscroll.count = _fios_num+1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	WP(w, network_ql_d).q.text.caret = true;
 | 
						WP(w, network_ql_d).q.text.caret = true;
 | 
				
			||||||
	WP(w, network_ql_d).q.text.maxlength = MAX_QUERYSTR_LEN - 1;
 | 
						WP(w, network_ql_d).q.text.maxlength = MAX_QUERYSTR_LEN;
 | 
				
			||||||
	WP(w, network_ql_d).q.text.maxwidth = 160;
 | 
						WP(w, network_ql_d).q.text.maxwidth = 160;
 | 
				
			||||||
	WP(w, network_ql_d).q.text.buf = _edit_str_buf;
 | 
						WP(w, network_ql_d).q.text.buf = _edit_str_buf;
 | 
				
			||||||
	UpdateTextBufferSize(&WP(w, network_ql_d).q.text);
 | 
						UpdateTextBufferSize(&WP(w, network_ql_d).q.text);
 | 
				
			||||||
@@ -1584,7 +1584,7 @@ void ShowChatWindow(StringID str, StringID caption, int maxlen, int maxwidth, Wi
 | 
				
			|||||||
	WP(w,querystr_d).wnd_class = window_class;
 | 
						WP(w,querystr_d).wnd_class = window_class;
 | 
				
			||||||
	WP(w,querystr_d).wnd_num = window_number;
 | 
						WP(w,querystr_d).wnd_num = window_number;
 | 
				
			||||||
	WP(w,querystr_d).text.caret = false;
 | 
						WP(w,querystr_d).text.caret = false;
 | 
				
			||||||
	WP(w,querystr_d).text.maxlength = maxlen - 1;
 | 
						WP(w,querystr_d).text.maxlength = maxlen;
 | 
				
			||||||
	WP(w,querystr_d).text.maxwidth = maxwidth;
 | 
						WP(w,querystr_d).text.maxwidth = maxwidth;
 | 
				
			||||||
	WP(w,querystr_d).text.buf = _edit_str_buf;
 | 
						WP(w,querystr_d).text.buf = _edit_str_buf;
 | 
				
			||||||
	UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
						UpdateTextBufferSize(&WP(w, querystr_d).text);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										7
									
								
								win32.c
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								win32.c
									
									
									
									
									
								
							@@ -1228,14 +1228,13 @@ bool InsertTextBufferClipboard(Textbuf *tb)
 | 
				
			|||||||
		data = GlobalLock(cbuf); // clipboard data
 | 
							data = GlobalLock(cbuf); // clipboard data
 | 
				
			||||||
		dataptr = data;
 | 
							dataptr = data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for (; IsValidAsciiChar(*dataptr) && (tb->length + length) < tb->maxlength - 1 &&
 | 
							for (; IsValidAsciiChar(*dataptr) && (tb->length + length) < (tb->maxlength - 1) &&
 | 
				
			||||||
				(tb->maxwidth == 0 || width + tb->width + GetCharacterWidth((byte)*dataptr) <= tb->maxwidth); dataptr++) {
 | 
									(tb->maxwidth == 0 || width + tb->width + GetCharacterWidth((byte)*dataptr) <= tb->maxwidth); dataptr++) {
 | 
				
			||||||
					width += GetCharacterWidth((byte)*dataptr);
 | 
										width += GetCharacterWidth((byte)*dataptr);
 | 
				
			||||||
			length++;
 | 
								length++;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (length == 0)
 | 
							if (length == 0) return false;
 | 
				
			||||||
			return false;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos);
 | 
							memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos);
 | 
				
			||||||
		memcpy(tb->buf + tb->caretpos, data, length);
 | 
							memcpy(tb->buf + tb->caretpos, data, length);
 | 
				
			||||||
@@ -1244,7 +1243,7 @@ bool InsertTextBufferClipboard(Textbuf *tb)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		tb->length += length;
 | 
							tb->length += length;
 | 
				
			||||||
		tb->caretpos += length;
 | 
							tb->caretpos += length;
 | 
				
			||||||
		tb->buf[tb->length + 1] = '\0'; // terminating zero
 | 
							tb->buf[tb->length] = '\0'; // terminating zero
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		GlobalUnlock(cbuf);
 | 
							GlobalUnlock(cbuf);
 | 
				
			||||||
		CloseClipboard();
 | 
							CloseClipboard();
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								window.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								window.h
									
									
									
									
									
								
							@@ -238,7 +238,7 @@ enum {
 | 
				
			|||||||
typedef struct Textbuf {
 | 
					typedef struct Textbuf {
 | 
				
			||||||
	char *buf;                  /* buffer in which text is saved */
 | 
						char *buf;                  /* buffer in which text is saved */
 | 
				
			||||||
	uint16 maxlength, maxwidth; /* the maximum size of the buffer. Maxwidth specifies screensize in pixels */
 | 
						uint16 maxlength, maxwidth; /* the maximum size of the buffer. Maxwidth specifies screensize in pixels */
 | 
				
			||||||
	uint16 length, width;       /* the current size of the buffer. Width specifies screensize in pixels */
 | 
						uint16 length, width;       /* the current size of the string. Width specifies screensize in pixels */
 | 
				
			||||||
	bool caret;                 /* is the caret ("_") visible or not */
 | 
						bool caret;                 /* is the caret ("_") visible or not */
 | 
				
			||||||
	uint16 caretpos;            /* the current position of the caret in the buffer */
 | 
						uint16 caretpos;            /* the current position of the caret in the buffer */
 | 
				
			||||||
	uint16 caretxoffs;          /* the current position of the caret in pixels */
 | 
						uint16 caretxoffs;          /* the current position of the caret in pixels */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user