@matheusbsbs / matheus-bagliano / commits / f57882505e
Fork 0

git-log: detect dup and fdopen failure

This defines xdup() and xfdopen() in git-compat-util.h to give us error-catching variants of them without cluttering the code too much. Signed-off-by: Jim Meyering <jim@meyering.net> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jim Meyering committed Jun 27, 2007 at 16:28 UTC f57882505efe05990102a0d96b37c09baadae03d
2 files changed +17 -1
builtin-log.c
+1 -1
@@ -589,7 +589,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
589 get_patch_ids(&rev, &ids, prefix);
590
591 if (!use_stdout)
592 - realstdout = fdopen(dup(1), "w");
592 + realstdout = xfdopen(xdup(1), "w");
593
594 prepare_revision_walk(&rev);
595 while ((commit = get_revision(&rev)) != NULL) {
git-compat-util.h
+16
@@ -287,6 +287,22 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
287 }
288 }
289
290 +static inline int xdup(int fd)
291 +{
292 + int ret = dup(fd);
293 + if (ret < 0)
294 + die("dup failed: %s", strerror(errno));
295 + return ret;
296 +}
297 +
298 +static inline FILE *xfdopen(int fd, const char *mode)
299 +{
300 + FILE *stream = fdopen(fd, mode);
301 + if (stream == NULL)
302 + die("Out of memory? fdopen failed: %s", strerror(errno));
303 + return stream;
304 +}
305 +
306 static inline size_t xsize_t(off_t len)
307 {
308 return (size_t)len;