{"thread":{"id":"20908","subject":"[PATCH] don't dereference NULL upon fdopen failure","startedAt":"2009-09-12T09:16:59Z","lastAt":"2009-09-12T09:16:59Z","messageCount":1,"participants":["Jim Meyering"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"122938","messageId":"87my5034l0.fsf@meyering.net","threadId":"20908","inReplyTo":null,"subject":"[PATCH] don't dereference NULL upon fdopen failure","fromName":"Jim Meyering","fromEmail":"jim@meyering.net","sentAt":"2009-09-12T09:16:59Z","receivedAt":"2009-09-12T09:16:59Z","isPatch":true,"sender":{"key":"jim@meyering.net","avatar":"https://avatars.githubusercontent.com/u/710630?v=4"},"body":"While making today's write_in_full -> write_str_in_full changes,\nI noticed some unchecked fdopen uses.  A few were already checked,\nbut several others were not.  The patch below changes each unchecked\none to xfdopen.\n\n>From 0cbde9e065a309f75a44a32f7ecc7453e0d482cf Mon Sep 17 00:00:00 2001\nFrom: Jim Meyering <meyering@redhat.com>\nDate: Sat, 12 Sep 2009 10:43:27 +0200\nSubject: [PATCH] don't dereference NULL upon fdopen failure\n\n* builtin-add.c (edit_patch): Use xfdopen instead.\n* builtin-mailsplit.c (split_one): Likewise.\n* bundle.c (create_bundle): Likewise.\n* transport-helper.c (get_helper, fetch_with_fetch): Likewise.\n(get_refs_list): Likewise.\n* upload-pack.c (do_rev_list, create_pack_file): Likewise.\n\nSigned-off-by: Jim Meyering <meyering@redhat.com>\n---\n builtin-add.c       |    2 +-\n builtin-mailsplit.c |    2 +-\n bundle.c            |    2 +-\n transport-helper.c  |    6 +++---\n upload-pack.c       |    4 ++--\n 5 files changed, 8 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin-add.c b/builtin-add.c\nindex a571473..cb6e590 100644\n--- a/builtin-add.c\n+++ b/builtin-add.c\n@@ -198,7 +198,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)\n \tout = open(file, O_CREAT | O_WRONLY, 0644);\n \tif (out < 0)\n \t\tdie (\"Could not open '%s' for writing.\", file);\n-\trev.diffopt.file = fdopen(out, \"w\");\n+\trev.diffopt.file = xfdopen(out, \"w\");\n \trev.diffopt.close_file = 1;\n \tif (run_diff_files(&rev, 0))\n \t\tdie (\"Could not write patch\");\ndiff --git a/builtin-mailsplit.c b/builtin-mailsplit.c\nindex ee6ca0e..dfe5b15 100644\n--- a/builtin-mailsplit.c\n+++ b/builtin-mailsplit.c\n@@ -64,7 +64,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)\n \tfd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);\n \tif (fd < 0)\n \t\tdie_errno(\"cannot open output file '%s'\", name);\n-\toutput = fdopen(fd, \"w\");\n+\toutput = xfdopen(fd, \"w\");\n\n \t/* Copy it out, while searching for a line that begins with\n \t * \"From \" and having something that looks like a date format.\ndiff --git a/bundle.c b/bundle.c\nindex e4b2aa9..df95e15 100644\n--- a/bundle.c\n+++ b/bundle.c\n@@ -234,7 +234,7 @@ int create_bundle(struct bundle_header *header, const char *path,\n \trls.git_cmd = 1;\n \tif (start_command(&rls))\n \t\treturn -1;\n-\trls_fout = fdopen(rls.out, \"r\");\n+\trls_fout = xfdopen(rls.out, \"r\");\n \twhile (fgets(buffer, sizeof(buffer), rls_fout)) {\n \t\tunsigned char sha1[20];\n \t\tif (buffer[0] == '-') {\ndiff --git a/transport-helper.c b/transport-helper.c\nindex 832d81f..f57e84c 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -39,7 +39,7 @@ static struct child_process *get_helper(struct transport *transport)\n\n \twrite_str_in_full(helper->in, \"capabilities\\n\");\n\n-\tfile = fdopen(helper->out, \"r\");\n+\tfile = xfdopen(helper->out, \"r\");\n \twhile (1) {\n \t\tif (strbuf_getline(&buf, file, '\\n') == EOF)\n \t\t\texit(128); /* child died, message supplied already */\n@@ -71,7 +71,7 @@ static int fetch_with_fetch(struct transport *transport,\n \t\t\t    int nr_heads, const struct ref **to_fetch)\n {\n \tstruct child_process *helper = get_helper(transport);\n-\tFILE *file = fdopen(helper->out, \"r\");\n+\tFILE *file = xfdopen(helper->out, \"r\");\n \tint i;\n \tstruct strbuf buf = STRBUF_INIT;\n\n@@ -124,7 +124,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)\n\n \twrite_str_in_full(helper->in, \"list\\n\");\n\n-\tfile = fdopen(helper->out, \"r\");\n+\tfile = xfdopen(helper->out, \"r\");\n \twhile (1) {\n \t\tchar *eov, *eon;\n \t\tif (strbuf_getline(&buf, file, '\\n') == EOF)\ndiff --git a/upload-pack.c b/upload-pack.c\nindex b3471e4..38ddac2 100644\n--- a/upload-pack.c\n+++ b/upload-pack.c\n@@ -108,7 +108,7 @@ static int do_rev_list(int fd, void *create_full_pack)\n \tint i;\n \tstruct rev_info revs;\n\n-\tpack_pipe = fdopen(fd, \"w\");\n+\tpack_pipe = xfdopen(fd, \"w\");\n \tinit_revisions(&revs, NULL);\n \trevs.tag_objects = 1;\n \trevs.tree_objects = 1;\n@@ -255,7 +255,7 @@ static void create_pack_file(void)\n\n \t/* pass on revisions we (don't) want */\n \tif (!shallow_nr) {\n-\t\tFILE *pipe_fd = fdopen(pack_objects.in, \"w\");\n+\t\tFILE *pipe_fd = xfdopen(pack_objects.in, \"w\");\n \t\tif (!create_full_pack) {\n \t\t\tint i;\n \t\t\tfor (i = 0; i < want_obj.nr; i++)\n--\n1.6.5.rc0.190.g15871\n"}]}