Re: [PATCH v2] bulk-checkin: fix sign compare warnings
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 23, 2025, 22:08 UTC
- Message-ID
- <xmqqo6xrqu2k.fsf@gitster.g>
- In-Reply-To
- <20250321221404.10727-1-taahol@utu.fi>
Tuomas Ahola <taahol@utu.fi> writes:
Show 5 quoted lines
> In file bulk-checkin.c, three warnings are emitted by > "-Wsign-compare", two of which are caused by trivial loop iterator > type mismatches. The third one is also an uncomplicated case for > which a simple cast is a safe and sufficient action as the variable in > question only holds positive values (from sizeof() expression).
The point of the sign-compare is that a positive value that is assigned to a signed variable may wrap around to become negative, causing a comparison with an unsigned type with the same size to fail.
So "only holds positive" is not a good enough explanation for the reason why this workaround for the "-Wsign-compare" false-positive [*] does not make things too bad. The key thing is that the value assigned to this "ssize_t rsize" variable is a small non-negative value that can fit both size_t and ssize_t.
[Footnote]
* If we take -Wsign-compare too literally, it is warning every time a signed quantity and an unsigned quantity is being compared, so we could argue that there is no false-positive. But that is an obviously pretty useless warning, when we can trivially tell that the value in a signed variable cannot have wrapped around.