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

Fix up totally buggered read_or_die()

From
Linus Torvalds <torvalds@osdl.org>
Date
Jan 12, 2007, 04:37 UTC
Message-ID
<Pine.LNX.4.64.0701112033490.3594@woody.osdl.org>

The "read_or_die()" function would silently NOT die for a partial read, and since it was of type "void" it obviously couldn't even return the partial number of bytes read.

IOW, it was totally broken. This hopefully fixes it up.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

I'm really quite fed up with the "cleanups" that were just too buggy for words.

diff --git a/write_or_die.c b/write_or_die.c
index 1224cac..4e8183e 100644
--- a/write_or_die.c
+++ b/write_or_die.c
@@ -4,16 +4,11 @@ int read_in_full(int fd, void *buf, size_t count)
 {
 	char *p = buf;
 	ssize_t total = 0;
-	ssize_t loaded = 0;
 
 	while (count > 0) {
-		loaded = xread(fd, p, count);
-		if (loaded <= 0) {
-			if (total)
-				return total;
-			else
-				return loaded;
-		}
+		ssize_t loaded = xread(fd, p, count);
+		if (loaded <= 0)
+			return total ? total : loaded;
 		count -= loaded;
 		p += loaded;
 		total += loaded;
@@ -26,13 +21,12 @@ 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");
-	else if (loaded < 0)
-		die("read error (%s)", strerror(errno));
+	if (loaded != count) {
+		if (loaded < 0)
+			die("read error (%s)", strerror(errno));
+		die("read error: end of file");
+	}
 }
 
 int write_in_full(int fd, const void *buf, size_t count)
Message 1 of 1 in “Fix up totally buggered read_or_die()”
  1. Linus TorvaldsJan 12, 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.