Merge branch 'ab/grep-plug-pathspec-leak'
authorJunio C Hamano <gitster@pobox.com>
Thu, 20 Apr 2017 04:37:21 +0000 (21:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 20 Apr 2017 04:37:21 +0000 (21:37 -0700)
Call clear_pathspec() to release resources immediately before the
cmd_grep() function returns.

* ab/grep-plug-pathspec-leak:
grep: plug a trivial memory leak

1  2 
builtin/grep.c
diff --combined builtin/grep.c
index 65070c52fc2da84bc48804817f1e714f755c9ab5,1b642b70e7e6fb2108fc296ea5a13b803d2b743e..3ffb5b4e8176bbcd1ecf2e4ae2dc84aee968cd22
@@@ -294,23 -294,26 +294,23 @@@ static int grep_cmd_config(const char *
        return st;
  }
  
 -static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
 +static void *lock_and_read_oid_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
  {
        void *data;
  
        grep_read_lock();
 -      data = read_sha1_file(sha1, type, size);
 +      data = read_sha1_file(oid->hash, type, size);
        grep_read_unlock();
        return data;
  }
  
 -static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
 +static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
                     const char *filename, int tree_name_len,
                     const char *path)
  {
        struct strbuf pathbuf = STRBUF_INIT;
  
 -      if (opt->relative && opt->prefix_length) {
 -              quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
 -              strbuf_insert(&pathbuf, 0, filename, tree_name_len);
 -      } else if (super_prefix) {
 +      if (super_prefix) {
                strbuf_add(&pathbuf, filename, tree_name_len);
                strbuf_addstr(&pathbuf, super_prefix);
                strbuf_addstr(&pathbuf, filename + tree_name_len);
                strbuf_addstr(&pathbuf, filename);
        }
  
 +      if (opt->relative && opt->prefix_length) {
 +              char *name = strbuf_detach(&pathbuf, NULL);
 +              quote_path_relative(name + tree_name_len, opt->prefix, &pathbuf);
 +              strbuf_insert(&pathbuf, 0, name, tree_name_len);
 +              free(name);
 +      }
 +
  #ifndef NO_PTHREADS
        if (num_threads) {
 -              add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
 +              add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                return 0;
        } else
                struct grep_source gs;
                int hit;
  
 -              grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, sha1);
 +              grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                hit = grep_source(opt, &gs);
  
@@@ -349,14 -345,12 +349,14 @@@ static int grep_file(struct grep_opt *o
  {
        struct strbuf buf = STRBUF_INIT;
  
 +      if (super_prefix)
 +              strbuf_addstr(&buf, super_prefix);
 +      strbuf_addstr(&buf, filename);
 +
        if (opt->relative && opt->prefix_length) {
 -              quote_path_relative(filename, opt->prefix, &buf);
 -      } else {
 -              if (super_prefix)
 -                      strbuf_addstr(&buf, super_prefix);
 -              strbuf_addstr(&buf, filename);
 +              char *name = strbuf_detach(&buf, NULL);
 +              quote_path_relative(name, opt->prefix, &buf);
 +              free(name);
        }
  
  #ifndef NO_PTHREADS
@@@ -405,12 -399,13 +405,12 @@@ static void run_pager(struct grep_opt *
  }
  
  static void compile_submodule_options(const struct grep_opt *opt,
 -                                    const struct pathspec *pathspec,
 +                                    const char **argv,
                                      int cached, int untracked,
                                      int opt_exclude, int use_index,
                                      int pattern_type_arg)
  {
        struct grep_pat *pattern;
 -      int i;
  
        if (recurse_submodules)
                argv_array_push(&submodule_options, "--recurse-submodules");
  
        /* Add Pathspecs */
        argv_array_push(&submodule_options, "--");
 -      for (i = 0; i < pathspec->nr; i++)
 -              argv_array_push(&submodule_options,
 -                              pathspec->items[i].original);
 +      for (; *argv; argv++)
 +              argv_array_push(&submodule_options, *argv);
  }
  
  /*
@@@ -542,7 -538,7 +542,7 @@@ static int grep_submodule_launch(struc
        int status, i;
        const char *end_of_base;
        const char *name;
 -      struct work_item *w = opt->output_priv;
 +      struct strbuf child_output = STRBUF_INIT;
  
        end_of_base = strchr(gs->name, ':');
        if (gs->identifier && end_of_base)
        prepare_submodule_repo_env(&cp.env_array);
        argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
  
 +      if (opt->relative && opt->prefix_length)
 +              argv_array_pushf(&cp.env_array, "%s=%s",
 +                               GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
 +                               opt->prefix);
 +
        /* Add super prefix */
        argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
                         super_prefix ? super_prefix : "",
         * child process.  A '0' indicates a hit, a '1' indicates no hit and
         * anything else is an error.
         */
 -      status = capture_command(&cp, &w->out, 0);
 +      status = capture_command(&cp, &child_output, 0);
        if (status && (status != 1)) {
                /* flush the buffer */
 -              write_or_die(1, w->out.buf, w->out.len);
 +              write_or_die(1, child_output.buf, child_output.len);
                die("process for submodule '%s' failed with exit code: %d",
                    gs->name, status);
        }
  
 +      opt->output(opt, child_output.buf, child_output.len);
 +      strbuf_release(&child_output);
        /* invert the return code to make a hit equal to 1 */
        return !status;
  }
@@@ -627,7 -616,7 +627,7 @@@ static int grep_submodule(struct grep_o
  {
        if (!is_submodule_initialized(path))
                return 0;
 -      if (!is_submodule_populated(path)) {
 +      if (!is_submodule_populated_gently(path, NULL)) {
                /*
                 * If searching history, check for the presense of the
                 * submodule's gitdir before skipping the submodule.
        } else
  #endif
        {
 -              struct work_item w;
 +              struct grep_source gs;
                int hit;
  
 -              grep_source_init(&w.source, GREP_SOURCE_SUBMODULE,
 +              grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
                                 filename, path, sha1);
 -              strbuf_init(&w.out, 0);
 -              opt->output_priv = &w;
 -              hit = grep_submodule_launch(opt, &w.source);
 -
 -              write_or_die(1, w.out.buf, w.out.len);
 +              hit = grep_submodule_launch(opt, &gs);
  
 -              grep_source_clear(&w.source);
 -              strbuf_release(&w.out);
 +              grep_source_clear(&gs);
                return hit;
        }
  }
@@@ -696,7 -690,7 +696,7 @@@ static int grep_cache(struct grep_opt *
                            ce_skip_worktree(ce)) {
                                if (ce_stage(ce) || ce_intent_to_add(ce))
                                        continue;
 -                              hit |= grep_sha1(opt, ce->oid.hash, ce->name,
 +                              hit |= grep_oid(opt, &ce->oid, ce->name,
                                                 0, ce->name);
                        } else {
                                hit |= grep_file(opt, ce->name);
@@@ -756,7 -750,7 +756,7 @@@ static int grep_tree(struct grep_opt *o
                strbuf_add(base, entry.path, te_len);
  
                if (S_ISREG(entry.mode)) {
 -                      hit |= grep_sha1(opt, entry.oid->hash, base->buf, tn_len,
 +                      hit |= grep_oid(opt, entry.oid, base->buf, tn_len,
                                         check_attr ? base->buf + tn_len : NULL);
                } else if (S_ISDIR(entry.mode)) {
                        enum object_type type;
                        void *data;
                        unsigned long size;
  
 -                      data = lock_and_read_sha1_file(entry.oid->hash, &type, &size);
 +                      data = lock_and_read_oid_file(entry.oid, &type, &size);
                        if (!data)
                                die(_("unable to read tree (%s)"),
                                    oid_to_hex(entry.oid));
@@@ -793,7 -787,7 +793,7 @@@ static int grep_object(struct grep_opt 
                       struct object *obj, const char *name, const char *path)
  {
        if (obj->type == OBJ_BLOB)
 -              return grep_sha1(opt, obj->oid.hash, name, 0, path);
 +              return grep_oid(opt, &obj->oid, name, 0, path);
        if (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {
                struct tree_desc tree;
                void *data;
@@@ -985,7 -979,7 +985,7 @@@ int cmd_grep(int argc, const char **arg
                OPT_SET_INT(0, "exclude-standard", &opt_exclude,
                            N_("ignore files specified via '.gitignore'"), 1),
                OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
 -                       N_("recursivley search in each submodule")),
 +                       N_("recursively search in each submodule")),
                OPT_STRING(0, "parent-basename", &parent_basename,
                           N_("basename"),
                           N_("prepend parent project's basename to output")),
        allow_revs = use_index && !untracked;
        for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
 -              unsigned char sha1[20];
 +              struct object_id oid;
                struct object_context oc;
                struct object *object;
  
                        break;
                }
  
 -              if (get_sha1_with_context(arg, 0, sha1, &oc)) {
 +              if (get_sha1_with_context(arg, 0, oid.hash, &oc)) {
                        if (seen_dashdash)
                                die(_("unable to resolve revision: %s"), arg);
                        break;
                }
  
 -              object = parse_object_or_die(sha1, arg);
 +              object = parse_object_or_die(oid.hash, arg);
                if (!seen_dashdash)
                        verify_non_filename(prefix, arg);
                add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
  
        if (recurse_submodules) {
                gitmodules_config();
 -              compile_submodule_options(&opt, &pathspec, cached, untracked,
 +              compile_submodule_options(&opt, argv + i, cached, untracked,
                                          opt_exclude, use_index,
                                          pattern_type_arg);
        }
                hit |= wait_all();
        if (hit && show_in_pager)
                run_pager(&opt, prefix);
+       clear_pathspec(&pathspec);
        free_grep_patterns(&opt);
        return !hit;
  }