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

Re: Changes in function read_pipe

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 16, 2007, 23:50 UTC
Message-ID
<7vfy3nc3h9.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<469B9939.5050800@gmail.com>
Carlos Rica <jasampler@gmail.com> writes:
Show 6 quoted lines
> Some people talked recently about renaming the function read_pipe
> (sha1_file.c) to the better name read_fd.
> Here I discuss other possible changes to the current version:
>
> 1. It now requires to allocate memory for the buffer before calling it,
> you cannot pass it a pointer set to NULL or not initializated at all.

It should be a trivial and easy fix to add "initial allocation" at the beginning of the function. While I suspect that the optimal initial allocation would be different for different callers, probably we would not care that much.

> 2. The function doesn't terminate the data with NUL, and if you
> need that, you must to realloc before adding the '\0', because
> the function only returns the size of the data, not the buffer size.

Among the existing callers, index_fd() does not need NUL termination, but we are almost always allocating more than needed when the final size is unknown, so giving an extra NUL (of course not including that in the final result size) would not hurt. Other callers are 'stripspace' and 'mktag', and I would imagine 'tag' and 'commit' when they are reading the message from the standard input would benefit from the implicit NUL termination. I'd say go for it.

> 3. When function fails in reading (xread returns < 0), buffer is not
> freed.

I do not think this is a problem. The current callers seem to do something like this:

	if (read_pipe(fd, ...)) {
        	free(buf);
                return error(...);
	}

but not freeing, and reporting how many you read so far, would allow readers to work on results partially read (not to be confused with the EAGAIN which is already handled with xread()).

Previous: Carlos Rica
Message 2 of 2 in “Changes in function read_pipe”
  1. Carlos RicaJul 16, 2007
  2. Junio C HamanoJul 16, 2007

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.