Skip to content

Commit 773e408

Browse files
committed
Merge branch 'jk/save-getenv-result'
There were many places the code relied on the string returned from getenv() to be non-volatile, which is not true, that have been corrected. * jk/save-getenv-result: builtin_diff(): read $GIT_DIFF_OPTS closer to use merge-recursive: copy $GITHEAD strings init: make a copy of $GIT_DIR string config: make a copy of $GIT_CONFIG string commit: copy saved getenv() result get_super_prefix(): copy getenv() result
2 parents 15b07cb + 0da0e92 commit 773e408

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
351351
if (write_locked_index(&the_index, &index_lock, 0))
352352
die(_("unable to create temporary index"));
353353

354-
old_index_env = getenv(INDEX_ENVIRONMENT);
354+
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
355355
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
356356

357357
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
@@ -361,6 +361,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
361361
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
362362
else
363363
unsetenv(INDEX_ENVIRONMENT);
364+
FREE_AND_NULL(old_index_env);
364365

365366
discard_cache();
366367
read_cache_from(get_lock_file_path(&index_lock));

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
599599
int nongit = !startup_info->have_repository;
600600
char *value;
601601

602-
given_config_source.file = getenv(CONFIG_ENVIRONMENT);
602+
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
603603

604604
argc = parse_options(argc, argv, prefix, builtin_config_options,
605605
builtin_config_usage,

builtin/init-db.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
542542
* GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
543543
* without --bare. Catch the error early.
544544
*/
545-
git_dir = getenv(GIT_DIR_ENVIRONMENT);
546-
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
545+
git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
546+
work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
547547
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
548548
die(_("%s (or --work-tree=<directory>) not allowed without "
549549
"specifying %s (or --git-dir=<directory>)"),
@@ -582,6 +582,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
582582
}
583583

584584
UNLEAK(real_git_dir);
585+
UNLEAK(git_dir);
586+
UNLEAK(work_tree);
585587

586588
flags |= INIT_DB_EXIST_OK;
587589
return init_db(git_dir, real_git_dir, template_dir, flags);

builtin/merge-recursive.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
static const char builtin_merge_recursive_usage[] =
88
"git %s <base>... -- <head> <remote> ...";
99

10-
static const char *better_branch_name(const char *branch)
10+
static char *better_branch_name(const char *branch)
1111
{
1212
static char githead_env[8 + GIT_MAX_HEXSZ + 1];
1313
char *name;
1414

1515
if (strlen(branch) != the_hash_algo->hexsz)
16-
return branch;
16+
return xstrdup(branch);
1717
xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
1818
name = getenv(githead_env);
19-
return name ? name : branch;
19+
return xstrdup(name ? name : branch);
2020
}
2121

2222
int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
@@ -26,6 +26,7 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
2626
int i, failed;
2727
struct object_id h1, h2;
2828
struct merge_options o;
29+
char *better1, *better2;
2930
struct commit *result;
3031

3132
init_merge_options(&o);
@@ -70,13 +71,17 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
7071
if (get_oid(o.branch2, &h2))
7172
die(_("could not resolve ref '%s'"), o.branch2);
7273

73-
o.branch1 = better_branch_name(o.branch1);
74-
o.branch2 = better_branch_name(o.branch2);
74+
o.branch1 = better1 = better_branch_name(o.branch1);
75+
o.branch2 = better2 = better_branch_name(o.branch2);
7576

7677
if (o.verbosity >= 3)
7778
printf(_("Merging %s with %s\n"), o.branch1, o.branch2);
7879

7980
failed = merge_recursive_generic(&o, &h1, &h2, bases_count, bases, &result);
81+
82+
free(better1);
83+
free(better2);
84+
8085
if (failed < 0)
8186
return 128; /* die() error code */
8287
return failed;

diff.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,7 @@ static void builtin_diff(const char *name_a,
35443544
o->found_changes = 1;
35453545
} else {
35463546
/* Crazy xdl interfaces.. */
3547-
const char *diffopts = getenv("GIT_DIFF_OPTS");
3547+
const char *diffopts;
35483548
const char *v;
35493549
xpparam_t xpp;
35503550
xdemitconf_t xecfg;
@@ -3587,12 +3587,15 @@ static void builtin_diff(const char *name_a,
35873587
xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
35883588
if (pe)
35893589
xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
3590+
3591+
diffopts = getenv("GIT_DIFF_OPTS");
35903592
if (!diffopts)
35913593
;
35923594
else if (skip_prefix(diffopts, "--unified=", &v))
35933595
xecfg.ctxlen = strtoul(v, NULL, 10);
35943596
else if (skip_prefix(diffopts, "-u", &v))
35953597
xecfg.ctxlen = strtoul(v, NULL, 10);
3598+
35963599
if (o->word_diff)
35973600
init_diff_words_data(&ecbdata, o, one, two);
35983601
if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,

environment.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ char *git_work_tree_cfg;
107107

108108
static char *git_namespace;
109109

110-
static const char *super_prefix;
110+
static char *super_prefix;
111111

112112
/*
113113
* Repository-local GIT_* environment variables; see cache.h for details.
@@ -240,7 +240,7 @@ const char *get_super_prefix(void)
240240
{
241241
static int initialized;
242242
if (!initialized) {
243-
super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
243+
super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
244244
initialized = 1;
245245
}
246246
return super_prefix;

0 commit comments

Comments
 (0)