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

[PATCH 5/7] pkt-line: check write_in_full() errors against "< 0"

From
Jeff King <peff@peff.net>
Date
Sep 13, 2017, 17:17 UTC
Message-ID
<20170913171730.ckcebchjhhvtwcsj@sigill.intra.peff.net>
In-Reply-To
<20170913170807.cyx7rrpoyhaauvol@sigill.intra.peff.net>

As with the previous two commits, we prefer to check write_in_full()'s return value to see if it is negative, rather than comparing it to the input length.

These cases actually flip the logic to check for success, making conversion a little different than in other cases. We could of course write:

  if (write_in_full(...) >= 0)
          return 0;
  return error(...);

But our usual method of spelling write() error checks is just "< 0". So let's flip the logic for each of these conditionals to our usual style.

Signed-off-by: Jeff King <peff@peff.net>
---
This will have a minor textual conflict with ma/pkt-line-leakfix which
hasn't quite made it into master yet. The resolution should be
straight-forward, though.
 pkt-line.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/pkt-line.c b/pkt-line.c
index 7db9119573..4823d3bb9d 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -94,9 +94,9 @@ void packet_flush(int fd)
 int packet_flush_gently(int fd)
 {
 	packet_trace("0000", 4, 1);
-	if (write_in_full(fd, "0000", 4) == 4)
-		return 0;
-	return error("flush packet write failed");
+	if (write_in_full(fd, "0000", 4) < 0)
+		return error("flush packet write failed");
+	return 0;
 }
 
 void packet_buf_flush(struct strbuf *buf)
@@ -137,18 +137,17 @@ static int packet_write_fmt_1(int fd, int gently,
 			      const char *fmt, va_list args)
 {
 	struct strbuf buf = STRBUF_INIT;
-	ssize_t count;
 
 	format_packet(&buf, fmt, args);
-	count = write_in_full(fd, buf.buf, buf.len);
-	if (count == buf.len)
-		return 0;
-
-	if (!gently) {
-		check_pipe(errno);
-		die_errno("packet write with format failed");
+	if (write_in_full(fd, buf.buf, buf.len) < 0) {
+		if (!gently) {
+			check_pipe(errno);
+			die_errno("packet write with format failed");
+		}
+		return error("packet write with format failed");
 	}
-	return error("packet write with format failed");
+
+	return 0;
 }
 
 void packet_write_fmt(int fd, const char *fmt, ...)
@@ -183,9 +182,9 @@ static int packet_write_gently(const int fd_out, const char *buf, size_t size)
 	packet_size = size + 4;
 	set_packet_header(packet_write_buffer, packet_size);
 	memcpy(packet_write_buffer + 4, buf, size);
-	if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
-		return 0;
-	return error("packet write failed");
+	if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
+		return error("packet write failed");
+	return 0;
 }
 
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
-- 
2.14.1.874.ge7b2e05270
Previous: Jonathan NiederNext: Jonathan Nieder
Message 37 of 46 in “Bug: git branch --unset-upstream command can nuke config when disk is full.”
  1. demerphqSep 13, 2017
  2. Jeff KingSep 13, 2017
  3. demerphqSep 13, 2017
  4. Jeff KingSep 13, 2017
  5. demerphqSep 13, 2017
  6. Jeff KingSep 13, 2017
  7. demerphqSep 13, 2017
  8. Jeff KingSep 13, 2017
  9. demerphqSep 13, 2017
  10. 0/7 config.c may fail to notice some write() failuresJeff King, Sep 13, 2017
  11. 1/7 config: avoid "write_in_full(fd, buf, len) < len" patternJeff King, Sep 13, 2017
  12. Jonathan NiederSep 13, 2017
  13. Jeff KingSep 13, 2017
  14. Jonathan NiederSep 13, 2017
  15. Jeff KingSep 13, 2017
  16. config: avoid "write_in_full(fd, buf, len) < len" patternJeff King, Sep 13, 2017
  17. Jonathan NiederSep 13, 2017
  18. Jeff KingSep 13, 2017
  19. Jonathan NiederSep 13, 2017
  20. Jonathan NiederSep 13, 2017
  21. Ramsay JonesSep 13, 2017
  22. Ramsay JonesSep 13, 2017
  23. Jeff KingSep 15, 2017
  24. Ramsay JonesSep 15, 2017
  25. Junio C HamanoSep 13, 2017
  26. 2/7 get-tar-commit-id: check write_in_full() return against 0Jeff King, Sep 13, 2017
  27. Jonathan NiederSep 13, 2017
  28. Jeff KingSep 13, 2017
  29. Jeff KingSep 13, 2017
  30. Jonathan NiederSep 13, 2017
  31. Jeff KingSep 15, 2017
  32. 3/7 avoid "write_in_full(fd, buf, len) != len" patternJeff King, Sep 13, 2017
  33. Jonathan NiederSep 13, 2017
  34. Jeff KingSep 15, 2017
  35. 4/7 convert less-trivial versions of "write_in_full() != len"Jeff King, Sep 13, 2017
  36. Jonathan NiederSep 13, 2017
  37. 5/7 pkt-line: check write_in_full() errors against "< 0"Jeff King, Sep 13, 2017
  38. Jonathan NiederSep 13, 2017
  39. 6/7 notes-merge: use ssize_t for write_in_full() return valueJeff King, Sep 13, 2017
  40. Jonathan NiederSep 13, 2017
  41. Jeff KingSep 15, 2017
  42. 7/7 config: flip return value of store_write_*()Jeff King, Sep 13, 2017
  43. Jonathan NiederSep 13, 2017
  44. Jeff KingSep 15, 2017
  45. 8/7 read_pack_header: handle signed/unsigned comparison in read resultJeff King, Sep 13, 2017
  46. Jonathan NiederSep 13, 2017

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.