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

Re: [Patch size_t V3 19/19] Convert xdiff-interface to size_t

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 17, 2017, 17:49 UTC
Message-ID
<xmqqwp62gk3c.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1502914591-26215-20-git-send-email-martin@mail.zuhause>
Martin Koegler <martin.koegler@chello.at> writes:
Show 13 quoted lines
> diff --git a/combine-diff.c b/combine-diff.c
> index acf39ec..ad5d177 100644
> --- a/combine-diff.c
> +++ b/combine-diff.c
> @@ -343,7 +343,7 @@ struct combine_diff_state {
>  	struct sline *lost_bucket;
>  };
>  
> -static void consume_line(void *state_, char *line, unsigned long len)
> +static void consume_line(void *state_, char *line, size_t len)
>  {
>  	struct combine_diff_state *state = state_;
>  	if (5 < len && !memcmp("@@ -", line, 4)) {

This is a correct logical consequence of making consume_fn to take size_t.

Show 10 quoted lines
> diff --git a/diff.c b/diff.c
> index c12d062..f665f8d 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -406,7 +406,7 @@ static struct diff_tempfile {
>  	struct tempfile tempfile;
>  } diff_temp[2];
>  
> -typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
> +typedef size_t (*sane_truncate_fn)(char *line, size_t len);

It turns out that this type, the member of this type in ecb struct, and a conditional call to the function when it is not NULL, are all unused. If this were used, this conversion is correct ;-)

Show 9 quoted lines
> @@ -4536,7 +4536,7 @@ struct patch_id_t {
>  	int patchlen;
>  };
>  
> -static int remove_space(char *line, int len)
> +static size_t remove_space(char *line, size_t len)
>  {
>  	int i;
>  	char *dst = line;

This function may also want to be rewritten. The loop counter and array index "i" is used this way:

	for (i = 0; i < len i++)
		if (!isspace((c = line[i]))
			*dst++ = c;
	return dst - line;

So you are still risking data loss (later parts of line[] may not be scanned due to int possibly being narrower than size_t).

Turning "len" and the return type into size_t is absolutely the right thing to do, and it is tempting to turn "i" also into size_t, but we may just want to scan the thing with another pointer, i.e.

        size_t remove_space(char *line, size_t len)
        {
                char *src = line;
                char *dst = line;
                while (src < line + len) {
                        char c = *src++;
                        if (!isspace(c))
                                *dst++ = c;
                }
                return dst - line;
        }

Changes to size_t from types other than "ulong" is worth looking at twice for a potential issue like this.

Show 13 quoted lines
> diff --git a/xdiff-interface.c b/xdiff-interface.c
> index d82cd4a..f12285d 100644
> --- a/xdiff-interface.c
> +++ b/xdiff-interface.c
> @@ -26,7 +26,7 @@ static int parse_num(char **cp_p, int *num_p)
>  	return 0;
>  }
>  
> -int parse_hunk_header(char *line, int len,
> +int parse_hunk_header(char *line, size_t len,
>  		      int *ob, int *on,
>  		      int *nb, int *nn)
>  {

This is a correct conversion for the purpose of this series, but the implementation of this function before (and after) this patch is already incorrect. It does not pay any attention to "len", and neither the helper function this function uses, parse_num(), is told how many bytes are remaining on the line to be parsed, so it will happily go beyond "len".

The two things we may want to fix is obviously outside the scope of this series, but they should be fixed (not necessarily by you) after this series once the codebase solidifies, or before this series as preliminary clean-up.

Thanks.
Previous: Martin KoeglerNext: Martin Koegler
Message 11 of 33 in “use size_t”
  1. 00/19 use size_tMartin Koegler, Aug 16, 2017
  2. 03/19 Convert zlib.c to size_tMartin Koegler, Aug 16, 2017
  3. 01/19 delta: fix enconding size larger than an "uint" can holdMartin Koegler, Aug 16, 2017
  4. Torsten BögershausenAug 17, 2017
  5. 08/19 Convert fsck.c & commit.c to size_tMartin Koegler, Aug 16, 2017
  6. 11/19 Use size_t for config parsingMartin Koegler, Aug 16, 2017
  7. Johannes SixtAug 24, 2017
  8. 04/19 delta: Fix offset overflowsMartin Koegler, Aug 16, 2017
  9. 12/19 Convert pack-objects to size_tMartin Koegler, Aug 16, 2017
  10. 19/19 Convert xdiff-interface to size_tMartin Koegler, Aug 16, 2017
  11. Junio C HamanoAug 17, 2017
  12. 17/19 Convert ref-filter to size_tMartin Koegler, Aug 16, 2017
  13. Junio C HamanoAug 17, 2017
  14. Junio C HamanoAug 17, 2017
  15. 10/19 Add overflow check to get_delta_hdr_sizeMartin Koegler, Aug 16, 2017
  16. 18/19 Convert tree-walk to size_tMartin Koegler, Aug 16, 2017
  17. Junio C HamanoAug 17, 2017
  18. 16/19 Convert various things to size_tMartin Koegler, Aug 16, 2017
  19. Junio C HamanoAug 21, 2017
  20. 15/19 Convert archive functions to size_tMartin Koegler, Aug 16, 2017
  21. Junio C HamanoAug 21, 2017
  22. brian m. carlsonAug 22, 2017
  23. 07/19 Convert parse_X_buffer to size_tMartin Koegler, Aug 16, 2017
  24. 14/19 Convert unpack-objects to size_tMartin Koegler, Aug 16, 2017
  25. 13/19 Convert index-pack to size_tMartin Koegler, Aug 16, 2017
  26. Ramsay JonesAug 16, 2017
  27. 02/19 Convert size datatype to size_tMartin Koegler, Aug 16, 2017
  28. 09/19 Convert cache functions to size_tMartin Koegler, Aug 16, 2017
  29. 05/19 Convert sha1_file.c to size_tMartin Koegler, Aug 16, 2017
  30. 06/19 Use size_t for sha1Martin Koegler, Aug 16, 2017
  31. Junio C HamanoAug 16, 2017
  32. Torsten BögershausenAug 17, 2017
  33. Martin KoeglerAug 18, 2017

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.