Skip to content

Commit de15bdb

Browse files
committed
Merge branch 'jk/config-no-ungetc-eof'
Reading configuration from a blob object, when it ends with a lone CR, use to confuse the configuration parser. * jk/config-no-ungetc-eof: config_buf_ungetc: warn when pushing back a random character config: do not ungetc EOF
2 parents 2c1f554 + 1d0655c commit de15bdb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

config.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ static int config_buf_fgetc(struct config_source *conf)
7373

7474
static int config_buf_ungetc(int c, struct config_source *conf)
7575
{
76-
if (conf->u.buf.pos > 0)
77-
return conf->u.buf.buf[--conf->u.buf.pos];
76+
if (conf->u.buf.pos > 0) {
77+
conf->u.buf.pos--;
78+
if (conf->u.buf.buf[conf->u.buf.pos] != c)
79+
die("BUG: config_buf can only ungetc the same character");
80+
return c;
81+
}
7882

7983
return EOF;
8084
}
@@ -235,7 +239,8 @@ static int get_next_char(void)
235239
/* DOS like systems */
236240
c = cf->do_fgetc(cf);
237241
if (c != '\n') {
238-
cf->do_ungetc(c, cf);
242+
if (c != EOF)
243+
cf->do_ungetc(c, cf);
239244
c = '\r';
240245
}
241246
}

t/t1307-config-blob.sh

+9
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ test_expect_success 'parse errors in blobs are properly attributed' '
6767
grep "HEAD:config" err
6868
'
6969

70+
test_expect_success 'can parse blob ending with CR' '
71+
printf "[some]key = value\\r" >config &&
72+
git add config &&
73+
git commit -m CR &&
74+
echo value >expect &&
75+
git config --blob=HEAD:config some.key >actual &&
76+
test_cmp expect actual
77+
'
78+
7079
test_done

0 commit comments

Comments
 (0)