Skip to content

Commit 563b352

Browse files
committed
Merge branch 'jk/maint-config-bogus-section'
"git config --rename-section" to rename an existing section into a bogus one did not check the new name. By Jeff King * jk/maint-config-bogus-section: config: reject bogus section names for --rename-section
2 parents 10d4332 + 94a35b1 commit 563b352

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

config.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,20 +1558,42 @@ static int section_name_match (const char *buf, const char *name)
15581558
return 0;
15591559
}
15601560

1561+
static int section_name_is_ok(const char *name)
1562+
{
1563+
/* Empty section names are bogus. */
1564+
if (!*name)
1565+
return 0;
1566+
1567+
/*
1568+
* Before a dot, we must be alphanumeric or dash. After the first dot,
1569+
* anything goes, so we can stop checking.
1570+
*/
1571+
for (; *name && *name != '.'; name++)
1572+
if (*name != '-' && !isalnum(*name))
1573+
return 0;
1574+
return 1;
1575+
}
1576+
15611577
/* if new_name == NULL, the section is removed instead */
15621578
int git_config_rename_section_in_file(const char *config_filename,
15631579
const char *old_name, const char *new_name)
15641580
{
15651581
int ret = 0, remove = 0;
15661582
char *filename_buf = NULL;
1567-
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1583+
struct lock_file *lock;
15681584
int out_fd;
15691585
char buf[1024];
15701586
FILE *config_file;
15711587

1588+
if (new_name && !section_name_is_ok(new_name)) {
1589+
ret = error("invalid section name: %s", new_name);
1590+
goto out;
1591+
}
1592+
15721593
if (!config_filename)
15731594
config_filename = filename_buf = git_pathdup("config");
15741595

1596+
lock = xcalloc(sizeof(struct lock_file), 1);
15751597
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
15761598
if (out_fd < 0) {
15771599
ret = error("could not lock config file %s", config_filename);

t/t1300-repo-config.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ EOF
550550

551551
test_expect_success "rename succeeded" "test_cmp expect .git/config"
552552

553+
test_expect_success 'renaming empty section name is rejected' '
554+
test_must_fail git config --rename-section branch.zwei ""
555+
'
556+
557+
test_expect_success 'renaming to bogus section is rejected' '
558+
test_must_fail git config --rename-section branch.zwei "bogus name"
559+
'
560+
553561
cat >> .git/config << EOF
554562
[branch "zwei"] a = 1 [branch "vier"]
555563
EOF

0 commit comments

Comments
 (0)