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

[PATCH] Do not use errno when pread() returns 0

From
Samuel Tardieu <sam@rfc1149.net>
Date
Oct 6, 2008, 17:28 UTC
Message-ID
<1223314121-21528-1-git-send-email-sam@rfc1149.net>

If we use pread() while at the end of the file, it will return 0, which is not an error from the operating system point of view. In this case, errno has not been set and must not be used.

Signed-off-by: Samuel Tardieu <sam@rfc1149.net>
---
 index-pack.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/index-pack.c b/index-pack.c
index 2e4c088..73860bf 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -365,8 +365,11 @@ static void *get_data_from_pack(struct object_entry *obj)
 	data = src;
 	do {
 		ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
-		if (n <= 0)
+		if (n < 0)
 			die("cannot pread pack file: %s", strerror(errno));
+		if (!n)
+			die("premature end of pack file, %lu bytes missing",
+			    len - rdy);
 		rdy += n;
 	} while (rdy < len);
 	data = xmalloc(obj->size);
-- 
tg: (395ff9b..) short-pread (depends on: spearce/next)
Message 1 of 1 in “Do not use errno when pread() returns 0”
  1. Do not use errno when pread() returns 0Samuel Tardieu, Oct 6, 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.