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

Re: [Possible Bug] Use of write on size-limited platforms

From
Jeff King <peff@peff.net>
Date
Jun 15, 2020, 21:59 UTC
Message-ID
<20200615215937.GA636737@coredump.intra.peff.net>
In-Reply-To
<015c01d63ddd$97d65080$c782f180$@nexbridge.com>
On Mon, Jun 08, 2020 at 05:41:34PM -0400, Randall S. Becker wrote:
Show 7 quoted lines
> I just wanted to check the following calls to make sure that it does not
> fwrite or write should be xread/xwrite or are guaranteed not to exceed
> MAX_IO_SIZE:
> 
> strbuf.c: strbuf_write, strbuf_write_fd, the size is not specified.
> 
> The other uses of read/write appear to be safe.

strbuf_write() is using fwrite(), and we don't enforce MAX_IO_SIZE with stdio anywhere else. And I'd expect in general that if there are any platform limitations, the system libc would choose a sane value anyway. So that one is probably fine.

I think strbuf_write_fd() is wrong to use a raw write(), but for several reasons:

 - it won't enforce MAX_IO_SIZE, as you note
 - it won't handle EINTR, etc; callers need to be prepared to restart
   such a write
 - it won't handle a partial write by looping until all output is sent

For the latter two, there are cases where some callers want the flexibility to stop when seeing a signal or a partial write. But I don't think that makes any sense for strbuf_write_fd(). If I pass in a strbuf with 8kb of data and I get a return value that indicates we only wrote 4kb, what do I do next? I certainly can't call strbuf_write_fd() again, since it would write from the beginning of the strbuf again. I'd have to call xwrite() myself after that. At which point I may as well have done so for the first call. :)

So I think this really ought to be using write_in_full(). There's only one caller, and I think it would be improved by the switch. Do you want to write a patch?

You could make an argument that the fwrite() version ought to also loop, since it's possible to get a partial write there, too. But we don't do that in general. I suspect in practice most stdio implementations will keep writing until they see an error, and most callers don't bother checking stdio errors at all, or use ferror().

-Peff
Previous: Randall S. BeckerNext: Randall S. Becker
Message 2 of 6 in “[Possible Bug] Use of write on size-limited platforms”
  1. Randall S. BeckerJun 8, 2020
  2. Jeff KingJun 15, 2020
  3. Randall S. BeckerJun 15, 2020
  4. Jeff KingJun 16, 2020
  5. Randall S. BeckerJun 19, 2020
  6. Junio C HamanoJun 19, 2020

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.