Skip to content

Commit e82bd6c

Browse files
bk2204gitster
authored andcommitted
commit: reject overlong UTF-8 sequences
The commit code accepts pseudo-UTF-8 sequences that encode a character with more bytes than necessary. Reject such sequences, since they are not valid UTF-8. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28110d4 commit e82bd6c

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

commit.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,11 +1240,15 @@ int commit_tree(const struct strbuf *msg, unsigned char *tree,
12401240
static int find_invalid_utf8(const char *buf, int len)
12411241
{
12421242
int offset = 0;
1243+
static const unsigned int max_codepoint[] = {
1244+
0x7f, 0x7ff, 0xffff, 0x10ffff
1245+
};
12431246

12441247
while (len) {
12451248
unsigned char c = *buf++;
12461249
int bytes, bad_offset;
12471250
unsigned int codepoint;
1251+
unsigned int min_val, max_val;
12481252

12491253
len--;
12501254
offset++;
@@ -1276,8 +1280,13 @@ static int find_invalid_utf8(const char *buf, int len)
12761280
if (len < bytes)
12771281
return bad_offset;
12781282

1279-
/* Place the encoded bits at the bottom of the value. */
1283+
/*
1284+
* Place the encoded bits at the bottom of the value and compute the
1285+
* valid range.
1286+
*/
12801287
codepoint = (c & 0x7f) >> bytes;
1288+
min_val = max_codepoint[bytes-1] + 1;
1289+
max_val = max_codepoint[bytes];
12811290

12821291
offset += bytes;
12831292
len -= bytes;
@@ -1290,8 +1299,8 @@ static int find_invalid_utf8(const char *buf, int len)
12901299
return bad_offset;
12911300
} while (--bytes);
12921301

1293-
/* No codepoints can ever be allocated beyond U+10FFFF. */
1294-
if (codepoint > 0x10ffff)
1302+
/* Reject codepoints that are out of range for the sequence length. */
1303+
if (codepoint < min_val || codepoint > max_val)
12951304
return bad_offset;
12961305
/* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
12971306
if ((codepoint & 0x1ff800) == 0xd800)
@@ -1308,9 +1317,6 @@ static int find_invalid_utf8(const char *buf, int len)
13081317
*
13091318
* If it isn't, it assumes any non-utf8 characters are Latin1,
13101319
* and does the conversion.
1311-
*
1312-
* Fixme: we should probably also disallow overlong forms.
1313-
* But we don't do that currently.
13141320
*/
13151321
static int verify_utf8(struct strbuf *buf)
13161322
{

t/t3900-i18n-commit.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ test_expect_success 'UTF-8 invalid characters refused' '
4848
grep "did not conform" "$HOME"/stderr
4949
'
5050

51+
test_expect_success 'UTF-8 overlong sequences rejected' '
52+
test_when_finished "rm -f $HOME/stderr $HOME/invalid" &&
53+
rm -f "$HOME/stderr" "$HOME/invalid" &&
54+
echo "UTF-8 overlong" >F &&
55+
printf "\340\202\251ommit message\n\nThis is not a space:\300\240\n" \
56+
>"$HOME/invalid" &&
57+
git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
58+
grep "did not conform" "$HOME"/stderr
59+
'
60+
5161
for H in ISO8859-1 eucJP ISO-2022-JP
5262
do
5363
test_expect_success "$H setup" '

0 commit comments

Comments
 (0)