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

Re: [PATCH] fix recursive-merge of empty files with different permissions

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Mar 13, 2008, 21:28 UTC
Message-ID
<alpine.LSU.1.00.0803132227510.4174@racer.site>
In-Reply-To
<7vabl2xxun.fsf@gitster.siamese.dyndns.org>
Hi,
On Thu, 13 Mar 2008, Junio C Hamano wrote:
Show 27 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > diff --git a/builtin-merge-file.c b/builtin-merge-file.c
> > index adce6d4..cde4b2c 100644
> > --- a/builtin-merge-file.c
> > +++ b/builtin-merge-file.c
> > @@ -57,7 +57,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
> >  
> >  		if (!f)
> >  			ret = error("Could not open %s for writing", filename);
> > -		else if (fwrite(result.ptr, result.size, 1, f) != 1)
> > +		else if (result.size &&
> > +				fwrite(result.ptr, result.size, 1, f) != 1)
> >  			ret = error("Could not write to %s", filename);
> >  		else if (fclose(f))
> >  			ret = error("Could not close %s", filename);
> 
> Lol.  We are dealing with N-byte quantity so we send one record of length
> N and make sure we processed one record, and it does not work when N is
> zero.
> 
> We could instead send N records of size 1 and make sure we processed N
> records to lose the conditional instead, but the patch avoids unnecessary
> call to fread/fwrite so that is good.  Thanks.
> 
> It felt funny because my current bedtime reading happens to be "Zero: The
> Biography of a Dangerous Idea (ISBN 0140296476)".
Heh.
Show 29 quoted lines
> > diff --git a/xdiff-interface.c b/xdiff-interface.c
> > index bba2364..61dc5c5 100644
> > --- a/xdiff-interface.c
> > +++ b/xdiff-interface.c
> > @@ -152,8 +152,8 @@ int read_mmfile(mmfile_t *ptr, const char *filename)
> >  	if ((f = fopen(filename, "rb")) == NULL)
> >  		return error("Could not open %s", filename);
> >  	sz = xsize_t(st.st_size);
> > -	ptr->ptr = xmalloc(sz);
> > -	if (fread(ptr->ptr, sz, 1, f) != 1)
> > +	ptr->ptr = xmalloc(sz ? sz : 1);
> > +	if (sz && fread(ptr->ptr, sz, 1, f) != 1)
> >  		return error("Could not read %s", filename);
> >  	fclose(f);
> >  	ptr->size = sz;
> 
> Do you need to actually allocate ptr->ptr when sz is zero, instead of
> setting it to NULL, like:
> 
> 	sz = xsize_t(st.st_size);
> 	ptr->size = sz;
>         if (!sz)
>         	ptr->ptr = NULL;
> 	else {
>         	ptr->ptr = xmalloc(sz);
> 		if (fread(ptr->ptr, 1, sz, f) != sz)
> 			return error("Could not read %s", filename);
> 	}
> 	fclose(f);

I was going for the safe option. In theory, you are right, but I cannot really be sure that e.g. a memcpy() with size 0 will not access the source pointer at all.

Ciao, Dscho

Previous: Junio C HamanoNext: Clemens Buchacher
Message 6 of 23 in “fix recursive-merge of empty files with different permissions”
  1. fix recursive-merge of empty files with different permissionsClemens Buchacher, Mar 8, 2008
  2. Johannes SchindelinMar 8, 2008
  3. Clemens BuchacherMar 13, 2008
  4. Johannes SchindelinMar 13, 2008
  5. Junio C HamanoMar 13, 2008
  6. Johannes SchindelinMar 13, 2008
  7. merge-recursive: cause a conflict if file mode does not matchClemens Buchacher, Mar 13, 2008
  8. Johannes SchindelinMar 13, 2008
  9. merge-recursive: handle file mode changesClemens Buchacher, Mar 13, 2008
  10. Johannes SchindelinMar 13, 2008
  11. merge-recursive: handle file mode and links similarly to file contentClemens Buchacher, Mar 14, 2008
  12. Clemens BuchacherMar 14, 2008
  13. Junio C HamanoMar 14, 2008
  14. Junio C HamanoMar 14, 2008
  15. Clemens BuchacherMar 14, 2008
  16. Jakub NarebskiMar 14, 2008
  17. Clemens BuchacherMar 14, 2008
  18. Junio C HamanoMar 14, 2008
  19. Clemens BuchacherMar 14, 2008
  20. Junio C HamanoMar 14, 2008
  21. Clemens BuchacherMar 14, 2008
  22. Junio C HamanoMar 14, 2008
  23. Clemens BuchacherMar 14, 2008

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.