Skip to contentSkip to conversation

git-config does not check validity of variable names

PATCH In review Libor Pechacek 5 participants 35 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

Hello,

I've noticed that git-config accepts variable names in the form "a=b" for its "get" operation. That means "git config a=b" does not write anything to its output and exists with status 1.

According to the man page only alphanumeric characters and - are allowed in variable names. Would it make sense to spit out an error message when the user supplies an invalid variable name like the above?

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#1 lore raw
Quoted 7 lines · Libor Pechacek

I've noticed that git-config accepts variable names in the form "a=b" for its "get" operation. That means "git config a=b" does not write anything to its output and exists with status 1.

According to the man page only alphanumeric characters and - are allowed in variable names. Would it make sense to spit out an error message when the user supplies an invalid variable name like the above?

Probably. The current behavior isn't all that terrible, in that it simply tries to look up the key, which of course doesn't exist (because it cannot syntactically), and does signal an error (with the exit code). So it is in some ways no worse than a typo like "git config color.dif.branch". And we probably don't want to start writing to stderr in such a case, as scripts assume they can call git config to find out whether the variable is defined without having to redirect stderr.

That being said, I can see how the lack of a message could be confusing for a user who mistakenly thinks "git config color.diff.branch=red" should work. So I think a patch to make that better would get a favorable response.

Note, though, that what you wrote above is not strictly true. The manpage says variable names and section names must be alphanumeric. But subsection names can contain any character except newline. So it is valid syntactically to do:

git config color.diff=red.branch

where the subsection contains the "=". Obviously this example is nonsense, and in practice most such "a=b" forms will end up not being syntactically valid (because the = will be part of the variable name, not the subsection). But if you are going to write a patch, you need to make sure not to accidentally disallow:

git config 'diff.my custom diff driver.command'

-Peff

#2 lore raw

Hi Jeff,

Quoted 10 lines · Jeff King
Quoted 7 lines · Libor Pechacek

I've noticed that git-config accepts variable names in the form "a=b" for its "get" operation. That means "git config a=b" does not write anything to its output and exists with status 1.

According to the man page only alphanumeric characters and - are allowed in variable names. Would it make sense to spit out an error message when the user supplies an invalid variable name like the above?

Quoted 7 lines

Probably. The current behavior isn't all that terrible, in that it simply tries to look up the key, which of course doesn't exist (because it cannot syntactically), and does signal an error (with the exit code). So it is in some ways no worse than a typo like "git config color.dif.branch". And we probably don't want to start writing to stderr in such a case, as scripts assume they can call git config to find out whether the variable is defined without having to redirect stderr.

I fully agree that there should be no extra output if the user searches for an undefined variable. My idea is to warn the user when the variable name is clearly invalid.

Quoted 10 lines

That being said, I can see how the lack of a message could be confusing for a user who mistakenly thinks "git config color.diff.branch=red" should work. So I think a patch to make that better would get a favorable response.

Note, though, that what you wrote above is not strictly true. The manpage says variable names and section names must be alphanumeric. But subsection names can contain any character except newline. So it is valid syntactically to do:

Quoted 6 lines

git config color.diff=red.branch

where the subsection contains the "=". Obviously this example is nonsense, and in practice most such "a=b" forms will end up not being syntactically valid (because the = will be part of the variable name, not the subsection).

That was new for me, so I had to learn the concept. Thanks for thorough explanation.

Quoted 4 lines

But if you are going to write a patch, you need to make sure not to accidentally disallow:

git config 'diff.my custom diff driver.command'

There is already a check in git_config_set_multivar to prevent the user from creating variables with invalid names. That can probably be separated and reused in the "get value" path. I'll have a look.

In the course of reading the code I've spotted another trouble related to regexps. Variable and section names are case insensitive, while subsection names are case sensitive. How can regexp matching be made "partially case sensitive"? That's a challenge.

The current approach is to lowercase everything in the pattern from the beginning until the first dot and from the last dot till the end. That has its limitations as regular expressions can be fairly complex.

Just to illustrate the situation:

$ git config --get-regexp '(CORE|USER)\.'
user.email [email protected]
core.repositoryformatversion 0
core.filemode true
$ git config --get-regexp '(COR.|USER)\.'
core.repositoryformatversion 0
core.filemode true

Currently have no idea how to fix that apart from fixing the documentation. :)

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#3 lore raw

PATCH 1/1 Sanity-ckeck config variable names

Sanity-ckeck config variable names when adding and retrieving them.

As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

The regular expression patterns are left intact when using --get-regexp option.

---
 builtin/config.c |    8 ++---
 cache.h          |    1 +
 config.c         |  106 ++++++++++++++++++++++++++++++++++--------------------
 3 files changed, 71 insertions(+), 44 deletions(-)
builtin/config.c +3 -5
index ca4a0db..068ef76 100644
@@ -153,7 +153,6 @@ static int show_config(const char *key_, const char *value_, void *cb)
 static int get_value(const char *key_, const char *regex_)
 {
 	int ret = -1;
-	char *tl;
 	char *global = NULL, *repo_config = NULL;
 	const char *system_wide = NULL, *local;
 
@@ -168,10 +167,6 @@ static int get_value(const char *key_, const char *regex_)
 	}
 
 	key = xstrdup(key_);
-	for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-		*tl = tolower(*tl);
-	for (tl=key; *tl && *tl != '.'; ++tl)
-		*tl = tolower(*tl);
 
 	if (use_key_regexp) {
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
@@ -179,6 +174,9 @@ static int get_value(const char *key_, const char *regex_)
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;
 		}
