1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-26 01:32:25 +00:00

Tighten assertions in Windows wc_to_mb.

This assertion was supposed to be checking for the buffer overrun
fixed by the previous commit, but because it checks the buffer index
just _after_ writing into the buffer, it would have permitted a
one-byte overrun before failing the assertion.
This commit is contained in:
Simon Tatham 2019-07-02 21:22:01 +01:00
parent 54cd853a49
commit 11f504c440

View File

@ -1170,21 +1170,28 @@ int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
wchar_t ch = wcstr[i]; wchar_t ch = wcstr[i];
int by; int by;
char *p1; char *p1;
if (ucsdata->uni_tbl && (p1 = ucsdata->uni_tbl[(ch >> 8) & 0xFF])
&& (by = p1[ch & 0xFF])) #define WRITECH(chr) do \
*p++ = by; { \
assert(p - mbstr < mblen); \
*p++ = (char)(chr); \
} while (0)
if (ucsdata->uni_tbl &&
(p1 = ucsdata->uni_tbl[(ch >> 8) & 0xFF]) != NULL &&
(by = p1[ch & 0xFF]) != '\0')
WRITECH(by);
else if (ch < 0x80) else if (ch < 0x80)
*p++ = (char) ch; WRITECH(ch);
else if (defchr) { else if (defchr)
int j; for (const char *q = defchr; *q; q++)
for (j = 0; defchr[j]; j++) WRITECH(*q);
*p++ = defchr[j];
}
#if 1 #if 1
else else
*p++ = '.'; WRITECH('.');
#endif #endif
assert(p - mbstr < mblen);
#undef WRITECH
} }
return p - mbstr; return p - mbstr;
} else { } else {