Fix a bug in configuration parsing, and improve tests and documentation
v4 cover Fix a bug in configuration parsing, and improve tests and documentation
This series is an evolvement from another recent series, [1] as a result of a decision to fix a longstanding bug in the parsing of configuration option values, instead of documenting the status quo. [2][3]
The bufgix introduced in this series _should_ have no hidden negative effects. All of the configuration-related tests, both the old and the new ones, pass with the patches applied.
In v2, this series had five patches in total, out of which the third patch (i.e. patch 3/5) was dropped in v3. [4] Other changes in each version are described in each patch.
There will most probably be follow-up patches, to address the remaining points raised during the review of this series. [5]
Link to v2: https://lore.kernel.org/git/[email protected]/T/#u
Link to v3: https://lore.kernel.org/git/[email protected]/T/#u
[1] https://lore.kernel.org/git/[email protected]/T/#u
[2] https://lore.kernel.org/git/[email protected]/
[3] https://lore.kernel.org/git/[email protected]/
[4] https://lore.kernel.org/git/[email protected]/
[5] https://lore.kernel.org/git/[email protected]/
Dragan Simic (4):
config: minor addition of whitespace
config: really keep value-internal whitespace verbatim
t1300: add more tests for whitespace and inline comments config.txt: describe handling of whitespace further
Documentation/config.txt | 21 ++++---- config.c | 15 ++++-- t/t1300-config.sh | 114 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 17 deletions(-)
PATCH v4 1/4 config: minor addition of whitespace
In general, binary operators should be enclosed in a pair of leading and trailing space (SP) characters. Thus, clean up one spotted expression that for some reason had a "bunched up" operator.
---
Notes:
Changes in v4:
- Junio responded with "will queue" in v3, [1] but this patch hasn't reached the "next" branch yet, so resending it as a reminder
- No changes were introduced
Changes in v3:
- Patch description was expanded a tiny bit, to make it more accurate
- No changes to the source code were introduced
Changes in v2:
- No changes were introduced
[1] https://lore.kernel.org/git/[email protected]/
config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
config.c +1 -1
@@ -869,7 +869,7 @@ static char *parse_value(struct config_source *cs)continue ; }if (c =='"' ) { - quote =1 -quote; + quote =1 - quote;continue ; }strbuf_addch (&cs->value, c);
PATCH v4 2/4 config: really keep value-internal whitespace verbatim
In reply to Dragan SimicFix a bug in function parse_value() that prevented whitespace characters (i.e. spaces and horizontal tabs) found inside configuration option values from being parsed and returned in their original form. The bug caused any number of consecutive whitespace characters to be wrongly "squashed" into the same number of space characters.
This bug was introduced back in July 2009, in commit ebdaae372b46 ("config:
Keep inner whitespace verbatim").
Further investigation showed that setting a configuration value, by invoking git-config(1), converts value-internal horizontal tabs into "\t" escape sequences, which the buggy value-parsing logic in function parse_value() didn't "squash" into spaces. That's why the test included in the ebdaae37 commit passed, which presumably made the bug remain undetected for this long. On the other hand, value-internal literal horizontal tab characters, found in a configuration file edited by hand, do get "squashed" by the value-parsing logic, so the right choice was to fix this bug by making the value-internal whitespace characters preserved verbatim.
---
Notes:
Changes in v4:
- No changes were introduced
Changes in v3:
- No changes were introduced
Changes in v2:
- Dropped the "Fixes" tag, as explained and requested by Junio, [1] because having such tags actually doesn't help us in the long run
- No changes to the source code were introduced
[1] https://lore.kernel.org/git/[email protected]/
config.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
config.c +9 -4
@@ -817,33 +817,38 @@ static int get_next_char(struct config_source *cs)static char *parse_value (struct config_source *cs) { -int quote =0 , comment =0 , space =0 ; +int quote =0 , comment =0 ; +size_t trim_len =0 ;strbuf_reset (&cs->value);for (;;) {int c =get_next_char (cs);if (c =='\n' ) {if (quote) { cs->linenr--;return NULL ; } +if (trim_len) +strbuf_setlen (&cs->value, trim_len);return cs->value.buf; }if (comment)continue ;if (isspace (c) && !quote) { +if (!trim_len) + trim_len = cs->value.len;if (cs->value.len) - space++; +strbuf_addch (&cs->value, c);continue ; }if (!quote) {if (c ==';' || c =='#' ) { comment =1 ;continue ; } } -for (; space; space--) -strbuf_addch (&cs->value,' ' ); +if (trim_len) + trim_len =0 ;if (c =='\\' ) { c =get_next_char (cs);switch (c) {
PATCH v4 3/4 t1300: add more tests for whitespace and inline comments
In reply to Dragan SimicAdd a handful of additional tests, to improve the coverage of the handling of configuration file entries whose values contain internal whitespace, leading and/or trailing whitespace, which may or may not be enclosed within quotation marks, or which contain an additional inline comment.
At the same time, rework one already existing whitespace-related test a bit, to ensure its consistency with the newly added tests. This change introduced no functional changes to the already existing test.
---
Notes:
Changes in v4:
- Improved the added configuration setup test, to make the included leading and trailing whitespace characters more robust and easier on the eyes, as suggested and outlined by Junio [5]
- Expanded the patch description a bit and improved the wording
- Added a Helped-by tag
Changes in v3:
- Removed a few unnecessary invocations of x_to_tab()
- As pointed out by Eric Sunshine, [3] it's better not to introduce new random helper functions, so x_to_tab() was replaced by a direct invocation of tr(1), in one case that requires use of literal 'Q' characters, and by invocations of q_to_tab(), in the remaining cases that actually allow use of 'Q' characters to represent HTs
- Dropped the change of the name of an already existing test, to not distract the reviewers, as suggested by Eric Sunshine [4]
- Renamed the first added test, as suggested by Eric Sunshine, [4] to make it consistent with the expected way for naming setup tests
Changes in v2:
- All new tests and one already existing test reworked according to Eric Sunshine's review suggestions; [1][2] the already existing test was reworked a bit to ensure consistency
- Added a Helped-by tag
[1] https://lore.kernel.org/git/CAPig+cRMPNExbG34xJ0w5npUc3DDwxQUGS_AQfam_mi4s53=sA@mail.gmail.com/
[2] https://lore.kernel.org/git/CAPig+cRG8eFxepkaiN54H+fa7D=rFGsmEHdvTP+HSSaLO_6T_A@mail.gmail.com/
[3] https://lore.kernel.org/git/CAPig+cSLb+Rsy81itvw9Tfvqv9vvKSPgO_ER9fWL04XZrgFvwg@mail.gmail.com/T/#u
[4] https://lore.kernel.org/git/CAPig+cTVmQzC38DympSEtPNhgY=-+dYbZmkr0RTRbhG-hp2fmQ@mail.gmail.com/
[5] https://lore.kernel.org/git/[email protected]/
t/t1300-config.sh | 114 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 3 deletions(-)
t/t1300-config.sh +111 -3
@@ -11,6 +11,98 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh +test_expect_success 'setup whitespace config' ' + sed -e "s/^|//" \ + -e "s/[$]$//" \ + -e "s/X/\\t/g" >.git/config <<-\EOF + [section] + | solid = rock + | sparse = big XX blue + | sparseAndTail = big XX blue $ + | sparseAndTailQuoted = "big XX blue " + | sparseAndBiggerTail = big XX blue X X + | sparseAndBiggerTailQuoted = "big XX blue X X" + | sparseAndBiggerTailQuotedPlus = "big XX blue X X"X $ + | headAndTail = Xbig blue $ + | headAndTailQuoted = "Xbig blue " + | headAndTailQuotedPlus = "Xbig blue " $ + | annotated = big blueX# to be discarded + | annotatedQuoted = "big blue"X# to be discarded + EOF +' + +test_expect_success 'no internal whitespace' ' + echo "rock" >expect && + git config --get section.solid >actual && + test_cmp expect actual +' + +test_expect_success 'internal whitespace' ' + echo "big QQ blue" | q_to_tab >expect && + git config --get section.sparse >actual && + test_cmp expect actual +' + +test_expect_success 'internal and trailing whitespace' ' + echo "big QQ blue" | q_to_tab >expect && + git config --get section.sparseAndTail >actual && + test_cmp expect actual +' + +test_expect_success 'internal and trailing whitespace, all quoted' ' + echo "big QQ blue " | q_to_tab >expect && + git config --get section.sparseAndTailQuoted >actual && + test_cmp expect actual +' + +test_expect_success 'internal and more trailing whitespace' ' + echo "big QQ blue" | q_to_tab >expect && + git config --get section.sparseAndBiggerTail >actual && + test_cmp expect actual +' + +test_expect_success 'internal and more trailing whitespace, all quoted' ' + echo "big QQ blue Q Q" | q_to_tab >expect && + git config --get section.sparseAndBiggerTailQuoted >actual && + test_cmp expect actual +' + +test_expect_success 'internal and more trailing whitespace, not all quoted' ' + echo "big QQ blue Q Q" | q_to_tab >expect && + git config --get section.sparseAndBiggerTailQuotedPlus >actual && + test_cmp expect actual +' + +test_expect_success 'leading and trailing whitespace' ' + echo "big blue" >expect && + git config --get section.headAndTail >actual && + test_cmp expect actual +' + +test_expect_success 'leading and trailing whitespace, all quoted' ' + echo "Qbig blue " | q_to_tab >expect && + git config --get section.headAndTailQuoted >actual && + test_cmp expect actual +' + +test_expect_success 'leading and trailing whitespace, not all quoted' ' + echo "Qbig blue " | q_to_tab >expect && + git config --get section.headAndTailQuotedPlus >actual && + test_cmp expect actual +' + +test_expect_success 'inline comment' ' + echo "big blue" >expect && + git config --get section.annotated >actual && + test_cmp expect actual +' + +test_expect_success 'inline comment, quoted' ' + echo "big blue" >expect && + git config --get section.annotatedQuoted >actual && + test_cmp expect actual +' + test_expect_success 'clear default config' ' rm -f .git/config '
@@ -1066,9 +1158,25 @@ test_expect_success '--null --get-regexp' ' test_cmp expect result ' -test_expect_success 'inner whitespace kept verbatim' ' - git config section.val "foo bar" && - test_cmp_config "foo bar" section.val +test_expect_success 'inner whitespace kept verbatim, spaces only' ' + echo "foo bar" >expect && + git config section.val "foo bar" && + git config --get section.val >actual && + test_cmp expect actual +' + +test_expect_success 'inner whitespace kept verbatim, horizontal tabs only' ' + echo "fooQQbar" | q_to_tab >expect && + git config section.val "$(cat expect)" && + git config --get section.val >actual && + test_cmp expect actual +' + +test_expect_success 'inner whitespace kept verbatim, horizontal tabs and spaces' ' + echo "foo Q bar" | q_to_tab >expect && + git config section.val "$(cat expect)" && + git config --get section.val >actual && + test_cmp expect actual ' test_expect_success SYMLINKS 'symlinked configuration' '
Re: [PATCH v4 3/4] t1300: add more tests for whitespace and inline comments
Quoted 15 lines · Dragan Simic
Add a handful of additional tests, to improve the coverage of the handling of configuration file entries whose values contain internal whitespace, leading and/or trailing whitespace, which may or may not be enclosed within quotation marks, or which contain an additional inline comment.
At the same time, rework one already existing whitespace-related test a bit, to ensure its consistency with the newly added tests. This change introduced no functional changes to the already existing test.
Helped-by Eric Sunshine <[email protected]>Helped-by Junio C Hamano <[email protected]>Signed-off-by Dragan Simic <[email protected]>---
t/t1300-config.sh export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME · line 14
Quoted 4 lines
t/t1300-config.sh +4
-0@@ -11,6 +11,98 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +test_expect_success 'setup whitespace config' ' + sed -e "s/^|//" \ + -e "s/[$]$//" \ + -e "s/X/\\t/g" >.git/config <<-\EOF
Representing TAB as \t in the sed replacement is not necessarily portable and (as far as I can see) we don't rely upon that in any existing tests. It probably would be safer to employ a literal TAB character in s/X/ /g as Junio showed in his example[1].
Re: [PATCH v4 3/4] t1300: add more tests for whitespace and inline comments
Quoted 10 lines · Eric Sunshine
Quoted 9 lines · Dragan Simic
Add a handful of additional tests, to improve the coverage of the handling of configuration file entries whose values contain internal whitespace, leading and/or trailing whitespace, which may or may not be enclosed within quotation marks, or which contain an additional inline comment.
At the same time, rework one already existing whitespace-related test a bit, to ensure its consistency with the newly added tests. This change introduced no functional changes to the already existing test.
Quoted 15 lines
t/t1300-config.sh export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME · line 14
Quoted 10 lines
Helped-by Eric Sunshine <[email protected]>Helped-by Junio C Hamano <[email protected]>Signed-off-by Dragan Simic <[email protected]>---
t/t1300-config.sh +4
-0@@ -11,6 +11,98 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +test_expect_success 'setup whitespace config' ' + sed -e "s/^|//" \ + -e "s/[$]$//" \ + -e "s/X/\\t/g" >.git/config <<-\EOFRepresenting TAB as
\tin the sed replacement is not necessarily portable and (as far as I can see) we don't rely upon that in any existing tests. It probably would be safer to employ a literal TAB character ins/X/ /gas Junio showed in his example[1].
Hmm, I actually saw using a literal HT character as a disadvantage. Thanks for the notice, I'll send the v5 with this fixed.
Quoted 1 line
PATCH v4 4/4 config.txt: describe handling of whitespace further
In reply to Dragan SimicMake it more clear what the whitespace characters are in the context of git configuration files, and significantly improve the description of the leading and trailing whitespace handling, especially how it works out together with the presence of inline comments.
---
Notes:
Changes in v4:
- Improved the wording and accuracy of the description of whitespace character handling, as discussed with Junio, [1][2] by taking a more radical approach and rewriting an entire paragraph, because it has reached the point where "patching the patchwork" no longer worked; I'm quite happy with the way it turned out this time
- Expanded the patch description a tiny bit
- Added a Helped-by tag
Changes in v3:
- Patch description was expanded a bit, to make it more on point
- No changes to the documentation were introduced
Changes in v2:
- No changes were introduced
[1] https://lore.kernel.org/git/[email protected]/
[2] https://lore.kernel.org/git/[email protected]/
Documentation/config.txt | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
Documentation/config.txt +12 -9
@@ -22,9 +22,10 @@ multivalued. Syntax ~~~~~~ -The syntax is fairly flexible and permissive; whitespaces are mostly -ignored. The '#' and ';' characters begin comments to the end of line, -blank lines are ignored. +The syntax is fairly flexible and permissive. Whitespace characters, +which in this context are the space character (SP) and the horizontal +tabulation (HT), are mostly ignored. The '#' and ';' characters begin +comments to the end of line. Blank lines are ignored. The file consists of sections and variables. A section begins with the name of the section in square brackets and continues until the next
@@ -63,13 +64,15 @@ the variable is the boolean "true"). The variable names are case-insensitive, allow only alphanumeric characters and `-`, and must start with an alphabetic character. + A line that defines a value can be continued to the next line by -ending it with a `\`; the backslash and the end-of-line are -stripped. Leading whitespaces after 'name =', the remainder of the -line after the first comment character '#' or ';', and trailing -whitespaces of the line are discarded unless they are enclosed in -double quotes. Internal whitespaces within the value are retained -verbatim. +ending it with a `\`; the backslash and the end-of-line are stripped. +Leading whitespace characters before 'name =' are discarded. +The portion of the line after the first comment character, including +the comment character itself, is discarded. Unless enclosed in double +quotation marks (`"`), any leading or trailing whitespace characters +surrounding 'value' are discarded. Internal whitespace characters +within 'value' are retained verbatim. Inside double quotes, double quote `"` and backslash `\` characters must be escaped: use `\"` for `"` and `\\` for `\`.
Re: [PATCH v4 4/4] config.txt: describe handling of whitespace further
Quoted 10 lines · Dragan Simic
Make it more clear what the whitespace characters are in the context of git configuration files, and significantly improve the description of the leading and trailing whitespace handling, especially how it works out together with the presence of inline comments.
Helped-by Junio C Hamano <[email protected]>Signed-off-by Dragan Simic <[email protected]>---
Documentation/config.txt the variable is the boolean "true"). · line 71
Quoted 8 lines
Documentation/config.txt +7
-0@@ -63,13 +64,15 @@ the variable is the boolean "true"). A line that defines a value can be continued to the next line by +ending it with a `\`; the backslash and the end-of-line are stripped. +Leading whitespace characters before 'name =' are discarded. +The portion of the line after the first comment character, including +the comment character itself, is discarded. Unless enclosed in double +quotation marks (`"`), any leading or trailing whitespace characters +surrounding 'value' are discarded. Internal whitespace characters +within 'value' are retained verbatim.
I find this statement confusing and ambiguous:
Unless enclosed in double quotation marks ("), any leading or trailing whitespace characters surrounding 'value' are discarded.
since it might imply that the shown <SP> and <TAB> whitespace is retained outside the quotes, as well:
key =<SP><TAB>" string "<SP>
It should be possible to rephrase it to be more definite, while dropping the final sentence altogether. Perhaps:
Whitespace surrounding name, = and value is ignored. If value is surrounding by double quotation marks ("), all characters within the quoted string are retained verbatim, including whitespace. Comments starting with either # or ; and extending to the end of line are discarded. A line that defines a value can be continued to the next line by ending it with a \; the backslash and the end-of-line are stripped.
Re: [PATCH v4 4/4] config.txt: describe handling of whitespace further
Quoted 34 lines · Eric Sunshine
Quoted 16 lines · Dragan Simic
Make it more clear what the whitespace characters are in the context of git configuration files, and significantly improve the description of the leading and trailing whitespace handling, especially how it works out together with the presence of inline comments.
Helped-by Junio C Hamano <[email protected]>Signed-off-by Dragan Simic <[email protected]>---
Documentation/config.txt +4
-0@@ -63,13 +64,15 @@ the variable is the boolean "true"). A line that defines a value can be continued to the next line by +ending it with a `\`; the backslash and the end-of-line are stripped. +Leading whitespace characters before 'name =' are discarded. +The portion of the line after the first comment character, including +the comment character itself, is discarded. Unless enclosed indouble
Documentation/config.txt the variable is the boolean "true"). · line 75
Quoted 3 lines
Documentation/config.txt +3
-0+quotation marks (`"`), any leading or trailing whitespace characters +surrounding 'value' are discarded. Internal whitespace characters +within 'value' are retained verbatim.I find this statement confusing and ambiguous:
Unless enclosed in double quotation marks (
"), any leading or trailing whitespace characters surrounding 'value' are discarded.since it might imply that the shown <SP> and <TAB> whitespace is retained outside the quotes, as well:
key =<SP><TAB>" string "<SP>It should be possible to rephrase it to be more definite, while dropping the final sentence altogether. Perhaps:
Quoted 7 lines
Whitespace surrounding
name,=andvalueis ignored. Ifvalueis surrounding by double quotation marks ("), all characters within the quoted string are retained verbatim, including whitespace. Comments starting with either#or;and extending to the end of line are discarded. A line that defines a value can be continued to the next line by ending it with a\; the backslash and the end-of-line are stripped.
Looking good to me, thanks. I'll include it into the v5, with a small grammar issue fixed.
Re: [PATCH v4 4/4] config.txt: describe handling of whitespace further
Quoted 12 lines · Dragan Simic
Quoted 10 lines · Eric Sunshine
It should be possible to rephrase it to be more definite, while dropping the final sentence altogether. Perhaps:
Whitespace surrounding
name,=andvalueis ignored. Ifvalueis surrounding by double quotation marks ("), all characters within the quoted string are retained verbatim, including whitespace. Comments starting with either#or;and extending to the end of line are discarded. A line that defines a value can be continued to the next line by ending it with a\; the backslash and the end-of-line are stripped.
Quoted 2 lines
Looking good to me, thanks. I'll include it into the v5, with a small grammar issue fixed.
For completeness, I should mention that I intentionally reordered the topics so that the most common/important ones are mentioned earlier rather than later; i.e. (1) surrounding whitespace ignored, (2) double-quoted value, (3) comments, (4) \ line-splicing with.
Re: [PATCH v4 4/4] config.txt: describe handling of whitespace further
Quoted 13 lines · Eric Sunshine
Quoted 12 lines · Dragan Simic
Quoted 10 lines · Eric Sunshine
It should be possible to rephrase it to be more definite, while dropping the final sentence altogether. Perhaps:
Whitespace surrounding
name,=andvalueis ignored. Ifvalueis surrounding by double quotation marks ("), all characters within the quoted string are retained verbatim, including whitespace. Comments starting with either#or;and extending to the end of line are discarded. A line that defines a value can be continued to the next line by ending it with a\; the backslash and the end-of-line are stripped.
Quoted 7 lines
Quoted 2 lines
Looking good to me, thanks. I'll include it into the v5, with a small grammar issue fixed.
For completeness, I should mention that I intentionally reordered the topics so that the most common/important ones are mentioned earlier rather than later; i.e. (1) surrounding whitespace ignored, (2) double-quoted value, (3) comments, (4)
\line-splicing with.
Hmm, I just noticed that your proposed description actually contains some issues, e.g. it implies that the value-internal whitespace is retained verbatim only if the entire value is enclosed in double quotation marks. I'll try to reword it, so this is fixed.
Re: [PATCH v4 4/4] config.txt: describe handling of whitespace further
In reply to Eric SunshineQuoted 7 lines · Eric Sunshine
Whitespace surrounding
name,=andvalueis ignored. Ifvalueis surrounding by double quotation marks ("), all characters within the quoted string are retained verbatim, including whitespace. Comments starting with either#or;and extending to the end of line are discarded. A line that defines a value can be continued to the next line by ending it with a\; the backslash and the end-of-line are stripped.
Nice, but I am not sure how this captures how whitespaces between value and comment are handled, e.g., in this line
| name = value # comment$
humans know the space before '#' is removed because it is "whitespace surrounding value". But there is a bit of chicken and egg problem; before you realize '# comment' is a comment and strip it from the line, you do not know where value ends, so your reading of the above need to backtrack.
Quoted 3 lines · Dragan Simic
In v2, this series had five patches in total, out of which the third patch (i.e. patch 3/5) was dropped in v3. [4] Other changes in each version are described in each patch.
In addition to a written description of the changes since the previous version, reviewers also greatly appreciate seeing a range-diff which you can generate automatically in the cover-letter via:
git format-patch --cover-letter --range-diff=<...> ...
or less frequently, an interdiff via:
git format-patch --cover-letter --interdiff=<...> ...
Both options (--range-diff & --interdiff) also work for single-patch series which lack a cover-letter.
Quoted 9 lines · Eric Sunshine
Quoted 3 lines · Dragan Simic
In v2, this series had five patches in total, out of which the third patch (i.e. patch 3/5) was dropped in v3. [4] Other changes in each version are described in each patch.
In addition to a written description of the changes since the previous version, reviewers also greatly appreciate seeing a range-diff which you can generate automatically in the cover-letter via:
Quoted 8 lines
git format-patch --cover-letter --range-diff=<...> ...or less frequently, an interdiff via:
git format-patch --cover-letter --interdiff=<...> ...Both options (--range-diff & --interdiff) also work for single-patch series which lack a cover-letter.
I'll keep that in mind for future patches.