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?).
This commit is contained in:
PatR
2016-12-20 12:43:35 -08:00
parent f4632732ac
commit d606b2a8ff
2 changed files with 21 additions and 18 deletions

View File

@@ -14,12 +14,12 @@
is reporting the wrong character but will be operating correctly if is reporting the wrong character but will be operating correctly if
it describes ^J when you type ^M. it describes ^J when you type ^M.
A NUL character, typed as ^<space> on some keyboards, ^@ on others, A NUL character, which is typed as ^<space> on some keyboards,
and maybe not typeable at all on yet others. It is not used as a ^@ on others, and maybe not typeable at all on yet others, is not
command, and will be converted into ESC before reaching 'whatdoes'. used as a command, and will be converted into ESC before reaching
Unlike ^M, this transformation is performed by NetHack itself. 'whatdoes'. Unlike ^M, this transformation is performed within
But like ^M, if you type NUL and get feedback about ESC, the NetHack. But like ^M, if you type NUL and get feedback about ESC,
situation is expected. the situation is expected.
ESC itself is a synonym for ^[, and is another source of oddity. ESC itself is a synonym for ^[, and is another source of oddity.
Various function keys, including cursor arrow keys, may transmit Various function keys, including cursor arrow keys, may transmit

View File

@@ -2448,19 +2448,22 @@ const char *str;
topline wrapping converts space at wrap point into newline, topline wrapping converts space at wrap point into newline,
we reverse that here */ we reverse that here */
if ((int) strlen(str) >= CO || index(str, '\n')) { if ((int) strlen(str) >= CO || index(str, '\n')) {
register const char *bp0 = str; const char *in_str = str;
char c, nxtc, *bp1 = cbuf, *endbp1 = &cbuf[sizeof cbuf - 1]; 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 */ while ((c = *in_str++) != '\0' && outstr < outend) {
nxtc = (*bp0 == '\n') ? ' ' : *bp0; if (c == '\n')
do { c = ' ';
c = nxtc; if (was_space && c == ' ')
nxtc = bp0[1]; continue;
if (nxtc == '\n') *outstr++ = c;
nxtc = ' '; was_space = (c == ' ');
if (c != ' ' || nxtc != ' ') }
*bp1++ = c; if ((was_space && outstr > cbuf) || outstr == outend)
} while (*bp0++ && bp1 < endbp1); --outstr; /* remove trailing space or make room for terminator */
*outstr = '\0';
str = cbuf; str = cbuf;
} }
return str; return str;