From d606b2a8fff94afa21bae57bd0045b409136b72d Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 20 Dec 2016 12:43:35 -0800 Subject: [PATCH] tty's compress_str(), core's dat/keyhelp Rewrite 3.6.1's compress_str() to avoid peeking past the end of the input string. This should eliminate the reported valgrind complaint. The problem was noticed for post-3.6.0 code introduced [by me...] last June, but it looks like it was present in the old code too. Also, fix the wording in the paragraph about NUL in the keyhelp text. tty_putstr() always passes non-message window text through compress_str(), clobbering usage of two spaces to separate sentences. putstr()'s caller ought to have more control over that (possibly via its hardly ever used 'attribute' arg?). --- dat/keyhelp | 12 ++++++------ win/tty/wintty.c | 27 +++++++++++++++------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/dat/keyhelp b/dat/keyhelp index 41ab37ef1..d4df8d444 100644 --- a/dat/keyhelp +++ b/dat/keyhelp @@ -14,12 +14,12 @@ is reporting the wrong character but will be operating correctly if it describes ^J when you type ^M. - A NUL character, typed as ^ on some keyboards, ^@ on others, - and maybe not typeable at all on yet others. It is not used as a - command, and will be converted into ESC before reaching 'whatdoes'. - Unlike ^M, this transformation is performed by NetHack itself. - But like ^M, if you type NUL and get feedback about ESC, the - situation is expected. + A NUL character, which is typed as ^ on some keyboards, + ^@ on others, and maybe not typeable at all on yet others, is not + used as a command, and will be converted into ESC before reaching + 'whatdoes'. Unlike ^M, this transformation is performed within + NetHack. But like ^M, if you type NUL and get feedback about ESC, + the situation is expected. ESC itself is a synonym for ^[, and is another source of oddity. Various function keys, including cursor arrow keys, may transmit diff --git a/win/tty/wintty.c b/win/tty/wintty.c index e38e3e7c8..adec33c22 100644 --- a/win/tty/wintty.c +++ b/win/tty/wintty.c @@ -2448,19 +2448,22 @@ const char *str; topline wrapping converts space at wrap point into newline, we reverse that here */ if ((int) strlen(str) >= CO || index(str, '\n')) { - register const char *bp0 = str; - char c, nxtc, *bp1 = cbuf, *endbp1 = &cbuf[sizeof cbuf - 1]; + const char *in_str = str; + char c, *outstr = cbuf, *outend = &cbuf[sizeof cbuf - 1]; + boolean was_space = TRUE; /* True discards all leading spaces; + False would retain one if present */ - cbuf[0] = cbuf[sizeof cbuf - 1] = '\0'; /* superfluous */ - nxtc = (*bp0 == '\n') ? ' ' : *bp0; - do { - c = nxtc; - nxtc = bp0[1]; - if (nxtc == '\n') - nxtc = ' '; - if (c != ' ' || nxtc != ' ') - *bp1++ = c; - } while (*bp0++ && bp1 < endbp1); + while ((c = *in_str++) != '\0' && outstr < outend) { + if (c == '\n') + c = ' '; + if (was_space && c == ' ') + continue; + *outstr++ = c; + was_space = (c == ' '); + } + if ((was_space && outstr > cbuf) || outstr == outend) + --outstr; /* remove trailing space or make room for terminator */ + *outstr = '\0'; str = cbuf; } return str;