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

[PATCH] strbuf_read: skip unnecessary strbuf_grow at eof

From
Jim Hill <gjthill@gmail.com>
Date
May 31, 2015, 18:16 UTC
Message-ID
<1433096205-14516-1-git-send-email-gjthill@gmail.com>

Make strbuf_read not try to do read_in_full's job too. If xread returns less than was requested it can be either eof or an interrupted read. If read_in_full returns less than was requested, it's eof. Use read_in_full to detect eof and not iterate when eof has been seen.

Signed-off-by: Jim Hill <gjthill@gmail.com>
---
 strbuf.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 88cafd4..c70733e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -364,19 +364,19 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 
 	strbuf_grow(sb, hint ? hint : 8192);
 	for (;;) {
-		ssize_t cnt;
+		ssize_t want = sb->alloc - sb->len - 1;
+		ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
 
-		cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
-		if (cnt < 0) {
+		if (got < 0) {
 			if (oldalloc == 0)
 				strbuf_release(sb);
 			else
 				strbuf_setlen(sb, oldlen);
 			return -1;
 		}
-		if (!cnt)
+		sb->len += got;
+		if (got < want)
 			break;
-		sb->len += cnt;
 		strbuf_grow(sb, 8192);
 	}
 
-- 
2.4.1.4.gfc728c2
Next: Jeff King
Message 1 of 3 in “strbuf_read: skip unnecessary strbuf_grow at eof”
  1. strbuf_read: skip unnecessary strbuf_grow at eofJim Hill, May 31, 2015
  2. Jeff KingJun 1, 2015
  3. Junio C HamanoJun 1, 2015

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.