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

[PATCH 2/2] sequencer.c: check return value of close() in rewrite_file()

From
Simon Ruderich <simon@ruderich.org>
Date
Nov 1, 2017, 14:45 UTC
Message-ID
<06c33d3cfa35c0524ede2970ee3169d6c62eb5c1.1509547231.git.simon@ruderich.org>
In-Reply-To
<22afeefa-cdd5-cd32-0a7c-6bad4de79f05@web.de>

Not checking close(2) can hide errors as not all errors are reported during the write(2).

Signed-off-by: Simon Ruderich <simon@ruderich.org>
---
On Wed, Nov 01, 2017 at 02:00:11PM +0100, René Scharfe wrote:
> Most calls are not checked, but that doesn't necessarily mean they need
> to (or should) stay that way.  The Linux man-page of close(2) spends
> multiple paragraphs recommending to check its return value..  Care to
> send a follow-up patch?
Hello,
Sure, here is it.

Regards Simon

 sequencer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/sequencer.c b/sequencer.c
index f93b60f61..e0cc2f777 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2673,7 +2673,8 @@ static int rewrite_file(const char *path, const char *buf, size_t len)
 		return error_errno(_("could not open '%s' for writing"), path);
 	if (write_in_full(fd, buf, len) < 0)
 		rc = error_errno(_("could not write to '%s'"), path);
-	close(fd);
+	if (close(fd) && !rc)
+		rc = error_errno(_("could not close '%s'"), path);
 	return rc;
 }
 
-- 
2.15.0

-- 
+ privacy is necessary
+ using gnupg http://gnupg.org
+ public key id: 0x92FEFDB7E44C32F9
Previous: Junio C HamanoNext: René Scharfe
Message 14 of 35 in “sequencer: factor out rewrite_file()”
  1. 1/2 sequencer: factor out rewrite_file()René Scharfe, Oct 31, 2017
  2. 2/2 sequencer: use O_TRUNC to truncate filesRené Scharfe, Oct 31, 2017
  3. Kevin DaudtOct 31, 2017
  4. Johannes SchindelinNov 1, 2017
  5. Kevin DaudtOct 31, 2017
  6. Kevin DaudtNov 1, 2017
  7. Simon RuderichNov 1, 2017
  8. René ScharfeNov 1, 2017
  9. 1/2 wrapper.c: consistently quote filenames in error messagesSimon Ruderich, Nov 1, 2017
  10. Junio C HamanoNov 2, 2017
  11. Junio C HamanoNov 2, 2017
  12. Simon RuderichNov 2, 2017
  13. Junio C HamanoNov 3, 2017
  14. 2/2 sequencer.c: check return value of close() in rewrite_file()Simon Ruderich, Nov 1, 2017
  15. René ScharfeNov 1, 2017
  16. Johannes SchindelinNov 1, 2017
  17. Jeff KingNov 1, 2017
  18. Johannes SchindelinNov 1, 2017
  19. Jeff KingNov 1, 2017
  20. Simon RuderichNov 3, 2017
  21. Junio C HamanoNov 3, 2017
  22. Jeff KingNov 3, 2017
  23. René ScharfeNov 4, 2017
  24. Jeff KingNov 4, 2017
  25. Simon RuderichNov 4, 2017
  26. Jeff KingNov 5, 2017
  27. Simon RuderichNov 6, 2017
  28. Simon RuderichNov 16, 2017
  29. Jeff KingNov 17, 2017
  30. Johannes SixtNov 18, 2017
  31. Jeff KingDec 24, 2017
  32. Randall S. BeckerDec 24, 2017
  33. Johannes SixtDec 25, 2017
  34. Johannes SchindelinNov 3, 2017
  35. Jeff KingNov 3, 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.