summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2012-04-30 21:46:46 (GMT)
committerJunio C Hamano <gitster@pobox.com>2012-04-30 21:46:46 (GMT)
commit563b3527b41a978eeef77d9426be460013c35d88 (patch)
tree5ca590f5d68d5b74b41451403a34220f2d30d455 /config.c
parent10d4332e007132a38dc61f03c760d355da5cd550 (diff)
parent94a35b1aea88f1ad882cdd111e01410fb6d3eb46 (diff)
downloadgit-563b3527b41a978eeef77d9426be460013c35d88.zip
git-563b3527b41a978eeef77d9426be460013c35d88.tar.gz
git-563b3527b41a978eeef77d9426be460013c35d88.tar.bz2
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
Diffstat (limited to 'config.c')
-rw-r--r--config.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/config.c b/config.c
index 2bbf02d..ac69cb6 100644
--- a/config.c
+++ b/config.c
@@ -1558,20 +1558,42 @@ static int section_name_match (const char *buf, const char *name)
return 0;
}
+static int section_name_is_ok(const char *name)
+{
+ /* Empty section names are bogus. */
+ if (!*name)
+ return 0;
+
+ /*
+ * Before a dot, we must be alphanumeric or dash. After the first dot,
+ * anything goes, so we can stop checking.
+ */
+ for (; *name && *name != '.'; name++)
+ if (*name != '-' && !isalnum(*name))
+ return 0;
+ return 1;
+}
+
/* if new_name == NULL, the section is removed instead */
int git_config_rename_section_in_file(const char *config_filename,
const char *old_name, const char *new_name)
{
int ret = 0, remove = 0;
char *filename_buf = NULL;
- struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
+ struct lock_file *lock;
int out_fd;
char buf[1024];
FILE *config_file;
+ if (new_name && !section_name_is_ok(new_name)) {
+ ret = error("invalid section name: %s", new_name);
+ goto out;
+ }
+
if (!config_filename)
config_filename = filename_buf = git_pathdup("config");
+ lock = xcalloc(sizeof(struct lock_file), 1);
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
if (out_fd < 0) {
ret = error("could not lock config file %s", config_filename);