Skip to content

Commit f578825

Browse files
meyeringgitster
authored andcommitted
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 <[email protected]> Acked-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5483c71 commit f578825

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

builtin-log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
589589
get_patch_ids(&rev, &ids, prefix);
590590

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

594594
prepare_revision_walk(&rev);
595595
while ((commit = get_revision(&rev)) != NULL) {

git-compat-util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,22 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
287287
}
288288
}
289289

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+
290306
static inline size_t xsize_t(off_t len)
291307
{
292308
return (size_t)len;

0 commit comments

Comments
 (0)