From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796055734; b=uxSNZfCJLh9iqavSKrPb8zw2LTX3a0MUbxTkNreSveB/1mZmJ0tEGO85D/frCmZsF7Pa ElBf/b2f1JguJp7EUS/6egD9rHAVcNYdnsvMTiIM89jbJjxM+/L+sGOdCCS62RQyAg2p+ D7t3A290fCZUa/A9COlToKmkAYJvCXK/UrDQJWwnR/Qy0ovM8K65XDpwihiNdGFiSHCvY Ln7auseynEPhpVXaCRH3FGA/db1rSUMXIYfkDW0UeTdotK/Bb9r9/FCTTyQJnhamihnLS 8qSLILqWZOKw98vF8onO6eMzTLHZfvi5iBEAOljSE2ysVC4fCpONgZXHneiA4c1D2jA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796055734; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=b3AumBVKDWuHrkKpAuUBvNfSnuagTTXyF9O5qoIR7uM=; b=cr+QDQw9KJcAzPstiJ8IUy6JeW504NMdhJLdv0OxIBf6sJqjfJmddmouyPAFHuPddBCo 69SZNQ5UrU06xt5BDV6z+RWdRb37nJxmF3nR7TAk1o2Gm4yQtZhRRBEaVNMw2Edyupa4O Dhio9jf1pU8Zvwk+gh2Jcoy7737loj0uIYbmgucXz3Xdbe45MSLOC+F+f9zGSKIk9X5W3 Sh5uN/RHuCY4c9kJheFOEBI6ZJYP+b/P4Hy0HuuhzHtnI1ttv8+gYGKDMbQmMXrxsjcIl CBli5YveXrcYqbVcDo48whFD8+GbkedUnm+d294oThvYbClWCB2u69nj7DW14ucHOtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmnk.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pDTby0Qw; arc=none smtp.remote-ip=209.85.208.72 Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFE29606A32F for ; Mon, 30 Nov 2026 16:22:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AFE29606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmnk.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pDTby0Qw Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-6b3dc91781fso2339261a12.0 for ; Mon, 30 Nov 2026 08:22:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796055731; x=1796660531; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=b3AumBVKDWuHrkKpAuUBvNfSnuagTTXyF9O5qoIR7uM=; b=pDTby0QwzveTVo6yIm4S3XCppFZPvZfCZJzD8Al+YoGDPTMsMQQ0A5HsPXMLt3T2LH eWJ8tdBckjnxSrB3hcIlPd4YlrfKvHe9NAvRfxXRxXEPN3wdV2I2PiIVWjtvGwwTEJDZ FfJuU0hBztaFvJhSIT3xCPg/6biIy8loIYcy5wcVk9RJILEh9zGUABhFrUtQbiA2KtF6 q1ao5ruCsn6P9RnaxrvLrM2k+vUB4ruBDXsP8KxhPMstl8MI1FjsmlmqSN/1NdAmxReE s7ljpxjXJiW/7TYf2ZldiMtNZVtjkvIkOtc+V8kE5mTGQOOpE5IPdTA1yIJi8Kh/fJtM CFzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796055731; x=1796660531; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=b3AumBVKDWuHrkKpAuUBvNfSnuagTTXyF9O5qoIR7uM=; b=rAjj/BcvotaDfR4nCooYrmjq/KZ4BmmA52xPKwg16PWK2QE3qL2wZjinXR8Li/POPq qLQ/JCqUUxD5+/qPHlDgJsCJ72CdEnDcc44rXDz84fszl96zVYPVKdErzcz6XgfEy1qC RcUNCO0jdwaptAGIqaY0BU5a26T4B+3hb8rOWzwWre1VabSZw9vs8tiTMnBEOTIrJc7W F7ZtmIN5heSuNBaDrRndPgmuuQOk7kDgXUGCIfZ8IOhsLwTRYCnupEZnC8UoI/IOdy2Y n8ojgqiJYeIhiZ1VO2Pxy0qrYqUoClPm47mIVUKObBpbpQzfzxY7aa5s5xVmnXfxotdJ AxHQ== X-Gm-Message-State: APuU4Z/oCkYIJR/IFeblkRFfpDnBCSrW223/Cnmgr68bqwKWFDDg0RJJ LW35+fpUMi3ma+Tbe41UKj5NAIxqEybz6ClewugGFu1rPEHI5iIoPgjo6oBfMW5mGj3orAI8JMB T0XJxC2iRlbgZF/CKcwPXFm2Fx/ShYCgfv1n7LobzzEoHVIxBeP0EvaYILLoeneSkwqYq5/ZBDC lqwTOlVPQpyVCKgYf3OD2scw== X-Received: from edyd13.prod.google.com ([2002:a05:6402:78d:b0:6b8:a034:6b10]) (user=dmnk job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:d587:0:b0:6b7:a647:c28d with SMTP id 4fb4d7f45d1cf-6ba37eb44b0mr16657063a12.32.1796055731217; Mon, 30 Nov 2026 08:22:11 -0800 (PST) Date: Mon, 30 Nov 2026 17:22:10 +0100 In-Reply-To: X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261130162210.3368215-1-dmnk@google.com> Subject: [PATCH v3] config: skip continued lines when matching section From: Dominik Maier To: git@vger.kernel.org Cc: Junio C Hamano , Phillip Wood , Chris Torek , dmnk+artist@google.com Content-Type: text/plain; charset="UTF-8" Renaming, copying, or removing a config section treats a continued multiline value starting with '[' as a new section header. Unlike the main config parser, the section rewriter scans each physical line on its own without tracking trailing backslash continuations. When a value in the target section continues onto a line starting with "[other]", removing or renaming the section stops early and leaves the rest of the value behind as a real "[other]" section. Track backslash and quote continuations across physical lines (including CRLF endings, bare carriage returns, quoted subsections, and empty "[]" or "[ ]" headers, which git_parse_source() rejects rather than reading as key-value lines), match section headers only at the start of a logical line, and keep any inline entry after a copied header. Signed-off-by: Dominik Maier --- Changes in v3: - Simplify the opening guard in skip_section_header() to "if (end <= p || *p != '[')" (Junio C Hamano). - Explain why empty "[]" and "[ ]" headers are rejected to match git_parse_source(), and add a "[ ]" test case (Junio C Hamano). - Rebase onto master (86e79b2795). - v2: https://lore.kernel.org/git/20261124180556.69724-1-dmnk@google.com/ config.c | 146 +++++++++++++++++++++++++++++++++++++++++++++- t/t1300-config.sh | 85 +++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 3 deletions(-) diff --git a/config.c b/config.c index 275e709f0..40eb1f15d 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'); +} + +static const char *skip_section_header(const char *p, const char *end) +{ + const char *start; + + if (end <= p || *p != '[') + return NULL; + for (start = ++p; !is_config_eol(p, end); p++) { + if (*p == ']') + return p > start ? p + 1 : NULL; + if (isspace(*p)) { + while (!is_config_eol(p, end) && isspace(*p)) + p++; + if (is_config_eol(p, end) || *p != '"') + return NULL; + for (p++; !is_config_eol(p, end); p++) { + if (*p == '\\') { + p++; + if (is_config_eol(p, end)) + return NULL; + continue; + } + if (*p == '"') { + p++; + return (!is_config_eol(p, end) && + *p == ']') ? p + 1 : NULL; + } + } + return NULL; + } + if (!iskeychar(*p) && *p != '.') + return NULL; + } + return NULL; +} + +static void update_line_continuation(const char *p, const char *end, + int is_first_line, int *continued, + int *in_quote) +{ + int in_value = *continued; + + if (is_first_line) { + size_t rem = end - p; + skip_prefix_mem(p, rem, utf8_bom, &p, &rem); + } + + if (!in_value) { + while (!is_config_eol(p, end)) { + if (isspace(*p)) { + p++; + continue; + } + if (*p == '#' || *p == ';') { + *continued = 0; + *in_quote = 0; + return; + } + if (*p == '[') { + p = skip_section_header(p, end); + if (!p) { + *continued = 0; + *in_quote = 0; + return; + } + continue; + } + if (!isalpha(*p)) { + *continued = 0; + *in_quote = 0; + return; + } + for (p++; !is_config_eol(p, end) && iskeychar(*p); p++) + ; /* skip key name */ + while (!is_config_eol(p, end) && + (*p == ' ' || *p == '\t')) + p++; + if (!is_config_eol(p, end) && *p == '=') { + p++; + in_value = 1; + break; + } + *continued = 0; + *in_quote = 0; + return; + } + if (!in_value) { + *continued = 0; + *in_quote = 0; + return; + } + } + + 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; + } + switch (*p) { + case 't': + case 'b': + case 'n': + case '\\': + case '"': + break; + default: + *continued = 0; + *in_quote = 0; + return; + } + } + } + + *continued = 0; + *in_quote = 0; +} + /* if new_name == NULL, the section is removed instead */ static int repo_config_copy_or_rename_section_in_file( struct repository *r, @@ -3435,7 +3569,7 @@ static int repo_config_copy_or_rename_section_in_file( const char *old_name, const char *new_name, int copy) { - int ret = 0, remove = 0; + int ret = 0, remove = 0, continued = 0, in_quote = 0; char *filename_buf = NULL; struct lock_file lock = LOCK_INIT; int out_fd; @@ -3485,7 +3619,7 @@ static int repo_config_copy_or_rename_section_in_file( while (!strbuf_getwholeline(&buf, config_file, '\n')) { size_t i, length; - int is_section = 0; + int is_section = 0, was_continued = continued; char *output = buf.buf; line_nr++; @@ -3499,7 +3633,9 @@ static int repo_config_copy_or_rename_section_in_file( for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++) ; /* do nothing */ - if (buf.buf[i] == '[') { + update_line_continuation(buf.buf, buf.buf + buf.len, + line_nr == 1, &continued, &in_quote); + if (!was_continued && buf.buf[i] == '[') { /* it's a section */ size_t offset; is_section = 1; @@ -3550,6 +3686,10 @@ static int repo_config_copy_or_rename_section_in_file( } else { strbuf_release(©str); copystr = store_create_section(new_name, &store); + if (output[offset + i]) { + strbuf_addch(©str, '\t'); + strbuf_addstr(©str, output + offset + i); + } } } remove = 0; diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 16bb396b6..d1bf5327f 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -3081,4 +3081,89 @@ test_expect_success POSIXPERM 'refuse to "overwrite" a read-only file' ' ) ' +test_expect_success 'remove-section and rename-section ignore bracketed continuation lines' ' + test_when_finished "rm -f multiline.cfg rename.cfg subbracket.cfg cr.cfg crlf.cfg emptysec.cfg spacesec.cfg raw actual expect" && + cat >multiline.cfg <<-\EOF && + [old] + key = "first \ + [smuggled] \ + # quoted hash \ + evil = yes" ; comment \ + [keep] + ok = true + EOF + cp multiline.cfg rename.cfg && + + git config --file=multiline.cfg --remove-section old && + test_must_fail git config --file=multiline.cfg --get smuggled.evil && + test "$(git config --file=multiline.cfg --get keep.ok)" = true && + + git config --file=rename.cfg --rename-section old renamed && + test_must_fail git config --file=rename.cfg --get smuggled.evil && + git config --file=rename.cfg --get renamed.key >actual && + test_grep "\[smuggled\]" actual && + test "$(git config --file=rename.cfg --get keep.ok)" = true && + + cat >subbracket.cfg <<-\EOF && + [keep "a] = \""] k = v ; \ + [old] + secret = should_be_removed + EOF + git config --file=subbracket.cfg --remove-section old && + test_must_fail git config --file=subbracket.cfg --get old.secret && + test "$(git config --file=subbracket.cfg --get "keep.a] = \".k")" = v && + + printf "[a \"b\rc\"]\rkey=\rv\ralue\\\\\n[smuggled]\n\tevil = yes\n[keep]\n\tok = true\n" >cr.cfg && + git config --file=cr.cfg --rename-section "$(printf "a.b\rc")" renamed && + test_must_fail git config --file=cr.cfg --get smuggled.evil && + git config --file=cr.cfg --get renamed.key >raw && + tr "\r" Q actual && + echo "vQalue[smuggled]" >expect && + test_cmp expect actual && + git config --file=cr.cfg --remove-section renamed && + test_must_fail git config --file=cr.cfg --get smuggled.evil && + test "$(git config --file=cr.cfg --get keep.ok)" = true && + + printf "[old]\r\n\tkey = first \\\\\r\n[smuggled]\r\n\tevil = yes\r\n[keep]\r\n\tok = true\r\n" >crlf.cfg && + git config --file=crlf.cfg --remove-section old && + test_must_fail git config --file=crlf.cfg --get smuggled.evil && + test "$(git config --file=crlf.cfg --get keep.ok)" = true && + + cat >emptysec.cfg <<-\EOF && + [] k = v \ + [old] + secret = gone + EOF + git config --file=emptysec.cfg --remove-section old && + test_grep ! "secret" emptysec.cfg && + + cat >spacesec.cfg <<-\EOF && + [ ] k = v \ + [old] + secret = gone + EOF + git config --file=spacesec.cfg --remove-section old && + test_grep ! "secret" spacesec.cfg && + + test_when_finished "rm -rf copy-repo" && + git init copy-repo && + ( + cd copy-repo && + test_commit init && + git branch src && + cat >>.git/config <<-\EOF && + [branch "src"] note = first \ + [smuggled] + evil = yes + [keep] + ok = true + EOF + git branch -c src dst && + test_must_fail git config --get smuggled.evil && + test "$(git config --get branch.dst.note)" = "first [smuggled]" && + test "$(git config --get branch.dst.evil)" = yes && + test "$(git config --get keep.ok)" = true + ) +' + test_done -- 2.56.1.911.g883d8bcb20-goog