Volume XXII, number 279Tuesday, October 6, 2026Latest message 39 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchsubmodule--helper: avoid use of %zu for now

3 messages between Jul 15, 2026 and Jul 16, 2026, from Junio C Hamano, Adrian Ratiu.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

Junio C HamanoJul 15, 2026, 20:10 UTC on lore

Since d7d850e2b9 (CodingGuidelines: mention C99 features we can't use, 2022-10-10), our CodingGuidelines document has explicitly forbidden the use of '%z' and '%zu' printf() format specifiers, even though C99 does support them. However, a new instance crept in via 82c36fa0a9 (submodule: hash the submodule name for the gitdir path, 2026-01-12).

We could claim that this is an unintentional weather balloon that nobody has complained about for the past six months since Git 2.54, proving that it is now safe to use these format specifiers. But (1) it is probably too early to make that claim, as distributions often stick to a stale version for several releases, and (2) it is unlikely that a failure in this code path would manifest as a major user-visible breakage that would trigger a failure report to percolate down to us.

Instead, let's stick to the established workaround recommended by our CodingGuidelines, which is to cast the value to (uintmax_t) and format it with PRIuMAX, at least for now. Even if we eventually perform a bulk update using a Coccinelle script to transition to %z and %zu in the future, adding one more instance to the pile that will need such a conversion is hardly a tragedy.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/submodule--helper.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Show changes to diff +2 −1
diff --git i/builtin/submodule--helper.c w/builtin/submodule--helper.c
index 1cc82a134d..92e38106c1 100644
--- i/builtin/submodule--helper.c
+++ w/builtin/submodule--helper.c
@@ -549,7 +549,8 @@ static void create_default_gitdir_config(const char *submodule_name)
 	}
 
 	/* Case 2.4: If all the above failed, try a hash of the name as a last resort */
-	header_len = snprintf(header, sizeof(header), "blob %zu", strlen(submodule_name));
+	header_len = snprintf(header, sizeof(header),
+			      "blob %"PRIuMAX, (uintmax_t)strlen(submodule_name));
 	the_hash_algo->init_fn(&ctx);
 	the_hash_algo->update_fn(&ctx, header, header_len);
 	the_hash_algo->update_fn(&ctx, "\0", 1);
Adrian RatiuJul 16, 2026, 12:40 UTC in reply to Junio C Hamano on lore

Re: [PATCH] submodule--helper: avoid use of %zu for now

On Wed, 15 Jul 2026, Junio C Hamano <gitster@pobox.com> wrote:
Show 42 quoted lines
> Since d7d850e2b9 (CodingGuidelines: mention C99 features we can't
> use, 2022-10-10), our CodingGuidelines document has explicitly
> forbidden the use of '%z' and '%zu' printf() format specifiers,
> even though C99 does support them.  However, a new instance crept
> in via 82c36fa0a9 (submodule: hash the submodule name for the
> gitdir path, 2026-01-12).
>
> We could claim that this is an unintentional weather balloon that
> nobody has complained about for the past six months since Git 2.54,
> proving that it is now safe to use these format specifiers.  But
> (1) it is probably too early to make that claim, as distributions
> often stick to a stale version for several releases, and (2) it is
> unlikely that a failure in this code path would manifest as a
> major user-visible breakage that would trigger a failure report to
> percolate down to us.
>
> Instead, let's stick to the established workaround recommended by
> our CodingGuidelines, which is to cast the value to (uintmax_t) and
> format it with PRIuMAX, at least for now.  Even if we eventually
> perform a bulk update using a Coccinelle script to transition to %z
> and %zu in the future, adding one more instance to the pile that
> will need such a conversion is hardly a tragedy.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  builtin/submodule--helper.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git i/builtin/submodule--helper.c w/builtin/submodule--helper.c
> index 1cc82a134d..92e38106c1 100644
> --- i/builtin/submodule--helper.c
> +++ w/builtin/submodule--helper.c
> @@ -549,7 +549,8 @@ static void create_default_gitdir_config(const char *submodule_name)
>  	}
>  
>  	/* Case 2.4: If all the above failed, try a hash of the name as a last resort */
> -	header_len = snprintf(header, sizeof(header), "blob %zu", strlen(submodule_name));
> +	header_len = snprintf(header, sizeof(header),
> +			      "blob %"PRIuMAX, (uintmax_t)strlen(submodule_name));
>  	the_hash_algo->init_fn(&ctx);
>  	the_hash_algo->update_fn(&ctx, header, header_len);
>  	the_hash_algo->update_fn(&ctx, "\0", 1);
LGTM and sorry for not following the guideline. :)
Junio C HamanoJul 16, 2026, 13:18 UTC in reply to Adrian Ratiu on lore

Re: [PATCH] submodule--helper: avoid use of %zu for now

Adrian Ratiu <adrian.ratiu@collabora.com> writes:
>
>
> LGTM and sorry for not following the guideline. :)
Thanks, and no worries.  We all make mistakes.

Back to recent threads