git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/4] add-patch: Fix type missmatch rom msvc

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 27, 2024, 14:31 UTC
Message-ID
<xmqq5xn5urhv.fsf@gitster.g>
In-Reply-To
<e396131c-1bd3-46d0-bae6-cd97ca9710d8@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 19 quoted lines
>>>   struct hunk_header {
>>> -	unsigned long old_offset, old_count, new_offset, new_count;
>>> +	size_t old_offset, old_count, new_offset, new_count;
>> These are not "size"s in the traditional sense of what size_t is
>> (i.e. the number of bytes in a region of memory), but are more or
>> less proportional to that in that they count in number of lines.
>> If ulong is sufficient to count number of lines in an incoming
>> patch, then turning size_t may be excessive---are we sure that we
>> are not unnecessarily using wider-than-necessary size_t in some
>> places to hold these values for which ulong is sufficient, causing
>> compilers to emit unnecessary warning?
>
> That's my thought too - I think something like the diff below should
> fix the warnings by using more appropriate types in expressions
> involving the hunk header offset and count. Our internal diff
> implementation will not generate diffs for blobs greater than ~1GB
> and I don't think "git apply" can handle diff headers that contain
> numbers greater that ULONG_MAX so switching to size_t here seems
> unnecessary.
Yes, exactly.

Of course, when filling old_offset and friends by parsing an input line like this:

    @@ -253,7 +253,7 @@ struct hunk_header {

it would be a bug if we did not check if "253" overflows the type of old_offset, etc. And I would very much welcome patches to fix such a careless input validation routine. But replacing ulong with size_t would not make such a problem go away.

Now, I would be a bit more sympathetic if the patch were to use integers of exact sizes, in the name of "let's make sure that regardless of the platforms we handle patches up to the same limit". But size_t is not a type that is appropriate for that (and of course ulong is not, either---but the original did not aim for such a uniform limit to begin with).

Show 9 quoted lines
> @@ -1626,7 +1628,7 @@ static int patch_update_file(struct add_p_state *s,
>  			else
>  				err(s, Q_("Sorry, only %d hunk available.",
>  					  "Sorry, only %d hunks available.",
> -					  file_diff->hunk_nr),
> +					  (int)file_diff->hunk_nr),
>  				    (int)file_diff->hunk_nr);
>  		} else if (s->answer.buf[0] == '/') {
>  			regex_t regex;

I skimmed your "how about going this way" illustration patch and found all the hunks reasonable, but this one I am not sure. Is there a reason why hunk_nr has to be of type size_t?

When queuing a hunk (and performing an operation that changes the number of hunks, like splitting an existing one), the code should be careful not to make too many hunks to overflow "int" (if that is the more natural type to count them---and "int" being the most natural integer type for the platform, I tend to think it should be fine), again, that applies equally if the type of hunk_nr is "size_t".

Thanks.
Previous: Phillip WoodNext: Sören Krecker
Message 8 of 17 in “Fixes typemissmatch warinigs from msvc”
  1. 0/4 Fixes typemissmatch warinigs from msvcSören Krecker, Dec 23, 2024
  2. 4/4 commit.c: Fix type missmatch warings from msvcSören Krecker, Dec 23, 2024
  3. Junio C HamanoDec 26, 2024
  4. 1/4 add-patch: Fix type missmatch rom msvcSören Krecker, Dec 23, 2024
  5. Junio C HamanoDec 26, 2024
  6. Patrick SteinhardtDec 27, 2024
  7. Phillip WoodDec 27, 2024
  8. Junio C HamanoDec 27, 2024
  9. Sören KreckerDec 27, 2024
  10. Junio C HamanoDec 27, 2024
  11. Phillip WoodDec 28, 2024
  12. 2/4 date.c: Fix type missmatch warings from msvcSören Krecker, Dec 23, 2024
  13. Junio C HamanoDec 26, 2024
  14. 3/4 apply.c : Fix type missmatch warings from msvcSören Krecker, Dec 23, 2024
  15. Junio C HamanoDec 23, 2024
  16. Junio C HamanoDec 23, 2024
  17. Sören KreckerDec 26, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.