1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Remove some pointless 'static' qualifiers.

In windows/window.c, a few variables inside functions were declared as
static, with no particular purpose that I can see: they don't seem to
have any reason to persist between calls to the function. So it makes
more sense to have them be ordinary stack-allocated automatic
variables.

Static variables removed by this commit:

 - 'RECT ss' in reset_window.
 - 'WORD keys[3]' and 'BYTE keysb[3]' in TranslateKey.
 - several (buffer, length) pairs in do_text_internal.
 - keys_unicode[] in TranslateKey.

All of these variables were originally introduced in patches credited
to Robert de Bath, which means I can't even try to reconstruct my
original thought processes, because they weren't _my_ thoughts anyway.
The arrays in do_text_internal are the easiest to understand: they're
reallocated larger as necessary, and making them static means the
allocation from a previous call can be reused, saving a malloc (though
I don't think that's a good enough reason to bother, these days).

The fixed-size static arrays and RECT are harder to explain. I suspect
they might originally have been that way because of 1990s attitudes to
performance: in x86-32 it's probably marginally faster to give your
variables constant addresses than sp-relative ones, and in the 1990s
computers were much slower, so there's an argument for making things
static if you have no _need_ to make them automatic. These days, the
difference is negligible, and persistent state is much more widely
recognised as a risk!

But keys_unicode[] is by far the strangest, because there was code
that clearly _did_ expect it to persist between calls, namely three
assignments to keys_unicode[0] near the end of the function after it's
finished being used for any other purpose, and a conditioned-out set
of debug() calls at the top of the function that print its contents
before anything has yet written to it.

But as far as I can see, the persistent data in the array is otherwise
completely unused. In any call to the function, if keys_unicode is
used at all, then it's either written directly by a call to ToAsciiEx,
or else (for pre-NT platforms) converted from ToAsciiEx's output via
MultiByteToWideChar. In both cases, the integer variable 'r' indicates
how many array elements were written, and subsequent accesses only
ever read those elements. So the assignments to keys_unicode[0] at the
end of the previous call will be overwritten before anything at all
can depend on them - with the exception of those debug statements.

I don't really understand what was going on here. It's tempting to
guess that those final assignments must have once done something
useful, and the code that used them was later removed. But the source
control history doesn't bear that out: a static array of three
elements (under its original name 'keys') was introduced in commit
0d5d39064a, and then commits 953b7775b3 and 26f1085038
added the other two assignments. And as far as I can see, even as of
the original commit 0d5d39064a, the code already had the property
that there was a final assignment to keys[0] which would inevitably be
overwritten in the next call before it could affect anything.

So I'm totally confused about what those assignments were _ever_
useful for. But an email thread from the time suggests that some of
those patches were being rebased repeatedly past other work (or
rather, the much less reliable CVS analogue of rebasing), so my best
guess is that that's where the confusion crept in - perhaps in RDB's
original version of the code they did do something useful.

Regardless of that, I'm pretty convinced that persistent array can't
be doing anything useful _now_. So I'm taking it out. But if anyone
reports a bug resulting from this change, then I'll eat my words - and
with any luck the details of the bug report will give us a clue what's
going on, and then we can put back some equivalent functionality with
much better comments!
This commit is contained in:
Simon Tatham 2023-05-27 14:56:31 +01:00
parent 14d47544ad
commit afb3dab1e9

View File

