Re: [PATCH 2/2] commit: sign commit after mutating buffer
- From
Elijah Newren <newren@gmail.com>
- Date
- Apr 22, 2026, 15:10 UTC
- Message-ID
- <CABPp-BEd9saiMWVdcSaZBHqGreshpA=fGZc4AvYkoU=swSSuxA@mail.gmail.com>
- In-Reply-To
- <20260420221425.2763661-2-sandals@crustytoothpaste.net>
On Mon, Apr 20, 2026 at 3:14 PM brian m. carlson <sandals@crustytoothpaste.net> wrote:
Show 36 quoted lines
>
> The ensure_utf8 function can mutate the buffer to change its encoding,
> so we must call it before signing the buffer so that we do not
> invalidate the signature, which is made over raw bytes. Add a test for
> this case as well using 0xfe and 0xff, which are never valid in UTF-8.
>
> Reported-by: Kushal Das <kushal@sunet.se>
> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
> ---
> commit.c | 12 ++++++++----
> t/t7510-signed-commit.sh | 8 ++++++++
> 2 files changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/commit.c b/commit.c
> index 790dd2faed..bc41859be1 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1747,6 +1747,11 @@ int commit_tree_extended(const char *msg, size_t msg_len,
> oidcpy(&parent_buf[i++], &p->item->object.oid);
>
> write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
> +
> + /* And check the encoding. */
> + if (encoding_is_utf8 && !ensure_utf8(&buffer))
> + fprintf(stderr, _(commit_utf8_warn));
> +
> if (sign_commit && sign_buffer(&buffer, &sig, sign_commit,
> SIGN_BUFFER_USE_DEFAULT_KEY)) {
> result = -1;
> @@ -1780,6 +1785,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
> free_commit_extra_headers(compat_extra);
> free(mapped_parents);
>
> + if (encoding_is_utf8 && !ensure_utf8(&compat_buffer))
> + fprintf(stderr, _(commit_utf8_warn));
> +So the users might see "commit message did not conform to UTF-8..." twice? (Isn't compat_buffer likely to have invalid UTF-8 whenever buffer does?) Do we want to avoid that double printing?
Show 11 quoted lines
> if (sign_commit && sign_buffer(&compat_buffer, &compat_sig,
> sign_commit,
> SIGN_BUFFER_USE_DEFAULT_KEY)) {
> @@ -1818,10 +1826,6 @@ int commit_tree_extended(const char *msg, size_t msg_len,
> }
> }
>
> - /* And check the encoding. */
> - if (encoding_is_utf8 && (!ensure_utf8(&buffer) || !ensure_utf8(&compat_buffer)))
> - fprintf(stderr, _(commit_utf8_warn));
> -Did the change in this patch also fix a short-circuiting error? Previously, when both buffers had invalid UTF-8, we'd only call ensure_utf8() on the first one and fix it, and then short-circuit and not handle compat_buffer, right?
Show 18 quoted lines
> if (r->compat_hash_algo) {
> hash_object_file(r->compat_hash_algo, compat_buffer.buf, compat_buffer.len,
> OBJ_COMMIT, &compat_oid_buf);
> diff --git a/t/t7510-signed-commit.sh b/t/t7510-signed-commit.sh
> index 1201c85ba6..071dbb3d39 100755
> --- a/t/t7510-signed-commit.sh
> +++ b/t/t7510-signed-commit.sh
> @@ -462,4 +462,12 @@ test_expect_success 'custom `gpg.program`' '
> git commit -S --allow-empty -m signed-commit
> '
>
> +test_expect_success GPG 'commit verifies with non-UTF-8 commit message' '
> + printf "I hate\\376\\377UTF-8\\n" >message &&
> + echo unusual-message >file &&
> + git add file &&
> + test_tick && git commit -S -F message &&
> + git verify-commit HEAD
> +'Nice test.