Skip to content

Commit 28110d4

Browse files
bk2204gitster
authored andcommitted
commit: reject invalid UTF-8 codepoints
The commit code already contains code for validating UTF-8, but it does not check for invalid values, such as guaranteed non-characters and surrogates. Fix this by explicitly checking for and rejecting such characters. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 81a199b commit 28110d4

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

commit.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ static int find_invalid_utf8(const char *buf, int len)
12441244
while (len) {
12451245
unsigned char c = *buf++;
12461246
int bytes, bad_offset;
1247+
unsigned int codepoint;
12471248

12481249
len--;
12491250
offset++;
@@ -1264,24 +1265,40 @@ static int find_invalid_utf8(const char *buf, int len)
12641265
bytes++;
12651266
}
12661267

1267-
/* Must be between 1 and 5 more bytes */
1268-
if (bytes < 1 || bytes > 5)
1268+
/*
1269+
* Must be between 1 and 3 more bytes. Longer sequences result in
1270+
* codepoints beyond U+10FFFF, which are guaranteed never to exist.
1271+
*/
1272+
if (bytes < 1 || 3 < bytes)
12691273
return bad_offset;
12701274

12711275
/* Do we *have* that many bytes? */
12721276
if (len < bytes)
12731277
return bad_offset;
12741278

1279+
/* Place the encoded bits at the bottom of the value. */
1280+
codepoint = (c & 0x7f) >> bytes;
1281+
12751282
offset += bytes;
12761283
len -= bytes;
12771284

12781285
/* And verify that they are good continuation bytes */
12791286
do {
1287+
codepoint <<= 6;
1288+
codepoint |= *buf & 0x3f;
12801289
if ((*buf++ & 0xc0) != 0x80)
12811290
return bad_offset;
12821291
} while (--bytes);
12831292

1284-
/* We could/should check the value and length here too */
1293+
/* No codepoints can ever be allocated beyond U+10FFFF. */
1294+
if (codepoint > 0x10ffff)
1295+
return bad_offset;
1296+
/* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1297+
if ((codepoint & 0x1ff800) == 0xd800)
1298+
return bad_offset;
1299+
/* U+FFFE and U+FFFF are guaranteed non-characters. */
1300+
if ((codepoint & 0x1ffffe) == 0xfffe)
1301+
return bad_offset;
12851302
}
12861303
return -1;
12871304
}
@@ -1292,8 +1309,8 @@ static int find_invalid_utf8(const char *buf, int len)
12921309
* If it isn't, it assumes any non-utf8 characters are Latin1,
12931310
* and does the conversion.
12941311
*
1295-
* Fixme: we should probably also disallow overlong forms and
1296-
* invalid characters. But we don't do that currently.
1312+
* Fixme: we should probably also disallow overlong forms.
1313+
* But we don't do that currently.
12971314
*/
12981315
static int verify_utf8(struct strbuf *buf)
12991316
{

t/t3900-i18n-commit.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ test_expect_failure 'UTF-16 refused because of NULs' '
3939
git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
4040
'
4141

42+
test_expect_success 'UTF-8 invalid characters refused' '
43+
test_when_finished "rm -f $HOME/stderr $HOME/invalid" &&
44+
echo "UTF-8 characters" >F &&
45+
printf "Commit message\n\nInvalid surrogate:\355\240\200\n" \
46+
>"$HOME/invalid" &&
47+
git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
48+
grep "did not conform" "$HOME"/stderr
49+
'
4250

4351
for H in ISO8859-1 eucJP ISO-2022-JP
4452
do

0 commit comments

Comments
 (0)