@ -1908,7 +1908,7 @@ static void reset_window(WinGuiSeat *wgs, int reinit)
if (win_width != font_width*wgs->term->cols + offset_width*2 ||
win_height != font_height*wgs->term->rows + offset_height*2) {
static RECT ss;
RECT ss;
int width, height;
get_fullscreen_rect(wgs, &ss);
@ -3538,9 +3538,12 @@ static void do_text_internal(
int maxlen, remaining;
bool opaque;
bool is_cursor = false;
static int *lpDx = NULL;
static size_t lpDx_len = 0;
int *lpDx = NULL;
size_t lpDx_len = 0;
int *lpDx_maybe;
wchar_t *wbuf = NULL;
char *cbuf = NULL;
size_t wbuflen = 0, cbuflen = 0;
int len2; /* for SURROGATE PAIR */
lattr &= LATTR_MODE;
@ -3782,17 +3785,11 @@ static void do_text_internal(
(text[0] & CSET_MASK) == CSET_ACP) {
/* Ho Hum, dbcs fonts are a PITA! */
/* To display on W9x I have to convert to UCS */
static wchar_t *uni_buf = 0;
static int uni_len = 0;
int nlen, mptr;
if (len > uni_len) {
sfree(uni_buf);
uni_len = len;
uni_buf = snewn(uni_len, wchar_t);
}
sgrowarray(wbuf, wbuflen, len);
for (nlen = mptr = 0; mptr<len; mptr++) {
uni_buf[nlen] = 0xFFFD;
wbuf[nlen] = 0xFFFD;
if (IsDBCSLeadByteEx(wgs->ucsdata.font_codepage,
(BYTE) text[mptr])) {
char dbcstext[2];
@ -3801,19 +3798,19 @@ static void do_text_internal(
lpDx[nlen] += char_width;
MultiByteToWideChar(
wgs->ucsdata.font_codepage, MB_USEGLYPHCHARS,
dbcstext, 2, uni_buf+nlen, 1);
dbcstext, 2, wbuf+nlen, 1);
mptr++;
} else {
char dbcstext[1];
dbcstext[0] = text[mptr] & 0xFF;
MultiByteToWideChar(
wgs->ucsdata.font_codepage, MB_USEGLYPHCHARS,
dbcstext, 1, uni_buf+nlen, 1);
dbcstext, 1, wbuf+nlen, 1);
}
nlen++;
}
if (nlen <= 0)
return; /* Eeek! */
goto out; /* Eeek! */
ExtTextOutW(wgs->wintw_hdc, x + xoffset,
y - font_height * (lattr == LATTR_BOT) + text_adjust,
@ -3830,12 +3827,9 @@ static void do_text_internal(
lpDx[0] = -1;
} else if (DIRECT_FONT(text[0])) {
static char *directbuf = NULL;
static size_t directlen = 0;
sgrowarray(directbuf, directlen, len);
sgrowarray(cbuf, cbuflen, len);
for (size_t i = 0; i < len; i++)
directbuf[i] = text[i] & 0xFF;
cbuf[i] = text[i] & 0xFF;
ExtTextOut(wgs->wintw_hdc, x + xoffset,
y - font_height * (lattr == LATTR_BOT) + text_adjust,
@ -3860,17 +3854,8 @@ static void do_text_internal(
}
} else {
/* And 'normal' unicode characters */
static WCHAR *wbuf = NULL;
static int wlen = 0;
int i;
if (wlen < len) {
sfree(wbuf);
wlen = len;
wbuf = snewn(wlen, WCHAR);
}
for (i = 0; i < len; i++)
sgrowarray(wbuf, wbuflen, len);
for (int i = 0; i < len; i++)
wbuf[i] = text[i];
/* print Glyphs as they are, without Windows' Shaping*/
@ -3905,6 +3890,11 @@ static void do_text_internal(
if (attr & ATTR_STRIKE)
draw_horizontal_line_on_text(wgs, wgs->font_strikethrough_y, lattr,
line_box, fg);
out:
sfree(lpDx);
sfree(wbuf);
sfree(cbuf);
}
/*
@ -4155,7 +4145,6 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
HKL kbd_layout = GetKeyboardLayout(0);
static wchar_t keys_unicode[3];
static int compose_char = 0;
static WPARAM compose_keycode = 0;
@ -4207,13 +4196,6 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
else if (ch)
debug(", $%02x", ch);
if (keys_unicode[0])
debug(", KB0=%04x", keys_unicode[0]);
if (keys_unicode[1])
debug(", KB1=%04x", keys_unicode[1]);
if (keys_unicode[2])
debug(", KB2=%04x", keys_unicode[2]);
if ((keystate[VK_SHIFT] & 0x80) != 0)
debug(", S");
if ((keystate[VK_CONTROL] & 0x80) != 0)
@ -4678,6 +4660,8 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
keystate[VK_CAPITAL] = 0;
}
wchar_t keys_unicode[3];
/* XXX how do we know what the max size of the keys array should
* be is? There's indication on MS' website of an Inquire/InquireEx
* functioning returning a KBINFO structure which tells us. */
@ -4696,8 +4680,8 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
* See wishlist item `win-dead-keys' for more horrible detail
* and speculations. */
int i;
static WORD keys[3];
static BYTE keysb[3];
WORD keys[3];
BYTE keysb[3];
r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
if (r > 0) {
for (i = 0; i < r; i++) {
@ -4794,18 +4778,8 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam,
show_mouseptr(wgs, false);
}
/* This is so the ALT-Numpad and dead keys work correctly. */
keys_unicode[0] = 0;
return p - output;
}
/* If we're definitely not building up an ALT-54321 then clear it */
if (!left_alt)
keys_unicode[0] = 0;
/* If we will be using alt_sum fix the 256s */
else if (keys_unicode[0] && (in_utf(wgs->term) ||
wgs->ucsdata.dbcs_screenfont))
keys_unicode[0] = 10;
}
/*