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

Re: [PATCH] git-log: detect dup and fdopen failure

From
Jim Meyering <jim@meyering.net>
Date
Jun 27, 2007, 14:28 UTC
Message-ID
<87odj1mpy2.fsf@rho.meyering.net>
In-Reply-To
<EF53B249-8430-4700-81AE-B97FD49FB955@adacore.com>
Geert Bosch <bosch@adacore.com> wrote:
> If we really case about catching such errors, write
> the code as:
> 	if (!use_stdout)
> 		realstdout = xfdopen(dup(1), "w");

Thanks for the suggestion. Here's a patch doing just that (and same for dup/xdup):

Subject: [PATCH] git-log: detect dup and fdopen failure
 git-compat-util.h (xdup, xfdopen): Define functions.
Signed-off-by: Jim Meyering <jim@meyering.net>
---
 builtin-log.c     |    2 +-
 git-compat-util.h |   16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/builtin-log.c b/builtin-log.c
index 073a2a1..a4186ea 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -589,7 +589,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		get_patch_ids(&rev, &ids, prefix);

 	if (!use_stdout)
-		realstdout = fdopen(dup(1), "w");
+		realstdout = xfdopen(xdup(1), "w");

 	prepare_revision_walk(&rev);
 	while ((commit = get_revision(&rev)) != NULL) {
diff --git a/git-compat-util.h b/git-compat-util.h
index b2ab3f8..362e040 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -287,6 +287,22 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
 	}
 }

+static inline int xdup(int fd)
+{
+	int ret = dup(fd);
+	if (ret < 0)
+		die("dup failed: %s", strerror(errno));
+	return ret;
+}
+
+static inline FILE *xfdopen(int fd, const char *mode)
+{
+	FILE *stream = fdopen(fd, mode);
+	if (stream == NULL)
+		die("Out of memory? fdopen failed: %s", strerror(errno));
+	return stream;
+}
+
 static inline size_t xsize_t(off_t len)
 {
 	return (size_t)len;
Previous: Jim MeyeringNext: Linus Torvalds
Message 12 of 15 in “detect dup failure”
  1. detect dup failureJim Meyering, Jun 27, 2007
  2. Alex RiesenJun 27, 2007
  3. git-log: detect dup and fdopen failureJim Meyering, Jun 27, 2007
  4. Alex RiesenJun 27, 2007
  5. Jim MeyeringJun 27, 2007
  6. Alex RiesenJun 27, 2007
  7. Jim MeyeringJun 27, 2007
  8. Alex RiesenJun 27, 2007
  9. Jim MeyeringJun 27, 2007
  10. Geert BoschJun 27, 2007
  11. Jim MeyeringJun 27, 2007
  12. Jim MeyeringJun 27, 2007
  13. Linus TorvaldsJun 27, 2007
  14. Josh TriplettJun 28, 2007
  15. Shawn O. PearceJun 28, 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.