All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Fernando Gouveia Lima <fernandolimabusiness@gmail.com>
Cc: git@vger.kernel.org, Patrick Steinhardt <ps@pks.im>,
	Christian Couder <chriscool@tuxfamily.org>,
	fernandolimabusiness@gmail.com,  stolee@gmail.com,
	 peff@peff.net
Subject: Re: [Newcomer PATCH] log-tree.c: Supress Wsign-compare-warning
Date: Wed, 21 May 2025 14:08:40 -0700	[thread overview]
Message-ID: <xmqqsekx8yef.fsf@gitster.g> (raw)
In-Reply-To: <20250521202409.26879-1-fernandolimabusiness@gmail.com> (Fernando Gouveia Lima's message of "Wed, 21 May 2025 17:24:09 -0300")

Fernando Gouveia Lima fernandolimabusiness@gmail.com writes:

> @@ -151,7 +151,7 @@ static int add_ref_decoration(const char *refname, const char *referent UNUSED,
>  			      int flags UNUSED,
>  			      void *cb_data)
>  {
> -	int i;
> +	long unsigned int i;
>  	struct object *obj;
>  	enum object_type objtype;
>  	enum decoration_type deco_type = DECORATION_NONE;

The complaint is about this line of code:

	for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {

where ref_namespace array is of NAMESPACE__COUNT size, which is the
last enum in "enum ref_namespace".  Anybody can tell it is a small
integer (like 9) that would fit comfotably well within the platform
natural "int" on anybody that can run Git.

ARRAY_SIZE() is defined in turns of sizeof(), so its type is "size_t";
it cannot be narrower than "int", but that is immaterial.  Comparing
'i' that counts up from 0 will never cause a problem.

And the compiler ought to know all of the above, or should stay
silent.

The compiler is wrong in this case.  As I already have said on this
list a few times, -Wsign-compare is often garbage and we should not
bend over backwards to butcher perfectly good code only to silence
its false positives.

> @@ -458,7 +458,7 @@ void fmt_output_subject(struct strbuf *filename,
>  	}
>  	strbuf_addf(filename, "%04d-%s", nr, subject);
>  
> -	if (max_len < filename->len)
> +	if (max_len < (int) filename->len)
>  		strbuf_setlen(filename, max_len);

This conversion is wrong, even if your compiler is made silent with
this change.  The function begins like this:

        void fmt_output_subject(struct strbuf *filename,
                                const char *subject,
                                struct rev_info *info)
        {
                const char *suffix = info->patch_suffix;
                int nr = info->nr;
                int start_len = filename->len;
                int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);

The filename variable is a pointer to a strbuf, whose .len member is
of type size_t, which can be wider than a platform natural "int".
Assigning it to "int start_len" risks overflowing and wrapping
around, and "int max_len" is also wrong.

So casting a large filename->len that would not fit within platform
natural "int" may make your compiler silent when you compare it with
max_len that is also platform natural "int".  But at that point,
what are you comparing with what?  Imagine on a platform with 32-bit
int and when the actual value of filename->len is very very long and
longer than say 3 billion bytes.

In this function, I think a more reasonable thing to do is to
express various length invoved in size_t, and make sure computations
among them does not over/underflow.

	Side note: Those who are maintaining GSoC microproject ideas
	page.  This is the second time in a few days that I had to
	look at a patch that makes the code worse by blindly trying
	to squelch the -Wsign-compare warnings.  Can you please take
	that out of the list of suggested tasks?  Thanks.

Fernando, please do not get discouraged by _our_ code being sloppy
and GSoC microproject ideas page being under-specified.  Neither is
your fault.

And welcome to Git development community.


[Footnote]

Quite honestly, -Wsign-compare is mostly garbage [*] and I wish we
did not add it to the developer settings.  A more effective way to
squelch them is not by sprinkling the casts like this, but to remove
it from config.mak.dev ;-)

https://staticthinking.wordpress.com/2023/07/25/wsign-compare-is-garbage/

  reply	other threads:[~2025-05-21 21:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-21 20:24 [Newcomer PATCH] log-tree.c: Supress Wsign-compare-warning Fernando
2025-05-21 21:08 ` Junio C Hamano [this message]
2025-05-23 15:12   ` Patrick Steinhardt
2025-05-23 15:25     ` Taylor Blau
2025-05-23 16:54     ` Junio C Hamano
2025-05-30  8:13       ` Patrick Steinhardt

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=xmqqsekx8yef.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=chriscool@tuxfamily.org \
    --cc=fernandolimabusiness@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    --cc=stolee@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.