Skip to content

Commit 623f77e

Browse files
committed
Avoid possibly accessing off the end of memory in SJIS2004 conversion.
The code in shift_jis_20042euc_jis_2004() would fetch two bytes even when only one remained in the string. Since conversion functions aren't supposed to assume null-terminated input, this poses a small risk of fetching past the end of memory and incurring SIGSEGV. No such crash has been identified in the field, but we've certainly seen the equivalent happen in other code paths, so patch this one all the way back. Report and patch by Noah Misch.
1 parent 780a342 commit 623f77e

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/backend/utils/mb/conversion_procs/euc2004_sjis2004/euc2004_sjis2004.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ get_ten(int b, int *ku)
218218
static void
219219
shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len)
220220
{
221-
int c1,
222-
c2;
221+
int c1;
223222
int ku,
224223
ten,
225224
kubun;
@@ -229,7 +228,6 @@ shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len
229228
while (len > 0)
230229
{
231230
c1 = *sjis;
232-
c2 = sjis[1];
233231

234232
if (!IS_HIGHBIT_SET(c1))
235233
{
@@ -245,7 +243,7 @@ shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len
245243

246244
l = pg_encoding_verifymb(PG_SHIFT_JIS_2004, (const char *) sjis, len);
247245

248-
if (l < 0)
246+
if (l < 0 || l > len)
249247
report_invalid_encoding(PG_SHIFT_JIS_2004,
250248
(const char *) sjis, len);
251249

@@ -257,6 +255,8 @@ shift_jis_20042euc_jis_2004(const unsigned char *sjis, unsigned char *p, int len
257255
}
258256
else if (l == 2)
259257
{
258+
int c2 = sjis[1];
259+
260260
plane = 1;
261261
ku = 1;
262262
ten = 1;

0 commit comments

Comments
 (0)