Skip to content

Commit

Permalink
git-log: detect dup and fdopen failure
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
meyering authored and gitster committed Jun 28, 2007
1 parent 5483c71 commit f578825
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f578825

Please sign in to comment.