+	} else {
+		if (git_config_parse_key(key_, &key, NULL))
+			goto free_strings;
 	}
 
 	if (regex_) {
cache.h +1 -0
index d83d68c..1e32d63 100644
@@ -997,6 +997,7 @@ extern int git_config_maybe_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
+extern int git_config_parse_key(const char *, char **, int *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
 extern const char *git_etc_gitconfig(void);
config.c +67 -39

132 lines not on this page · raw message

Signature · 1 line
1.7.4.rc2.2.gb4f4f
#4 lore raw

Re: [PATCH] Sanity-ckeck config variable names

Quoted 5 lines · Libor Pechacek

Sanity-ckeck config variable names when adding and retrieving them.

As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

I think this is a good goal, but a few nits:

Quoted 5 lines

+/* Auxiliary function to sanity-check and split the key into the section
+ * identifier and variable name.
+ *
+ * Returns 0 on success, 1 when there is an invalid character in the key and 2
+ * if there is no section name in the key.

Please switch these to -1 and -2, as we generally use negative integers to indicate errors in library-ish function. I know you were just copying git_config_set_multivar's error codes, but it is designed to return straight to exit(), which makes it an exception.

Other than that, the code looks OK to me. However, it does cause t1300.85 to fail. The problem is that the test is using these bogus names to check that "git -c" works. While it does technically work now to say "git -c foo=bar config foo" (which your patch breaks), I don't think that is a useful behavior in the real world, since no actual config options exist without a section name. So yes, you can "git -c" a non-sectioned variable, but why would you want to?

So I think it probably makes sense to squash this in:

t/t1300-repo-config.sh +3 -4
index d0e5546..3e79c37 100755
@@ -876,11 +876,10 @@ test_expect_success 'check split_cmdline return' "
 	"
 
 test_expect_success 'git -c "key=value" support' '
-	test "z$(git -c name=value config name)" = zvalue &&
 	test "z$(git -c core.name=value config core.name)" = zvalue &&
-	test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
-	test "z$(git -c flag config --bool flag)" = ztrue &&
-	test_must_fail git -c core.name=value config name
+	test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
+	test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
+	test_must_fail git -c name=value config core.name
 '
 
 test_done

and a note to the commit message like:

This breaks a test in t1300 which used invalid section-less keys in the tests for "git -c". However, allowing such names there was useless, since there was no way to set them via config file, and no part of git actually tried to use section-less keys. This patch updates the test to use more realistic examples.

-Peff

#5 lore raw

Re: [PATCH] Sanity-ckeck config variable names

Quoted 1 line · Jeff King

Other than that, the code looks OK to me.

Actually, I take this back.

Doesn't this hunk:

Review static int get_value(const char *key_, const char *regex_) · line 174

Quoted 8 lines
(quoted diff) +0 -4
@@ -168,10 +167,6 @@ static int get_value(const char *key_, const char *regex_)
      }
 
      key = xstrdup(key_);
-     for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-             *tl = tolower(*tl);
-     for (tl=key; *tl && *tl != '.'; ++tl)
-             *tl = tolower(*tl);

Mean that regexp keys no longer get downcased properly? I.e.,

  git config Foo.value true
  git config --get-regexp 'foo.*'
  git config --get-regexp 'Foo.*'

used to work for both lookups, but now fails for the second one?

The problem is that your git_config_parse_key handles the downcasing for the non-regexp case, but it is not called (for obvious reasons) in the regexp case.

-Peff

#6 lore raw

Re: [PATCH] Sanity-ckeck config variable names

Quoted 5 lines · Jeff King
Quoted 2 lines

+ * Returns 0 on success, 1 when there is an invalid character in the key and 2
+ * if there is no section name in the key.

Please switch these to -1 and -2, as we generally use negative integers to indicate errors in library-ish function.

Done.

Quoted 2 lines

Other than that, the code looks OK to me. However, it does cause t1300.85 to fail.

Thanks for catching it, added your changes to the patch and tested. I didn't notice the test suite. :)

Quoted 13 lines · Jeff King

Doesn't this hunk:

Review static int get_value(const char *key_, const char *regex_) · line 174

Quoted 8 lines
(quoted diff) +0 -4
@@ -168,10 +167,6 @@ static int get_value(const char *key_, const char *regex_)
      }
 
      key = xstrdup(key_);
-     for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-             *tl = tolower(*tl);
-     for (tl=key; *tl && *tl != '.'; ++tl)
-             *tl = tolower(*tl);

Mean that regexp keys no longer get downcased properly? I.e.,

Quoted 5 lines
  git config Foo.value true
  git config --get-regexp 'foo.*'
  git config --get-regexp 'Foo.*'

used to work for both lookups, but now fails for the second one?

After thinking about it I added the code back. More on it in the "documentation fix" thread.

Thanks for your kind guidance.

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#7 lore raw

PATCH v2 1/1 Sanity-ckeck config variable names

Sanity-ckeck config variable names when adding and retrieving them. As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

This breaks a test in t1300 which used invalid section-less keys in the tests for "git -c". However, allowing such names there was useless, since there was no way to set them via config file, and no part of git actually tried to use section-less keys. This patch updates the test to use more realistic examples.

---
 builtin/config.c       |   17 +++++--
 cache.h                |    1 +
 config.c               |  107 ++++++++++++++++++++++++++++++-----------------
 t/t1300-repo-config.sh |    7 +--
 4 files changed, 84 insertions(+), 48 deletions(-)
builtin/config.c +12 -5
index ca4a0db..ba614e7 100644
@@ -153,7 +153,6 @@ static int show_config(const char *key_, const char *value_, void *cb)
 static int get_value(const char *key_, const char *regex_)
 {
 	int ret = -1;
-	char *tl;
 	char *global = NULL, *repo_config = NULL;
 	const char *system_wide = NULL, *local;
 
@@ -168,17 +167,25 @@ static int get_value(const char *key_, const char *regex_)
 	}
 
 	key = xstrdup(key_);
-	for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-		*tl = tolower(*tl);
-	for (tl=key; *tl && *tl != '.'; ++tl)
-		*tl = tolower(*tl);
 
 	if (use_key_regexp) {
+		char *tl;
+
+		/* TODO - this naive pattern lowercasing obviously does not
+		 * work for more complex patterns like `^[^.]*Foo.*bar' */
+		for (tl = key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
+			*tl = tolower(*tl);
+		for (tl = key; *tl && *tl != '.'; ++tl)
+			*tl = tolower(*tl);
+
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;
 		}
+	} else {
+		if (git_config_parse_key(key_, &key, NULL))
+			goto free_strings;
 	}
 
 	if (regex_) {
cache.h +1 -0
index d83d68c..1e32d63 100644
@@ -997,6 +997,7 @@ extern int git_config_maybe_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
+extern int git_config_parse_key(const char *, char **, int *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
 extern const char *git_etc_gitconfig(void);
config.c +68 -39

133 lines not on this page · raw message

t/t1300-repo-config.sh +3 -4
index d0e5546..3e79c37 100755
@@ -876,11 +876,10 @@ test_expect_success 'check split_cmdline return' "
 	"
 
 test_expect_success 'git -c "key=value" support' '
-	test "z$(git -c name=value config name)" = zvalue &&
 	test "z$(git -c core.name=value config core.name)" = zvalue &&
-	test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
-	test "z$(git -c flag config --bool flag)" = ztrue &&
-	test_must_fail git -c core.name=value config name
+	test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
+	test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
+	test_must_fail git -c name=value config core.name
 '
 
 test_done
Signature · 1 line
1.7.4.rc2.20.gdb1b81
Acked-by on v2 1/1
#8 lore raw

Re: [PATCH v2] Sanity-ckeck config variable names

Quoted 1 line · Libor Pechacek

Sanity-ckeck config variable names when adding and retrieving them. As a side

Typo: s/ckeck/check/

Other than that, this version looks good to me.

-Peff

#9 lore raw

PATCH v3 1/1 Sanity-check config variable names

Sanity-check config variable names when adding and retrieving them. As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

This breaks a test in t1300 which used invalid section-less keys in the tests for "git -c". However, allowing such names there was useless, since there was no way to set them via config file, and no part of git actually tried to use section-less keys. This patch updates the test to use more realistic examples.

---

Quoted 3 lines · Jeff King

Typo: s/ckeck/check/

Other than that, this version looks good to me.

Fixed the typo and return values from get_value and git_config_set_multivar. We have changed git_config_parse_key to return negative values on error, but forgot to negate the numbers when returning them as exit codes. That led to process exit codes 255 and 254, which is, I think, unwanted.

Quoted 1 line

Thanks. Now I consider this patch truly final.

Libor

 builtin/config.c       |   18 ++++++--
 cache.h                |    1 +
 config.c               |  107 ++++++++++++++++++++++++++++++-----------------
 t/t1300-repo-config.sh |    7 +--
 4 files changed, 85 insertions(+), 48 deletions(-)
builtin/config.c +13 -5
index ca4a0db..66c0586 100644
@@ -153,7 +153,6 @@ static int show_config(const char *key_, const char *value_, void *cb)
 static int get_value(const char *key_, const char *regex_)
 {
 	int ret = -1;
-	char *tl;
 	char *global = NULL, *repo_config = NULL;
 	const char *system_wide = NULL, *local;
 
@@ -168,17 +167,26 @@ static int get_value(const char *key_, const char *regex_)
 	}
 
 	key = xstrdup(key_);
-	for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-		*tl = tolower(*tl);
-	for (tl=key; *tl && *tl != '.'; ++tl)
-		*tl = tolower(*tl);
 
 	if (use_key_regexp) {
+		char *tl;
+
+		/* TODO - this naive pattern lowercasing obviously does not
+		 * work for more complex patterns like `^[^.]*Foo.*bar' */
+		for (tl = key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
+			*tl = tolower(*tl);
+		for (tl = key; *tl && *tl != '.'; ++tl)
+			*tl = tolower(*tl);
+
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;
 		}
+	} else {
+		ret = -git_config_parse_key(key_, &key, NULL);
+		if (ret)
+			goto free_strings;
 	}
 
 	if (regex_) {
cache.h +1 -0
index d83d68c..1e32d63 100644
@@ -997,6 +997,7 @@ extern int git_config_maybe_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
+extern int git_config_parse_key(const char *, char **, int *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
 extern const char *git_etc_gitconfig(void);
config.c +68 -39

133 lines not on this page · raw message

t/t1300-repo-config.sh +3 -4
index d0e5546..3e79c37 100755
@@ -876,11 +876,10 @@ test_expect_success 'check split_cmdline return' "
 	"
 
 test_expect_success 'git -c "key=value" support' '
-	test "z$(git -c name=value config name)" = zvalue &&
 	test "z$(git -c core.name=value config core.name)" = zvalue &&
-	test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
-	test "z$(git -c flag config --bool flag)" = ztrue &&
-	test_must_fail git -c core.name=value config name
+	test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
+	test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
+	test_must_fail git -c name=value config core.name
 '
 
 test_done
Signature · 1 line
1.7.4.rc3.2.g27487
#10 lore raw

Re: [PATCH v3] Sanity-check config variable names

Quoted 14 lines · Libor Pechacek

Sanity-check config variable names when adding and retrieving them. As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

This breaks a test in t1300 which used invalid section-less keys in the tests for "git -c". However, allowing such names there was useless, since there was no way to set them via config file, and no part of git actually tried to use section-less keys. This patch updates the test to use more realistic examples.

---

Quoted 8 lines
Quoted 3 lines · Jeff King

Typo: s/ckeck/check/

Other than that, this version looks good to me.

Fixed the typo and return values from get_value and git_config_set_multivar. We have changed git_config_parse_key to return negative values on error, but forgot to negate the numbers when returning them as exit codes.

Earlier get_value() returned:

-1: when it saw an uncompilable regexp (either in key or value);
0: when a value was available (under --all) or unique (without --all); 1: when the requested variable is missing; and
2: when the requested variable is multivalued under --all.

As the new error condition you are adding is to detect and signal a broken input, doesn't it fall into the same category as "broken regexp", which in turn would mean that it should return the same -1?

Or to distinguish between "invalid character in the key" and "no section name in the key", you might want to add -2 to the mix, but personally I don't think it is worth it. The advantage of being able to tell between the two kinds of breakage feels much smaller than the cost of having to worry about breaking existing callers that try to catch broken user input by checking the return value from get_value() explicitly against -1, not just any negative value.

Note that I am not suggesting to change the return value from config-parse-key in the above; I am only talking about get_value().

builtin/config.c static int get_value(const char *key_, const char *regex_) · line 179

Quoted 9 lines
builtin/config.c +8 -0
 	if (use_key_regexp) {
+		char *tl;
+
+		/* TODO - this naive pattern lowercasing obviously does not
+		 * work for more complex patterns like `^[^.]*Foo.*bar' */
+		for (tl = key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
+			*tl = tolower(*tl);
+		for (tl = key; *tl && *tl != '.'; ++tl)
+			*tl = tolower(*tl);

When moving an existing code segment around like this, I would not mind to see style fixes thrown in to the patch, as long as the moved code is small enough. Perhaps like this:

	/*
         * NEEDSWORK: this naive pattern lowercasing obviously does
	 * not work for more complex patterns like "^[^.]*Foo.*bar".
	 * Perhaps we should deprecate this altogether someday.
         */
	for (tl = key + strlen(key) - 1;
	     tl >= key && *tl != '.';
	     tl--)
		*tl = tolower(*tl);
	for (tl = key; *tl && *tl != '.'; tl++)
		*tl = tolower(*tl);

The style rules applied above are...

- We want to see SP around binary operators;

- However, we don't want to see a line that is too long;

- When there is no reason to choose between (pre/post)-(in/de)crement, post-(in/de)crement is easier to read.

config.c int git_config_set(const char *key, const char *value) · line 1101

Quoted 9 lines
config.c +1 -0
index 625e051..c976544 100644
@@ -1098,6 +1098,72 @@ int git_config_set(const char *key, const char *value)
 	return git_config_set_multivar(key, value, NULL, 0);
 }
 
+/* Auxiliary function to sanity-check and split the key into the section
	/*
         * Style. We write our multi-line comments
	 * like this.
         */
Quoted 14 lines
config.c +14 -0
+int git_config_parse_key(const char *key, char **store_key, int *baselen_)
+{
+	int i, dot, baselen;
+	const char *last_dot = strrchr(key, '.');
+
+	/*
+	 * Since "key" actually contains the section name and the real
+	 * key name separated by a dot, we have to know where the dot is.
+	 */
+
+	if (last_dot == NULL) {
+		error("key does not contain a section: %s", key);
+		return -2;
+	}

config.c int git_config_set(const char *key, const char *value) · line 1135

Quoted 10 lines
config.c +10 -0
+
+	baselen = last_dot - key;
+	if (baselen_)
+		*baselen_ = baselen;
+
+	/*
+	 * Validate the key and while at it, lower case it for matching.
+	 */
+	if (store_key)
+		*store_key = xmalloc(strlen(key) + 1);

Does it make sense for this function to be prepared to get called with NULL in store_key like this (and in the remainder of your patch)?

The primary reason somebody calls this function is so that the caller can then use the resulting lowercased form for matching, but "if store-key is not NULL" feels as if this code is optimized for a special case caller that only wants to validate without using the resulting lowercased key for matching, which does not exist yet.

Quoted 7 lines
t/t1300-repo-config.sh +0 -0
index d0e5546..3e79c37 100755
@@ -876,11 +876,10 @@ test_expect_success 'check split_cmdline return' "
 	"
 

t/t1300-repo-config.sh test_expect_success 'check split_cmdline return' " · line 883

Quoted 10 lines
t/t1300-repo-config.sh +3 -4
 test_expect_success 'git -c "key=value" support' '
-	test "z$(git -c name=value config name)" = zvalue &&
 	test "z$(git -c core.name=value config core.name)" = zvalue &&
-	test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
-	test "z$(git -c flag config --bool flag)" = ztrue &&
-	test_must_fail git -c core.name=value config name
+	test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
+	test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
+	test_must_fail git -c name=value config core.name
 '

Don't you want to make sure that your sanity check triggers in new tests?

#11 lore raw

Re: [PATCH v3] Sanity-check config variable names

Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Libor Pechacek

Fixed the typo and return values from get_value and git_config_set_multivar. We have changed git_config_parse_key to return negative values on error, but forgot to negate the numbers when returning them as exit codes.

Earlier get_value() returned:

-1: when it saw an uncompilable regexp (either in key or value);
0: when a value was available (under --all) or unique (without --all); 1: when the requested variable is missing; and
2: when the requested variable is multivalued under --all.

Fixed one part with the last change and broke the other one. Thanks for catching it. The same return value for "invalid key" and "invalid regex" is OK for me.

BTW is it OK to exit(-1);? The return value of get_value() gets propagated to the process exit status. At the same time shell uses values >128 to indicate that the process was terminated by a signal.

[...]

Quoted 3 lines

When moving an existing code segment around like this, I would not mind to see style fixes thrown in to the patch, as long as the moved code is small enough. Perhaps like this:

I've added the style fix into the patch.

[...]

Quoted 6 lines
Quoted 1 line

+/* Auxiliary function to sanity-check and split the key into the section

	/*
       * Style. We write our multi-line comments
	 * like this.
       */

Fixed.

Quoted 1 line
Quoted 1 line

+int git_config_parse_key(const char *key, char **store_key, int *baselen_)

[...]

Quoted 3 lines

Does it make sense for this function to be prepared to get called with NULL in store_key like this (and in the remainder of your patch)?

No, I wrote it unnecessarily generic. Removed the excess code. Thanks for pointing it out.

[...]

Quoted 4 lines

t/t1300-repo-config.sh test_expect_success 'check split_cmdline return' " · line 883

Quoted 2 lines
t/t1300-repo-config.sh +1 -0
+	test_must_fail git -c name=value config core.name
 '

Don't you want to make sure that your sanity check triggers in new tests?

Added a few tests after getting familiar with the test suite.

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#12 lore raw

PATCH v4 1/1 Sanity-check config variable names

Sanity-check config variable names when adding and retrieving them. As a side effect code duplication between git_config_set_multivar and get_value (in builtin/config.c) was removed and the common functionality was placed in git_config_parse_key.

This breaks a test in t1300 which used invalid section-less keys in the tests for "git -c". However, allowing such names there was useless, since there was no way to set them via config file, and no part of git actually tried to use section-less keys. This patch updates the test to use more realistic examples as well as adding its own test.

---
 builtin/config.c       |   22 ++++++++--
 cache.h                |    1 +
 config.c               |  104 ++++++++++++++++++++++++++++++------------------
 t/t1300-repo-config.sh |   18 ++++++--
 4 files changed, 97 insertions(+), 48 deletions(-)
builtin/config.c +17 -5
index ca4a0db..dd17029 100644
@@ -153,7 +153,6 @@ static int show_config(const char *key_, const char *value_, void *cb)
 static int get_value(const char *key_, const char *regex_)
 {
 	int ret = -1;
-	char *tl;
 	char *global = NULL, *repo_config = NULL;
 	const char *system_wide = NULL, *local;
 
@@ -168,17 +167,30 @@ static int get_value(const char *key_, const char *regex_)
 	}
 
 	key = xstrdup(key_);
-	for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
-		*tl = tolower(*tl);
-	for (tl=key; *tl && *tl != '.'; ++tl)
-		*tl = tolower(*tl);
 
 	if (use_key_regexp) {
+		char *tl;
+
+		/*
+		 * NEEDSWORK: this naive pattern lowercasing obviously does not
+		 * work for more complex patterns like "^[^.]*Foo.*bar".
+		 * Perhaps we should deprecate this altogether someday.
+		 */
+		for (tl = key + strlen(key) - 1;
+		     tl >= key && *tl != '.';
+		     tl--)
+			*tl = tolower(*tl);
+		for (tl = key; *tl && *tl != '.'; tl++)
+			*tl = tolower(*tl);
+
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;
 		}
+	} else {
+		if (git_config_parse_key(key_, &key, NULL))
+			goto free_strings;
 	}
 
 	if (regex_) {
cache.h +1 -0
index d83d68c..1e32d63 100644
@@ -997,6 +997,7 @@ extern int git_config_maybe_bool(const char *, const char *);
 extern int git_config_string(const char **, const char *, const char *);
 extern int git_config_pathname(const char **, const char *, const char *);
 extern int git_config_set(const char *, const char *);
+extern int git_config_parse_key(const char *, char **, int *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
 extern int git_config_rename_section(const char *, const char *);
 extern const char *git_etc_gitconfig(void);
config.c +65 -39

130 lines not on this page · raw message

t/t1300-repo-config.sh +14 -4
index d0e5546..c3d91d1 100755
@@ -876,11 +876,21 @@ test_expect_success 'check split_cmdline return' "
 	"
 
 test_expect_success 'git -c "key=value" support' '
-	test "z$(git -c name=value config name)" = zvalue &&
 	test "z$(git -c core.name=value config core.name)" = zvalue &&
-	test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
-	test "z$(git -c flag config --bool flag)" = ztrue &&
-	test_must_fail git -c core.name=value config name
+	test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
+	test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
+	test_must_fail git -c name=value config core.name
+'
+
+test_expect_success 'key sanity-checking' '
+	test_must_fail git config foo=bar &&
+	test_must_fail git config foo=.bar &&
+	test_must_fail git config foo.ba=r &&
+	test_must_fail git config foo.1bar &&
+	test_must_fail git config foo."ba
+				z".bar &&
+	git config foo.bar true &&
+	git config foo."ba =z".bar false
 '
 
 test_done
Signature · 1 line
1.7.4.rc3.10.gf91c9
#13 lore raw

Re: [PATCH v4] Sanity-check config variable names

builtin/config.c static int get_value(const char *key_, const char *regex_)

Quoted 10 lines · Libor Pechacek
builtin/config.c +0 -0
index ca4a0db..dd17029 100644
@@ -168,17 +167,30 @@ static int get_value(const char *key_, const char *regex_)
...
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;

This is not a new issue introduced by this series, but isn't this goto leaking "key" by jumping over free(key) later in the function but before its target?

builtin/config.c

Quoted 4 lines
builtin/config.c +3 -0
 		}
+	} else {
+		if (git_config_parse_key(key_, &key, NULL))
+			goto free_strings;

This seemingly has the same issue but is worse than that. You allocate and overwrite "key" in git_config_parse_key(), so by calling the function after allocating key in the caller, you immediately leak it. The new copy allocated inside the callee is freed at its end upon error return, so jumping over free(key) in the caller does not leak, though.

builtin/config.c int git_config_set(const char *key, const char *value)

Quoted 5 lines
builtin/config.c +2 -0
@@ -1124,59 +1187,22 @@ int git_config_set(const char *key, const char *value)
...
+	ret = -git_config_parse_key(key, &store.key, &store.baselen);
+	if (ret)
 		goto out_free;

This '-' is very easy to miss; I'd rather see it spelled out with an explanation.

But looking at the bigger picture, don't you think that an internal function like git_config_set() should return negative on error, and we should make it the caller's responsibility to turn it to a value suitable for feeding exit(3)? It obviously is a separate topic.

Here is a minimum fix-up on top of your patch.
Thanks.

builtin/config.c +3 -2
index dd17029..6754da8 100644
@@ -166,8 +166,6 @@ static int get_value(const char *key_, const char *regex_)
 			system_wide = git_etc_gitconfig();
 	}
 
-	key = xstrdup(key_);
-
 	if (use_key_regexp) {
 		char *tl;
 
@@ -176,6 +174,8 @@ static int get_value(const char *key_, const char *regex_)
 		 * work for more complex patterns like "^[^.]*Foo.*bar".
 		 * Perhaps we should deprecate this altogether someday.
 		 */
+
+		key = xstrdup(key_);
 		for (tl = key + strlen(key) - 1;
 		     tl >= key && *tl != '.';
 		     tl--)
@@ -186,6 +186,7 @@ static int get_value(const char *key_, const char *regex_)
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
+			free(key);
 			goto free_strings;
 		}
 	} else {
config.c +4 -2
index fde91f5..f758734 100644
@@ -1141,7 +1141,8 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 			dot = 1;
 		/* Leave the extended basename untouched.. */
 		if (!dot || i > baselen) {
-			if (!iskeychar(c) || (i == baselen+1 && !isalpha(c))) {
+			if (!iskeychar(c) ||
+			    (i == baselen + 1 && !isalpha(c))) {
 				error("invalid key: %s", key);
 				goto out_free_ret_1;
 			}
@@ -1197,7 +1198,8 @@ int git_config_set_multivar(const char *key, const char *value,
 	else
 		config_filename = git_pathdup("config");
 
-	ret = -git_config_parse_key(key, &store.key, &store.baselen);
+	/* parse-key returns negative; flip the sign to feed exit(3) */
+	ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
 	if (ret)
 		goto out_free;
 
#14 lore raw

Re: [PATCH v4] Sanity-check config variable names

Quoted 16 lines · Junio C Hamano

builtin/config.c static int get_value(const char *key_, const char *regex_) · line 190

Quoted 10 lines · Libor Pechacek
builtin/config.c +0 -0
index ca4a0db..dd17029 100644
@@ -168,17 +167,30 @@ static int get_value(const char *key_, const char *regex_)
...
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			fprintf(stderr, "Invalid key pattern: %s\n", key_);
 			goto free_strings;

This is not a new issue introduced by this series, but isn't this goto leaking "key" by jumping over free(key) later in the function but before its target?

Yes, sure and not only there. A few lines below there is the compilation of the value regex. Freeing the memory allocated for key storage obviously needs a little bit more work and I'd like to post the fix as a separate patch.

Quoted 10 lines

builtin/config.c

Quoted 4 lines
builtin/config.c +3 -0
 		}
+	} else {
+		if (git_config_parse_key(key_, &key, NULL))
+			goto free_strings;

This seemingly has the same issue but is worse than that. You allocate and overwrite "key" in git_config_parse_key(), so by calling the function after allocating key in the caller, you immediately leak it. The new copy allocated inside the callee is freed at its end upon error return, so jumping over free(key) in the caller does not leak, though.

Negligence on my part, thanks for catching it.

Quoted 8 lines

builtin/config.c int git_config_set(const char *key, const char *value)

Quoted 5 lines
builtin/config.c +2 -0
@@ -1124,59 +1187,22 @@ int git_config_set(const char *key, const char *value)
...
+	ret = -git_config_parse_key(key, &store.key, &store.baselen);
+	if (ret)
 		goto out_free;

This '-' is very easy to miss; I'd rather see it spelled out with an explanation.

Agree. It would be yet better to get rid this weird transformation at all.

Quoted 4 lines

But looking at the bigger picture, don't you think that an internal function like git_config_set() should return negative on error, and we should make it the caller's responsibility to turn it to a value suitable for feeding exit(3)? It obviously is a separate topic.

Agree and not only that. The exit codes are inconsistent between "set" and "get" operation[1]. IMHO it's also questionable to feed exit(3) with negative values.

Would you be in favor of getting the exit codes consistent and documented?

Quoted 1 line

Here is a minimum fix-up on top of your patch.

Squashed into my patch, with the exception of the memleak fix, which I'm going to post separately.

Quoted 1 line

Thanks.

Thanks for the opportunity to contribute. :)

Libor

[1] $ ./git-config x.x x [; echo $?

    error: invalid pattern: [
    6
    $ ./git-config --get x.x [; echo $?
    Invalid pattern: [
    255
Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#15 lore raw

PATCH 1/1 Documentation fixes in git-config

In reply to Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching is case sensitive.

---
 Documentation/config.txt     |   12 +++++++-----
 Documentation/git-config.txt |    4 +++-
 2 files changed, 10 insertions(+), 6 deletions(-)
Documentation/config.txt +7 -5
index ff7c225..0f23bc7 100644
@@ -12,8 +12,9 @@ The configuration variables are used by both the git plumbing
 and the porcelains. The variables are divided into sections, wherein
 the fully qualified variable name of the variable itself is the last
 dot-separated segment and the section name is everything before the last
-dot. The variable names are case-insensitive and only alphanumeric
-characters are allowed. Some variables may appear multiple times.
+dot. The variable names are case-insensitive, only alphanumeric
+characters and '-' are allowed and must start with an alphabetic character.
+Some variables may appear multiple times.
 
 Syntax
 ~~~~~~
@@ -53,9 +54,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, only alphanumeric
+characters and `-` are allowed and must start with an alphabetic character.
+There can be more than one value for a given variable; we say then that
+variable is multivalued.
 
 Leading and trailing whitespace in a variable value is discarded.
 Internal whitespace within a variable value is retained verbatim.
Documentation/git-config.txt +3 -1
index 543dd64..6966ed6 100644
@@ -84,7 +84,9 @@ OPTIONS
 
 --get-regexp::
 	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Regular expression matching is case sensitive in all parts of the key,
+	therefore make sure your pattern matches lower case letters in section
+	and variable names.  Also outputs the key names.
 
 --global::
 	For writing options: write to global ~/.gitconfig file rather than
Signature · 1 line
1.7.4.rc2.2.gb4f4f
#16 lore raw

Re: [PATCH] Documentation fixes in git-config

Documentation/config.txt The configuration variables are used by both the git plumbing · line 17

Quoted 13 lines · Libor Pechacek
Documentation/config.txt +3 -2
index ff7c225..0f23bc7 100644
@@ -12,8 +12,9 @@ The configuration variables are used by both the git plumbing
 and the porcelains. The variables are divided into sections, wherein
 the fully qualified variable name of the variable itself is the last
 dot-separated segment and the section name is everything before the last
-dot. The variable names are case-insensitive and only alphanumeric
-characters are allowed. Some variables may appear multiple times.
+dot. The variable names are case-insensitive, only alphanumeric
+characters and '-' are allowed and must start with an alphabetic character.
+Some variables may appear multiple times.

The intent of the change looks fine, but your sentence doesn't quite parse to me (to be fair, the problem is in the one you are replacing, but adding the third clause makes it even more confusing). How about:

The variables names are case-insensitive, allow only alphanumeric characters and '-', and must start with an alphabetic character.

Documentation/config.txt The configuration variables are used by both the git plumbing

Quoted 6 lines
Documentation/config.txt +3 -1
 --get-regexp::
 	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Regular expression matching is case sensitive in all parts of the key,
+	therefore make sure your pattern matches lower case letters in section
+	and variable names.  Also outputs the key names.

That is only true because of the breakage in your first patch. Without your patch, both of these work:

  git config --get-regexp 'Foo.*'
  git config --get-regexp 'foo.*'

That being said, the downcasing is extremely naive for regexps, and you should try to match the canonical name. The current downcasing behavior should probably stay for historical reasons, but is not well thought-out (it may even be accidental). Perhaps we should therefore explain it in those terms:

Regular expression matching is case-sensitive and done against a canonicalized version of the key in which section and variable names are lowercased, but subsection names are not. For historical reasons, some simple regular expressions are lower-cased before matching (everything before the first dot and after the last dot), which makes things like "Core.*' work.

I dunno. Maybe we should just declare "Core.*' to be broken, and anybody who was relying on it is wrong.

-Peff

#17 lore raw

Re: [PATCH] Documentation fixes in git-config

Quoted 6 lines · Jeff King

The intent of the change looks fine, but your sentence doesn't quite parse to me (to be fair, the problem is in the one you are replacing, but adding the third clause makes it even more confusing). How about:

The variables names are case-insensitive, allow only alphanumeric characters and '-', and must start with an alphabetic character.

I like that very much. The original sentence sounded a little bit artificial to me, and after my amendments it felt like a collapsed list of items. This is far more natural, thanks for help.

Quoted 26 lines

Documentation/git-config.txt OPTIONS · line 89

Quoted 6 lines

--get-regexp::

Documentation/git-config.txt +3 -1
 	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Regular expression matching is case sensitive in all parts of the key,
+	therefore make sure your pattern matches lower case letters in section
+	and variable names.  Also outputs the key names.

That is only true because of the breakage in your first patch. Without your patch, both of these work:

  git config --get-regexp 'Foo.*'
  git config --get-regexp 'foo.*'

That being said, the downcasing is extremely naive for regexps, and you should try to match the canonical name. The current downcasing behavior should probably stay for historical reasons, but is not well thought-out (it may even be accidental). Perhaps we should therefore explain it in those terms:

Regular expression matching is case-sensitive and done against a canonicalized version of the key in which section and variable names are lowercased, but subsection names are not. For historical reasons, some simple regular expressions are lower-cased before matching (everything before the first dot and after the last dot), which makes things like "Core.*' work.

Quoted 2 lines

I dunno. Maybe we should just declare "Core.*' to be broken, and anybody who was relying on it is wrong.

After thinking about it more, I realized that the goal is to have a "mixed case-sensitivity" regexp matching. The case sensitivity if of course determined by the part of the key which is being processed, and not the pattern itself. That probably means changes in the regexp mathing sengine.

Declaring patterns like 'Core.*' invalid would be a step back in my opinion.

Therefore I've added the regexp lowercasing code back and documented the limitation. I believe it's fair to the users.

Updated patches will follow in a while.

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#18 lore raw

PATCH v2 1/1 Documentation fixes in git-config

Variable names must start with an alphabetic character, regexp config key matching has its limits.

---
 Documentation/config.txt     |   12 +++++++-----
 Documentation/git-config.txt |    9 +++++++--
 2 files changed, 14 insertions(+), 7 deletions(-)
Documentation/config.txt +7 -5
index ff7c225..928ceda 100644
@@ -12,8 +12,9 @@ The configuration variables are used by both the git plumbing
 and the porcelains. The variables are divided into sections, wherein
 the fully qualified variable name of the variable itself is the last
 dot-separated segment and the section name is everything before the last
-dot. The variable names are case-insensitive and only alphanumeric
-characters are allowed. Some variables may appear multiple times.
+dot. The variable names are case-insensitive, allow only alphanumeric
+characters and '-', and must start with an alphabetic character.  Some
+variables may appear multiple times.
 
 Syntax
 ~~~~~~
@@ -53,9 +54,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, allow only alphanumeric characters
+and `-`, and must start with an alphabetic character.  There can be more
+than one value for a given variable; we say then that variable is
+multivalued.
 
 Leading and trailing whitespace in a variable value is discarded.
 Internal whitespace within a variable value is retained verbatim.
Documentation/git-config.txt +7 -2
index 543dd64..31f4658 100644
@@ -83,8 +83,13 @@ OPTIONS
 	is not exactly one.
 
 --get-regexp::
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.  Regular expressions are partially lower-cased
+	before matching (everything before the first dot and after the last
+	dot), which makes things like "Core.*' work.
 
 --global::
 	For writing options: write to global ~/.gitconfig file rather than
Signature · 1 line
1.7.4.rc2.20.gdb1b81
Acked-by on v2 1/1
#19 lore raw

Re: [PATCH v2] Documentation fixes in git-config

Quoted 2 lines · Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching has its limits.

I think this is fine, although:

Documentation/git-config.txt OPTIONS · line 92

Quoted 10 lines

--get-regexp::

Documentation/git-config.txt +7 -2
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.  Regular expressions are partially lower-cased
+	before matching (everything before the first dot and after the last
+	dot), which makes things like "Core.*' work.

I am half-tempted to mark the lowercasing of the regex as deprecated (or at least discouraged). It's such a hack, and I don't think we will ever improve to make it work in the general case, as regexes are simply too complex for us to handle all possible inputs.

Either way, though, this is definitely an improvement over what is there, so:

-Peff

#20 lore raw

Re: [PATCH v2] Documentation fixes in git-config

Quoted 2 lines · Jeff King

I am half-tempted to mark the lowercasing of the regex as deprecated (or at least discouraged).

That's actually side effect of
http://git.kernel.org/?p=git/git.git;a=commitdiff;h=2fa9a0fb31cbf01e8318a02c3e222d7fd3fd0a83
Don't see any intent to support "mixed-sensitivity" matching in it.

Quoted 3 lines

It's such a hack, and I don't think we will ever improve to make it work in the general case, as regexes are simply too complex for us to handle all possible inputs.

As far as I understand git uses host library implementation of regcomp and regexec so we cannot fix that side. Writing code to modify regexes is not worth the effort.

FWIW I'm in favor of deprecating this functionality.

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#21 lore raw

PATCH v3 1/1 Documentation fixes in git-config

In reply to Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching has its limits.

---

Hello Junio,

This patch has fallen through the cracks, therefore I re-send it. Previous discussion about this patch is at http://www.spinics.net/lists/git/msg149593.html.
The only change I've done since version 2 of this patch is replacing apostrophes with backticks in the first hunk.

Be so kind as to apply the fix.

Libor

 Documentation/config.txt     |   12 +++++++-----
 Documentation/git-config.txt |    9 +++++++--
 2 files changed, 14 insertions(+), 7 deletions(-)
Documentation/config.txt +7 -5
index e55dae1..078313e 100644
@@ -12,8 +12,9 @@ The configuration variables are used by both the git plumbing
 and the porcelains. The variables are divided into sections, wherein
 the fully qualified variable name of the variable itself is the last
 dot-separated segment and the section name is everything before the last
-dot. The variable names are case-insensitive and only alphanumeric
-characters are allowed. Some variables may appear multiple times.
+dot. The variable names are case-insensitive, allow only alphanumeric
+characters and `-`, and must start with an alphabetic character.  Some
+variables may appear multiple times.
 
 Syntax
 ~~~~~~
@@ -54,9 +55,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, allow only alphanumeric characters
+and `-`, and must start with an alphabetic character.  There can be more
+than one value for a given variable; we say then that variable is
+multivalued.
 
 Leading and trailing whitespace in a variable value is discarded.
 Internal whitespace within a variable value is retained verbatim.
Documentation/git-config.txt +7 -2
index aa8303b..a54fee8 100644
@@ -85,8 +85,13 @@ OPTIONS
 	is not exactly one.
 
 --get-regexp::
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.  Regular expressions are partially lower-cased
+	before matching (everything before the first dot and after the last
+	dot), which makes things like "Core.*' work.
 
 --global::
 	For writing options: write to global ~/.gitconfig file rather than
Signature · 1 line
1.7.9.2.324.g78dedf
#22 lore raw

Re: [PATCH v3] Documentation fixes in git-config

Quoted 7 lines · Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching has its limits.
[...]
This patch has fallen through the cracks, therefore I re-send it. Previous discussion about this patch is at http://www.spinics.net/lists/git/msg149593.html.
The only change I've done since version 2 of this patch is replacing apostrophes with backticks in the first hunk.

Wow, it's been a while. :)

Generally it looks OK to me, but I have two comments:

Documentation/config.txt All the other lines (and the remainder of the line after the section · line 61

Quoted 13 lines

Syntax
~~~~~~

Documentation/config.txt +4 -3
@@ -54,9 +55,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, allow only alphanumeric characters
+and `-`, and must start with an alphabetic character.  There can be more
+than one value for a given variable; we say then that variable is
+multivalued.

Not an error you introduced, but should it be "...we say then that _the_ variable is multivalued".

Quoted 7 lines
Documentation/git-config.txt +0 -0
index aa8303b..a54fee8 100644
@@ -85,8 +85,13 @@ OPTIONS
 	is not exactly one.
 

Documentation/git-config.txt OPTIONS · line 94

Quoted 10 lines
Documentation/git-config.txt +7 -2
 --get-regexp::
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.  Regular expressions are partially lower-cased
+	before matching (everything before the first dot and after the last
+	dot), which makes things like "Core.*' work.

I know I ack'ed this last time around, but reading it fresh, I think we are probably better off to just not mention the down-casing at all. It's just confusing, and people shouldn't depend on it. They should know that they are comparing against the canonical name, and should use lowercase in their regex. I.e., just cut out the last sentence from there.

-Peff

#23 lore raw

Re: [PATCH v3] Documentation fixes in git-config

Quoted 1 line · Jeff King

On Thu, Mar 01, 2012 at 09:19:42AM +0100, Libor Pechacek wrote:

[...]

Quoted 18 lines

Generally it looks OK to me, but I have two comments:

Documentation/config.txt All the other lines (and the remainder of the line after the section · line 61

Quoted 13 lines

Syntax
~~~~~~

Documentation/config.txt +4 -3
@@ -54,9 +55,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, allow only alphanumeric characters
+and `-`, and must start with an alphabetic character.  There can be more
+than one value for a given variable; we say then that variable is
+multivalued.

Not an error you introduced, but should it be "...we say then that _the_ variable is multivalued".

Yes, even my language sense tells me the article is missing. Fixed.

Quoted 7 lines
Quoted 7 lines
Documentation/git-config.txt +0 -0
index aa8303b..a54fee8 100644
@@ -85,8 +85,13 @@ OPTIONS
 	is not exactly one.
 
Quoted 16 lines

Documentation/git-config.txt OPTIONS · line 94

Quoted 10 lines
Documentation/git-config.txt +7 -2
 --get-regexp::
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.  Regular expressions are partially lower-cased
+	before matching (everything before the first dot and after the last
+	dot), which makes things like "Core.*' work.

I know I ack'ed this last time around, but reading it fresh, I think we are probably better off to just not mention the down-casing at all. It's just confusing, and people shouldn't depend on it. They should know that they are comparing against the canonical name, and should use lowercase in their regex. I.e., just cut out the last sentence from there.

I agree, it's just a technical detail, which can be left out in sake of readability.

Thanks for review, the final patch will follow.

Libor

#24 lore raw

Re: [PATCH v3] Documentation fixes in git-config

In reply to Jeff King
Quoted 11 lines · Jeff King
Quoted 7 lines · Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching has its limits.
[...]
This patch has fallen through the cracks, therefore I re-send it. Previous discussion about this patch is at http://www.spinics.net/lists/git/msg149593.html.
The only change I've done since version 2 of this patch is replacing apostrophes with backticks in the first hunk.

Wow, it's been a while. :)

It indeed is.

Thanks. Picked up the v4 patch.

#25 lore raw

PATCH v4 1/1 Documentation fixes in git-config

In reply to Libor Pechacek

Variable names must start with an alphabetic character, regexp config key matching has its limits, sentence grammar.

---

Junio,

Jeff had two more comments, which I've incorporated into the fix. I think we've reached the acme of patch polishing, and I'd call this patch final. Please apply.

 Documentation/config.txt     |   12 +++++++-----
 Documentation/git-config.txt |    7 +++++--
 2 files changed, 12 insertions(+), 7 deletions(-)
Documentation/config.txt +7 -5
index e55dae1..5367ba9 100644
@@ -12,8 +12,9 @@ The configuration variables are used by both the git plumbing
 and the porcelains. The variables are divided into sections, wherein
 the fully qualified variable name of the variable itself is the last
 dot-separated segment and the section name is everything before the last
-dot. The variable names are case-insensitive and only alphanumeric
-characters are allowed. Some variables may appear multiple times.
+dot. The variable names are case-insensitive, allow only alphanumeric
+characters and `-`, and must start with an alphabetic character.  Some
+variables may appear multiple times.
 
 Syntax
 ~~~~~~
@@ -54,9 +55,10 @@ All the other lines (and the remainder of the line after the section
 header) are recognized as setting variables, in the form
 'name = value'.  If there is no equal sign on the line, the entire line
 is taken as 'name' and the variable is recognized as boolean "true".
-The variable names are case-insensitive and only alphanumeric
-characters and `-` are allowed.  There can be more than one value
-for a given variable; we say then that variable is multivalued.
+The variable names are case-insensitive, allow only alphanumeric characters
+and `-`, and must start with an alphabetic character.  There can be more
+than one value for a given variable; we say then that the variable is
+multivalued.
 
 Leading and trailing whitespace in a variable value is discarded.
 Internal whitespace within a variable value is retained verbatim.
Documentation/git-config.txt +5 -2
index aa8303b..81b0398 100644
@@ -85,8 +85,11 @@ OPTIONS
 	is not exactly one.
 
 --get-regexp::
-	Like --get-all, but interprets the name as a regular expression.
-	Also outputs the key names.
+	Like --get-all, but interprets the name as a regular expression and
+	writes out the key names.  Regular expression matching is currently
+	case-sensitive and done against a canonicalized version of the key
+	in which section and variable names are lowercased, but subsection
+	names are not.
 
 --global::
 	For writing options: write to global ~/.gitconfig file rather than
Signature · 1 line
1.7.9.2.324.g78dedf
#26 lore raw

PATCH 1/1 Disallow empty section and variable names

It is possible to break you repository config by creating an invalid config option. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config .foo
fatal: bad config file line 6 in .git/config

This patch makes git-config reject keys which start or end with a dot. The fix also revealed a typo in t5526-fetch-submodules, which is fixed by this patch as well.

---

Applies on top "Sanity-check config variable names".

 config.c                    |    7 ++++++-
 t/t5526-fetch-submodules.sh |    2 +-
 2 files changed, 7 insertions(+), 2 deletions(-)
config.c +6 -1
index c976544..81a0705 100644
@@ -1119,7 +1119,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL) {
+	if (last_dot == NULL || *key == '.') {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
@@ -1156,6 +1156,11 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	if (store_key)
 		(*store_key)[i] = 0;
 
+	if (key[i-1] == '.') {
+		error("key does not contain variable name: %s", key);
+		goto out_free_ret_1;
+	}
+
 	return 0;
 
 out_free_ret_1:
t/t5526-fetch-submodules.sh +1 -1
index 884a5e5..7106c6c 100755
@@ -124,7 +124,7 @@ test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti
 	(
 		cd downstream &&
 		git fetch --recurse-submodules >../actual.out 2>../actual.err &&
-		git config -f --unset .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+		git config -f .gitmodules --unset submodule.submodule.fetchRecurseSubmodules true &&
 		git config --unset submodule.submodule.fetchRecurseSubmodules
 	) &&
 	test_cmp expect.out actual.out &&
Signature · 1 line
1.7.4.rc3.3.g8b2bfe
#27 lore raw

PATCH v2 1/1 Disallow empty section and variable names

It is possible to break your repository config by creating an invalid key. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config .foo
fatal: bad config file line 6 in .git/config

This patch makes git-config reject keys which start or end with a dot, adds tests for these cases and also fixes a typo in t5526-fetch-submodules, which was exposed by the new check.

---

Added tests for the cases checked, made git_config_parse_key consistently return -2 when the key is invalid. Applies on top "Sanity-check config variable names".

 config.c                    |    8 +++++++-
 t/t1300-repo-config.sh      |    4 ++++
 t/t5526-fetch-submodules.sh |    2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)
config.c +7 -1
index fde91f5..e27a39f 100644
@@ -1120,11 +1120,17 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL) {
+	if (last_dot == NULL || *key == '.') {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
 
+	i = strlen(key);
+	if (i && key[i-1] == '.') {
+		error("key does not contain variable name: %s", key);
+		return -2;
+	}
+
 	baselen = last_dot - key;
 	if (baselen_)
 		*baselen_ = baselen;
t/t1300-repo-config.sh +4 -0
index c3d91d1..568d51d 100755
@@ -889,6 +889,10 @@ test_expect_success 'key sanity-checking' '
 	test_must_fail git config foo.1bar &&
 	test_must_fail git config foo."ba
 				z".bar &&
+	test_must_fail git config . &&
+	test_must_fail git config .foo &&
+	test_must_fail git config foo. &&
+	test_must_fail git config .foo. &&
 	git config foo.bar true &&
 	git config foo."ba =z".bar false
 '
t/t5526-fetch-submodules.sh +1 -1
index 884a5e5..7106c6c 100755
@@ -124,7 +124,7 @@ test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti
 	(
 		cd downstream &&
 		git fetch --recurse-submodules >../actual.out 2>../actual.err &&
-		git config -f --unset .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+		git config -f .gitmodules --unset submodule.submodule.fetchRecurseSubmodules true &&
 		git config --unset submodule.submodule.fetchRecurseSubmodules
 	) &&
 	test_cmp expect.out actual.out &&
Signature · 1 line
1.7.4.rc3.11.g54760
#28 lore raw

Re: [PATCH v2] Disallow empty section and variable names

Am 1/30/2011 21:34, schrieb Libor Pechacek:

Quoted 8 lines

It is possible to break your repository config by creating an invalid key. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config .foo
fatal: bad config file line 6 in .git/config

Just a nit: Your example is misleading because it "only" shows that if you shove in junk, and ask for junk, you get breakage. However, the breakage is much more serious, and you could have demonstrated it, if you had written your example like this:

$ git config .foo false
$ git config user.email
fatal: bad config file line 6 in .git/config

t/t1300-repo-config.sh test_expect_success 'key sanity-checking' ' · line 895

Quoted 4 lines
t/t1300-repo-config.sh +4 -0
+	test_must_fail git config . &&
+	test_must_fail git config .foo &&
+	test_must_fail git config foo. &&
+	test_must_fail git config .foo. &&

Not a nit: These tests only show that 'git config' cannot be asked for junk, but they do not show that you cannot insert junk into the config file anymore using 'git config'.

-- Hannes

#29 lore raw

Re: [PATCH v2] Disallow empty section and variable names

Quoted 10 lines · Johannes Sixt

Am 1/30/2011 21:34, schrieb Libor Pechacek:

Quoted 8 lines

It is possible to break your repository config by creating an invalid key. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config .foo
fatal: bad config file line 6 in .git/config
Quoted 8 lines

Just a nit: Your example is misleading because it "only" shows that if you shove in junk, and ask for junk, you get breakage. However, the breakage is much more serious, and you could have demonstrated it, if you had written your example like this:

$ git config .foo false
$ git config user.email
fatal: bad config file line 6 in .git/config

Yes, that one is more persuasive. Thanks for hint.

Quoted 8 lines

t/t1300-repo-config.sh test_expect_success 'key sanity-checking' ' · line 895

Quoted 4 lines
t/t1300-repo-config.sh +4 -0
+	test_must_fail git config . &&
+	test_must_fail git config .foo &&
+	test_must_fail git config foo. &&
+	test_must_fail git config .foo. &&

Not a nit: These tests only show that 'git config' cannot be asked for junk, but they do not show that you cannot insert junk into the config file anymore using 'git config'.

This change builds on top of "Sanity-check config variable names" which makes setting and getting values use the same key checking routine. For the moment, it does not matter if we test the "set" ot "get" path.

Yet, I accept the get/set operations can use different key checking routine in the future (although I can't imagine the reson for it), and overall testing the "set" path is in line with the idea of the patch.

Thanks for input, I'll send updated patch.

Libor

Signature · 2 lines
Libor Pechacek
SUSE L3 Team, Prague
#30 lore raw

Re: [PATCH v2] Disallow empty section and variable names

Am 1/31/2011 10:17, schrieb Libor Pechacek:

Quoted 11 lines · Johannes Sixt
Quoted 9 lines

Am 1/30/2011 21:34, schrieb Libor Pechacek:

t/t1300-repo-config.sh test_expect_success 'key sanity-checking' ' · line 895

Quoted 4 lines
t/t1300-repo-config.sh +4 -0
+	test_must_fail git config . &&
+	test_must_fail git config .foo &&
+	test_must_fail git config foo. &&
+	test_must_fail git config .foo. &&

Not a nit: These tests only show that 'git config' cannot be asked for junk, but they do not show that you cannot insert junk into the config file anymore using 'git config'.

Quoted 3 lines

This change builds on top of "Sanity-check config variable names" which makes setting and getting values use the same key checking routine. For the moment, it does not matter if we test the "set" ot "get" path.

The purpose of tests is not to check the implementation, but the observable behavior. Therefore, you want to write a test case to make sure that neither usage is broken by future changes.

-- Hannes

#31 lore raw

PATCH v3 1/1 Disallow empty section and variable names

It is possible to break your repository config by creating an invalid key. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config core.bare
fatal: bad config file line 6 in .git/config

This patch makes git-config reject keys which start or end with a dot, adds tests for these cases and also fixes a typo in t5526-fetch-submodules, which was exposed by the new check.

---

Incoporated feedback from Johannes, introduced keylen local variable to improve readability of the code. Applies on top "Sanity-check config variable names".

 config.c                    |   10 ++++++++--
 t/t1300-repo-config.sh      |    4 ++++
 t/t5526-fetch-submodules.sh |    2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)
config.c +8 -2
index fde91f5..5eb89a7 100644
@@ -1113,6 +1113,7 @@ int git_config_set(const char *key, const char *value)
 int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 {
 	int i, dot, baselen;
+	int keylen = strlen(key);
 	const char *last_dot = strrchr(key, '.');
 
 	/*
@@ -1120,11 +1121,16 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL) {
+	if (last_dot == NULL || *key == '.') {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
 
+	if (keylen && key[keylen-1] == '.') {
+		error("key does not contain variable name: %s", key);
+		return -2;
+	}
+
 	baselen = last_dot - key;
 	if (baselen_)
 		*baselen_ = baselen;
@@ -1132,7 +1138,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	/*
 	 * Validate the key and while at it, lower case it for matching.
 	 */
-	*store_key = xmalloc(strlen(key) + 1);
+	*store_key = xmalloc(keylen + 1);
 
 	dot = 0;
 	for (i = 0; key[i]; i++) {
t/t1300-repo-config.sh +4 -0
index c3d91d1..53fb822 100755
@@ -889,6 +889,10 @@ test_expect_success 'key sanity-checking' '
 	test_must_fail git config foo.1bar &&
 	test_must_fail git config foo."ba
 				z".bar &&
+	test_must_fail git config . false &&
+	test_must_fail git config .foo false &&
+	test_must_fail git config foo. false &&
+	test_must_fail git config .foo. false &&
 	git config foo.bar true &&
 	git config foo."ba =z".bar false
 '
t/t5526-fetch-submodules.sh +1 -1
index 884a5e5..7106c6c 100755
@@ -124,7 +124,7 @@ test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti
 	(
 		cd downstream &&
 		git fetch --recurse-submodules >../actual.out 2>../actual.err &&
-		git config -f --unset .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+		git config -f .gitmodules --unset submodule.submodule.fetchRecurseSubmodules true &&
 		git config --unset submodule.submodule.fetchRecurseSubmodules
 	) &&
 	test_cmp expect.out actual.out &&
Signature · 1 line
1.7.4.rc3.11.g863f7
#32 lore raw

Re: [PATCH v3] Disallow empty section and variable names

Am 31.01.2011 14:08, schrieb Libor Pechacek:

t/t5526-fetch-submodules.sh test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti · line 127

Quoted 10 lines
t/t5526-fetch-submodules.sh +1 -1
index 884a5e5..7106c6c 100755
@@ -124,7 +124,7 @@ test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setti
 	(
 		cd downstream &&
 		git fetch --recurse-submodules >../actual.out 2>../actual.err &&
-		git config -f --unset .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+		git config -f .gitmodules --unset submodule.submodule.fetchRecurseSubmodules true &&

This is not quite the right fix (notice the extra "true" value at the end). IMHO this should be fixed separately, patch coming.

#33 lore raw

PATCH v4 1/1 Disallow empty section and variable names

It is possible to break your repository config by creating an invalid key. The config parser in turn chokes on it.

$ git init
Initialized empty Git repository in /tmp/gittest/.git/
$ git config .foo false
$ git config core.bare
fatal: bad config file line 6 in .git/config

This patch makes git-config reject keys which start or end with a dot and adds tests for these cases.

---

Fix in t5526-fetch-submodules.sh was posted separately by Jens Lehmann. Applies on top "Sanity-check config variable names".

 config.c               |   10 ++++++++--
 t/t1300-repo-config.sh |    4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)
config.c +8 -2
index fde91f5..5eb89a7 100644
@@ -1113,6 +1113,7 @@ int git_config_set(const char *key, const char *value)
 int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 {
 	int i, dot, baselen;
+	int keylen = strlen(key);
 	const char *last_dot = strrchr(key, '.');
 
 	/*
@@ -1120,11 +1121,16 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL) {
+	if (last_dot == NULL || *key == '.') {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
 
+	if (keylen && key[keylen-1] == '.') {
+		error("key does not contain variable name: %s", key);
+		return -2;
+	}
+
 	baselen = last_dot - key;
 	if (baselen_)
 		*baselen_ = baselen;
@@ -1132,7 +1138,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	/*
 	 * Validate the key and while at it, lower case it for matching.
 	 */
-	*store_key = xmalloc(strlen(key) + 1);
+	*store_key = xmalloc(keylen + 1);
 
 	dot = 0;
 	for (i = 0; key[i]; i++) {
t/t1300-repo-config.sh +4 -0
index c3d91d1..53fb822 100755
@@ -889,6 +889,10 @@ test_expect_success 'key sanity-checking' '
 	test_must_fail git config foo.1bar &&
 	test_must_fail git config foo."ba
 				z".bar &&
+	test_must_fail git config . false &&
+	test_must_fail git config .foo false &&
+	test_must_fail git config foo. false &&
+	test_must_fail git config .foo. false &&
 	git config foo.bar true &&
 	git config foo."ba =z".bar false
 '
Signature · 1 line
1.7.4.rc3.11.g863f7
#34 lore raw

Re: [PATCH v4] Disallow empty section and variable names

Quoted 13 lines · Libor Pechacek
config.c +1 -0
index fde91f5..5eb89a7 100644
@@ -1113,6 +1113,7 @@ int git_config_set(const char *key, const char *value)
 int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 {
 	int i, dot, baselen;
+	int keylen = strlen(key);
 	const char *last_dot = strrchr(key, '.');
 
 	/*

config.c int git_config_parse_key(const char *key, char **store_key, int *baselen_) · line 1129

Quoted 10 lines
config.c +2 -1
@@ -1120,11 +1121,16 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL) {
+	if (last_dot == NULL || *key == '.') {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
 
+	if (keylen && key[keylen-1] == '.') {

Aren't these the same as saying

	if (!last_dot || last_dot == key) {
		...
        }
	if (!last_dot[1]) { /* we know last_dot is not NULL */
        	...
	}

Again, a minimum fix-up on top of your patch.
Thanks.

config.c +3 -4
index 75dd629..d5bb862 100644
@@ -1113,7 +1113,6 @@ int git_config_set(const char *key, const char *value)
 int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 {
 	int i, dot, baselen;
-	int keylen = strlen(key);
 	const char *last_dot = strrchr(key, '.');
 
 	/*
@@ -1121,12 +1120,12 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	 * key name separated by a dot, we have to know where the dot is.
 	 */
 
-	if (last_dot == NULL || *key == '.') {
+	if (last_dot == NULL || last_dot == key) {
 		error("key does not contain a section: %s", key);
 		return -2;
 	}
 
-	if (keylen && key[keylen-1] == '.') {
+	if (!last_dot[1]) {
 		error("key does not contain variable name: %s", key);
 		return -2;
 	}
@@ -1138,7 +1137,7 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
 	/*
 	 * Validate the key and while at it, lower case it for matching.
 	 */
-	*store_key = xmalloc(keylen + 1);
+	*store_key = xmalloc(strlen(key) + 1);
 
 	dot = 0;
 	for (i = 0; key[i]; i++) {