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

Re: [PATCH 3/2] apply: use COPY_ARRAY and MOVE_ARRAY in update_image()

From
René Scharfe <l.s.r@web.de>
Date
Jul 16, 2017, 04:04 UTC
Message-ID
<cfc122ac-bd43-99f4-395d-2607fc34370c@web.de>
In-Reply-To
<636e2b70-2687-a925-e52d-32aa966ae9a9@ramsayjones.plus.com>
Am 16.07.2017 um 02:31 schrieb Ramsay Jones:
Show 49 quoted lines
> 
> 
> On 15/07/17 21:20, René Scharfe wrote:
>> Simplify the code by using the helper macros COPY_ARRAY and MOVE_ARRAY,
>> which also makes them more robust in the case we copy or move no lines,
>> as they allow using NULL points in that case, while memcpy(3) and
>> memmove(3) don't.
>>
>> Found with Clang's UBSan.
>>
>> Signed-off-by: Rene Scharfe <l.s.r@web.de>
>> ---
>> I don't know why the rules in contrib/coccinelle/array.cocci didn't
>> match. :-?
>>
>>   apply.c | 11 ++++-------
>>   1 file changed, 4 insertions(+), 7 deletions(-)
>>
>> diff --git a/apply.c b/apply.c
>> index f2d599141d..40707ca50c 100644
>> --- a/apply.c
>> +++ b/apply.c
>> @@ -2809,13 +2809,10 @@ static void update_image(struct apply_state *state,
>>   		img->line_allocated = img->line;
>>   	}
>>   	if (preimage_limit != postimage->nr)
>> -		memmove(img->line + applied_pos + postimage->nr,
>> -			img->line + applied_pos + preimage_limit,
>> -			(img->nr - (applied_pos + preimage_limit)) *
>> -			sizeof(*img->line));
>> -	memcpy(img->line + applied_pos,
>> -	       postimage->line,
>> -	       postimage->nr * sizeof(*img->line));
>> +		MOVE_ARRAY(img->line + applied_pos + postimage->nr,
>> +			   img->line + applied_pos + preimage_limit,
>> +			   img->nr - (applied_pos + preimage_limit));
>> +	COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
> 
> My patch looks like:
> 
> -       memcpy(img->line + applied_pos,
> -              postimage->line,
> -              postimage->nr * sizeof(*img->line));
> +       if (postimage->line && postimage->nr)
> +               memcpy(img->line + applied_pos,
> +                      postimage->line,
> +                      postimage->nr * sizeof(*img->line));
> 
> ... which I think I prefer (slightly).

Can postimage->line be NULL when postimage->nr is bigger than 0? What would that mean? The only ways to arrive at that point that I an come up with are bugs (we accidentally set ->line to NULL, or we forgot to clean ->line). We'd better notice them early by getting a nice shrieking segfault. Adding an assert would work as well.

René
Previous: Ramsay JonesNext: Jeff King
Message 5 of 6 in “add MOVE_ARRAY”
  1. 1/2 add MOVE_ARRAYRené Scharfe, Jul 15, 2017
  2. 2/2 use MOVE_ARRAYRené Scharfe, Jul 15, 2017
  3. 3/2 apply: use COPY_ARRAY and MOVE_ARRAY in update_image()René Scharfe, Jul 15, 2017
  4. Ramsay JonesJul 16, 2017
  5. René ScharfeJul 16, 2017
  6. Jeff KingJul 16, 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.