Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Dominik Maier <dmnk@google.com>
Cc: git@vger.kernel.org,  Phillip Wood <phillip.wood@dunelm.org.uk>,
	dmnk+artist@google.com
Subject: Re: [PATCH v2] config: skip continued lines when matching section
Date: Thu, 26 Nov 2026 02:37:38 -0800	[thread overview]
Message-ID: <xmqqtsl3op3h.fsf@gitster.g> (raw)
In-Reply-To: <20261124180556.69724-1-dmnk@google.com> (Dominik Maier's message of "Tue, 24 Nov 2026 18:05:56 +0000")

Dominik Maier <dmnk@google.com> writes:

> repo_config_copy_or_rename_section_in_file() scans the config file line
> by line and treats any line whose first non-whitespace character is '['
> as the start of a new section, without tracking backslash-continued
> values across lines, unlike git_parse_source().

That sums up the mismatch cleanly.  Because git_parse_source() already
treats a trailing backslash on a value line as continuing onto the next
line, keeping repo_config_copy_or_rename_section_in_file() in sync fixes
a real corruption bug for remove, rename, and copy operations without
changing how config files are read.

> diff --git a/config.c b/config.c
> index 275e709f06..cbdf3431f3 100644
> --- a/config.c
> +++ b/config.c
> @@ -3428,6 +3428,140 @@ static int section_name_is_ok(const char *name)
>  
>  #define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
>  
> +static int is_config_eol(const char *p, const char *end)
> +{
> +	return p >= end || *p == '\n' ||
> +	       (*p == '\r' && p + 1 < end && p[1] == '\n');
> +}

This matches get_next_char() in config.c, which only collapses a '\r'
followed by '\n' into '\n' and otherwise passes bare '\r' through as
part of a subsection name or value.

Using p < end in the caller when setting *continued also avoids falsely
continuing when a file ends with a trailing backslash and no newline.

> +static const char *skip_section_header(const char *p, const char *end)
> +{
> +	const char *start;
> +
> +	if (is_config_eol(p, end) || *p != '[')
> +		return NULL;

We return when at the end of line, or not at '['.  Otherwise we go to
the next loop.  Wait...why check eol?  '\r' or '\n' are certainly not
'[' so wouldn't it be the same as "if (end <= p || *p != '[')"?  Wnat am
I missing?

Anyway, we saw '[' and come here.  We keep scanning ...

> +	for (start = ++p; !is_config_eol(p, end); p++) {
> +		if (*p == ']')
> +			return p > start ? p + 1 : NULL;

... and eventually find ']' before we see the end of line that had the
'[' on it.  Initialization is (start = ++p) so in the first iteration
we have (p == start), in which case we return NULL (i.e., we reject
"[]").  Otherwise we return the pointer past that closing ']'.

The proposed commit log message does not make it clear why we want to
reject an empty "[]".  Or what should happen to say "[ ]" for that
matter.

> +		if (isspace(*p)) {
> +			while (!is_config_eol(p, end) && isspace(*p))
> +				p++;

We allow whitespaces after the opening "[".  Then

> +			if (is_config_eol(p, end) || *p != '"')
> +				return NULL;

We want a double quote.  Wait, why double quote?

	[core] username = ...
	[remote "origin"] fetch = ...

We do not allow whitespace before 'core' or 'remote' in the above
example.  We see after this block we reject letters that are not key
letters or dot but otherwise keep looping.  So 'core' and 'remote' in
the above example are simply skipped over while this loop spins.  OK.
And after skipping over 'remote', we skip whitespaces, and then yes, we
expect double-quote at the beginning of 'origin'.

    Side note.  I recall we also support

	[remote.origin] fetch = ...

    as a less expressive (because you can only have keychars there)
    deprecated synonym.  They are treated just like [core] is treated by
    this code, I suppose.

> +			for (p++; !is_config_eol(p, end); p++) {

And we keep reading inside the double quotes.

> +				if (*p == '\\') {
> +					p++;
> +					if (is_config_eol(p, end))
> +						return NULL;
> +					continue;
> +				}

We allow a backslash to escape any single letter, and this can be used
to escape a double quote that is a part of the second level variable
name.  But we reject

	[foo "bar\
	... anything here ...

i.e., backslash and then end of line.

> +				if (*p == '"') {
> +					p++;
> +					return (!is_config_eol(p, end) &&
> +						*p == ']') ? p + 1 : NULL;
> +				}

After we see a closing double quote, if we are not yet at the end of
line, we may be ']' in which case we are happy and return the pointer to
the byte after that closing bracket; otherwise we reject and return NULL.

If we do not have ']' immediately after double quote we reject, which
matches what get_extended_base_var() does.

And all other letters are simply skipped in this loop.

> +			}
> +			return NULL;

If we exited the loop while looking for the closing ']', we reject.

> +		}
> +		if (!iskeychar(*p) && *p != '.')
> +			return NULL;
> +	}
> +	return NULL;
> +}

OK.  So we find [section "header"] that is syntactically valid?

> +	for (; !is_config_eol(p, end); p++) {
> +		if (!*in_quote && (*p == '#' || *p == ';')) {
> +			*continued = 0;
> +			return;
> +		}
> +		if (*p == '"') {
> +			*in_quote = !*in_quote;
> +			continue;
> +		}
> +		if (*p == '\\') {
> +			p++;
> +			if (is_config_eol(p, end)) {
> +				*continued = (p < end);
> +				if (!*continued)
> +					*in_quote = 0;
> +				return;
> +			}

Tracking *in_quote across continued lines makes sure '#' and ';' inside
a quoted multi-line value do not prematurely clear *continued, while an
invalid escape or a bare '\r' after '\' resets both flags just as
parse_value() rejects them.  Tricky.

> @@ -3550,6 +3686,10 @@ static int repo_config_copy_or_rename_section_in_file(
>  				} else {
>  					strbuf_release(&copystr);
>  					copystr = store_create_section(new_name, &store);
> +					if (output[offset + i]) {
> +						strbuf_addch(&copystr, '\t');
> +						strbuf_addstr(&copystr, output + offset + i);
> +					}
>  				}

For context, section_name_match() gobbles every trailing isspace()
character after ']', including '\r' and '\n', via

    i++;
    for (; buf[i] && isspace(buf[i]); i++)
            ; /* do nothing */
    return i;

So output[offset + i] is NUL on a plain "[section]\n" or "[section]\r\n"
line, and non-empty only when an inline entry or comment follows ']',
matching the existing rename branch.

  reply	other threads:[~2026-11-26 10:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:23 [PATCH] config: skip continued lines when matching section Dominik Maier
2026-11-20  0:44 ` Junio C Hamano
2026-11-20 14:33   ` Phillip Wood
2026-11-24 18:05 ` [PATCH v2] " Dominik Maier
2026-11-26 10:37   ` Junio C Hamano [this message]
2026-11-26 11:00     ` Chris Torek
2026-11-30 16:22     ` Dominik Maier
2026-11-30 16:22     ` [PATCH v3] " Dominik Maier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqtsl3op3h.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=dmnk+artist@google.com \
    --cc=dmnk@google.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox