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

[PATCH] Avoid errors and warnings when attempting to do I/O on zero bytes

From
EWEric Wong <normalperson@yhbt.net>
Date
Jan 11, 2007, 21:43 UTC
Message-ID
<20070111214340.GA25445@mayonaise.dyndns.org>
In-Reply-To
<45A2699F.5060100@shadowen.org>

Unfortunately, while {read,write}_in_full do take into account zero-sized reads/writes; their die and whine variants do not.

I have a repository where there are zero-sized files in the history that was triggering these things.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 sha1_file.c    |    2 ++
 write_or_die.c |    8 ++++++++
 2 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 53e25f2..18dd89b 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1620,6 +1620,8 @@ static int write_buffer(int fd, const void *buf, size_t len)
 {
 	ssize_t size;
 
+	if (!len)
+		return 0;
 	size = write_in_full(fd, buf, len);
 	if (!size)
 		return error("file write: disk full");
diff --git a/write_or_die.c b/write_or_die.c
index a119e1d..700ccd1 100644
--- a/write_or_die.c
+++ b/write_or_die.c
@@ -26,6 +26,8 @@ void read_or_die(int fd, void *buf, size_t count)
 {
 	ssize_t loaded;
 
+	if (!count)
+		return;
 	loaded = read_in_full(fd, buf, count);
 	if (loaded == 0)
 		die("unexpected end of file");
@@ -59,6 +61,8 @@ void write_or_die(int fd, const void *buf, size_t count)
 {
 	ssize_t written;
 
+	if (!count)
+		return;
 	written = write_in_full(fd, buf, count);
 	if (written == 0)
 		die("disk full?");
@@ -73,6 +77,8 @@ int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
 {
 	ssize_t written;
 
+	if (!count)
+		return 1;
 	written = write_in_full(fd, buf, count);
 	if (written == 0) {
 		fprintf(stderr, "%s: disk full?\n", msg);
@@ -93,6 +99,8 @@ int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
 {
 	ssize_t written;
 
+	if (!count)
+		return 1;
 	written = write_in_full(fd, buf, count);
 	if (written == 0) {
 		fprintf(stderr, "%s: disk full?\n", msg);
-- 
Eric Wong
Previous: Junio C Hamano
Message 10 of 10 in “use xread where we are not checking for EAGAIN/EINTR”
  1. use xread where we are not checking for EAGAIN/EINTRAndy Whitcroft, Jan 5, 2007
  2. Junio C HamanoJan 5, 2007
  3. Andy WhitcroftJan 5, 2007
  4. Andy WhitcroftJan 8, 2007
  5. 1/4 short i/o: clean up the naming for the write_{in,or}_xxx familyAndy Whitcroft, Jan 8, 2007
  6. 2/4 short i/o: fix calls to read to use xread or read_in_fullAndy Whitcroft, Jan 8, 2007
  7. 3/4 short i/o: fix calls to write to use xwrite or write_in_fullAndy Whitcroft, Jan 8, 2007
  8. 4/4 short i/o: fix config updates to use write_in_fullAndy Whitcroft, Jan 8, 2007
  9. Junio C HamanoJan 8, 2007
  10. Avoid errors and warnings when attempting to do I/O on zero bytesEric Wong, Jan 11, 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.