Merge branch 'jl/pack-transfer-avoid-double-close'
authorJunio C Hamano <gitster@pobox.com>
Wed, 30 Oct 2013 19:10:45 +0000 (12:10 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 Oct 2013 19:10:45 +0000 (12:10 -0700)
The codepath that send_pack() calls pack_objects() mistakenly
closed the same file descriptor twice, leading to potentially
closing a wrong file descriptor that was opened in the meantime.

* jl/pack-transfer-avoid-double-close:
Clear fd after closing to avoid double-close error

1  2 
fetch-pack.c
send-pack.c
diff --combined fetch-pack.c
index a0e0350ae6bdf338b3c11c21fc050edb60c88569,29b711a98b029152a087f280308e04cc51a1b81f..1042448fa0b321e8826f0fdb69c89810c731cf5d
@@@ -9,7 -9,6 +9,7 @@@
  #include "fetch-pack.h"
  #include "remote.h"
  #include "run-command.h"
 +#include "connect.h"
  #include "transport.h"
  #include "version.h"
  #include "prio-queue.h"
@@@ -185,6 -184,36 +185,6 @@@ static void consume_shallow_list(struc
        }
  }
  
 -struct write_shallow_data {
 -      struct strbuf *out;
 -      int use_pack_protocol;
 -      int count;
 -};
 -
 -static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
 -{
 -      struct write_shallow_data *data = cb_data;
 -      const char *hex = sha1_to_hex(graft->sha1);
 -      data->count++;
 -      if (data->use_pack_protocol)
 -              packet_buf_write(data->out, "shallow %s", hex);
 -      else {
 -              strbuf_addstr(data->out, hex);
 -              strbuf_addch(data->out, '\n');
 -      }
 -      return 0;
 -}
 -
 -static int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
 -{
 -      struct write_shallow_data data;
 -      data.out = out;
 -      data.use_pack_protocol = use_pack_protocol;
 -      data.count = 0;
 -      for_each_commit_graft(write_one_shallow, &data);
 -      return data.count;
 -}
 -
  static enum ack_type get_ack(int fd, unsigned char *result_sha1)
  {
        int len;
@@@ -659,7 -688,7 +659,7 @@@ static int get_pack(struct fetch_pack_a
        const char *argv[22];
        char keep_arg[256];
        char hdr_arg[256];
 -      const char **av;
 +      const char **av, *cmd_name;
        int do_keep = args->keep_pack;
        struct child_process cmd;
        int ret;
        if (do_keep) {
                if (pack_lockfile)
                        cmd.out = -1;
 -              *av++ = "index-pack";
 +              *av++ = cmd_name = "index-pack";
                *av++ = "--stdin";
                if (!args->quiet && !args->no_progress)
                        *av++ = "-v";
                        *av++ = "--check-self-contained-and-connected";
        }
        else {
 -              *av++ = "unpack-objects";
 +              *av++ = cmd_name = "unpack-objects";
                if (args->quiet || args->no_progress)
                        *av++ = "-q";
                args->check_self_contained_and_connected = 0;
        cmd.in = demux.out;
        cmd.git_cmd = 1;
        if (start_command(&cmd))
 -              die("fetch-pack: unable to fork off %s", argv[0]);
 +              die("fetch-pack: unable to fork off %s", cmd_name);
        if (do_keep && pack_lockfile) {
                *pack_lockfile = index_pack_lockfile(cmd.out);
                close(cmd.out);
        }
  
+       if (!use_sideband)
+               /* Closed by start_command() */
+               xd[0] = -1;
        ret = finish_command(&cmd);
        if (!ret || (args->check_self_contained_and_connected && ret == 1))
                args->self_contained_and_connected =
                        args->check_self_contained_and_connected &&
                        ret == 0;
        else
 -              die("%s failed", argv[0]);
 +              die("%s failed", cmd_name);
        if (use_sideband && finish_async(&demux))
                die("error in sideband demultiplexer");
        return 0;
@@@ -766,6 -799,27 +770,6 @@@ static int cmp_ref_by_name(const void *
        return strcmp(a->name, b->name);
  }
  
 -static void setup_alternate_shallow(void)
 -{
 -      struct strbuf sb = STRBUF_INIT;
 -      int fd;
 -
 -      check_shallow_file_for_update();
 -      fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
 -                                     LOCK_DIE_ON_ERROR);
 -      if (write_shallow_commits(&sb, 0)) {
 -              if (write_in_full(fd, sb.buf, sb.len) != sb.len)
 -                      die_errno("failed to write to %s", shallow_lock.filename);
 -              alternate_shallow_file = shallow_lock.filename;
 -      } else
 -              /*
 -               * is_repository_shallow() sees empty string as "no
 -               * shallow file".
 -               */
 -              alternate_shallow_file = "";
 -      strbuf_release(&sb);
 -}
 -
  static struct ref *do_fetch_pack(struct fetch_pack_args *args,
                                 int fd[2],
                                 const struct ref *orig_ref,
        if (args->stateless_rpc)
                packet_flush(fd[1]);
        if (args->depth > 0)
 -              setup_alternate_shallow();
 +              setup_alternate_shallow(&shallow_lock, &alternate_shallow_file);
        else
                alternate_shallow_file = NULL;
        if (get_pack(args, fd, pack_lockfile))
diff --combined send-pack.c
index b228d65613c511ffa59eb3ab254ee2e2b6f26c9b,edbfd076a9ebb6883753d77f66580db97851f9c0..fab62e3da05913b5a4f76db7ed1c17e5831384d2
@@@ -5,7 -5,6 +5,7 @@@
  #include "sideband.h"
  #include "run-command.h"
  #include "remote.h"
 +#include "connect.h"
  #include "send-pack.h"
  #include "quote.h"
  #include "transport.h"
@@@ -227,7 -226,6 +227,7 @@@ int send_pack(struct send_pack_args *ar
                case REF_STATUS_REJECT_ALREADY_EXISTS:
                case REF_STATUS_REJECT_FETCH_FIRST:
                case REF_STATUS_REJECT_NEEDS_FORCE:
 +              case REF_STATUS_REJECT_STALE:
                case REF_STATUS_UPTODATE:
                        continue;
                default:
                                shutdown(fd[0], SHUT_WR);
                        if (use_sideband)
                                finish_async(&demux);
+                       fd[1] = -1;
                        return -1;
                }
+               if (!args->stateless_rpc)
+                       /* Closed by pack_objects() via start_command() */
+                       fd[1] = -1;
        }
        if (args->stateless_rpc && cmds_sent)
                packet_flush(out);