Changeset - r14951:e24469d4e684
[Not reviewed]
master
0 3 0
yexo - 14 years ago 2010-04-06 21:16:36
yexo@openttd.org
(svn r19569) -Fix: possible buffer underflow in newgrf string code
3 files changed with 5 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/newgrf_text.cpp
Show inline comments
 
@@ -633,13 +633,13 @@ void RewindTextRefStack()
 
 * @param scc   the string control code that has been read
 
 * @param buff  the buffer we're writing to
 
 * @param str   the string that we need to write
 
 * @param argv  the OpenTTD stack of values
 
 * @return the string control code to "execute" now
 
 */
 
uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv)
 
uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv)
 
{
 
	if (_newgrf_textrefstack->used) {
 
		switch (scc) {
 
			default: NOT_REACHED();
 
			case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
 
			case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
 
@@ -660,13 +660,13 @@ uint RemapNewGRFStringControlCode(uint s
 
			case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
 

	
 
			case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
 

	
 
			case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
 
			case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
 
			case SCC_NEWGRF_UNPRINT:              *buff -= Utf8Consume(str); break;
 
			case SCC_NEWGRF_UNPRINT:              *buff = max(*buff - Utf8Consume(str), buf_start); break;
 

	
 
			case SCC_NEWGRF_PRINT_STRING_ID:
 
				*argv = TTDPStringIDToOTTDStringIDMapping(_newgrf_textrefstack->PopUnsignedWord());
 
				break;
 
		}
 
	}
src/newgrf_text.h
Show inline comments
 
@@ -25,11 +25,11 @@ bool CheckGrfLangID(byte lang_id, byte g
 

	
 
void PrepareTextRefStackUsage(byte numEntries);
 
void StopTextRefStackUsage();
 
void SwitchToNormalRefStack();
 
void SwitchToErrorRefStack();
 
void RewindTextRefStack();
 
uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv);
 
uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv);
 

	
 
StringID TTDPStringIDToOTTDStringIDMapping(StringID string);
 

	
 
#endif /* NEWGRF_TEXT_H */
src/strings.cpp
Show inline comments
 
@@ -551,17 +551,18 @@ uint ConvertDisplaySpeedToSpeed(uint spe
 

	
 
static char *FormatString(char *buff, const char *str, int64 *argv, uint casei, const char *last)
 
{
 
	WChar b;
 
	int64 *argv_orig = argv;
 
	uint modifier = 0;
 
	char *buf_start = buff;
 

	
 
	while ((b = Utf8Consume(&str)) != '\0') {
 
		if (SCC_NEWGRF_FIRST <= b && b <= SCC_NEWGRF_LAST) {
 
			/* We need to pass some stuff as it might be modified; oh boy. */
 
			b = RemapNewGRFStringControlCode(b, &buff, &str, argv);
 
			b = RemapNewGRFStringControlCode(b, buf_start, &buff, &str, argv);
 
			if (b == 0) continue;
 
		}
 

	
 
		switch (b) {
 
			case SCC_SETX: // {SETX}
 
				if (buff + Utf8CharLen(SCC_SETX) + 1 < last) {
0 comments (0 inline, 0 general)