threads / discuss / 22790

[PATCHv4 0/6] repo-local env vars handling

Subject: [PATCHv4 0/6] repo-local env vars handling

## tl;dr

15 messages between Feb 24, 2010 and Feb 24, 2010.

replies: 14people: 3as markdown or json

Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · lore
Changes from the previous iteration:
* moved the static list to environment.c, per Junio's suggestion
* added a local_repo_env_size constant. It's cheap, and it helps
  prevent some horrible gimmicks when the list size needs to be known
  without walking it
* a variation on Junio's variation on Jens' patch for submodule.c,
  using the static list _and_ the size constant.
Giuseppe Bilotta (6):
  environment: static list of repo-local env vars
  connect: use static list of repo-local env vars
  rev-parse: --local-env-vars option
  shell setup: clear_local_git_env() function
  submodules: ensure clean environment when operating in a submodule
  is_submodule_modified(): clear environment properly
 Documentation/git-rev-parse.txt |    6 ++++++
 builtin-rev-parse.c             |    8 ++++++++
 cache.h                         |    3 +++
 connect.c                       |   13 +------------
 environment.c                   |   15 +++++++++++++++
 git-sh-setup.sh                 |    7 +++++++
 git-submodule.sh                |   20 ++++++++++----------
 submodule.c                     |   23 +++++++++++++----------
 8 files changed, 63 insertions(+), 32 deletions(-)
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 1/6] environment: static list of repo-local env vars

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 cache.h       |    3 +++
 environment.c |   15 +++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index d454b7e..3b44fe2 100644
--- a/cache.h
+++ b/cache.h
@@ -388,6 +388,9 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_NOTES_REF_ENVIRONMENT "GIT_NOTES_REF"
 #define GIT_NOTES_DEFAULT_REF "refs/notes/commits"
 
+extern const char *const local_repo_env[];
+extern const unsigned int local_repo_env_size;
+
 extern int is_bare_repository_cfg;
 extern int is_bare_repository(void);
 extern int is_inside_git_dir(void);
diff --git a/environment.c b/environment.c
index 739ec27..b15352d 100644
--- a/environment.c
+++ b/environment.c
@@ -63,6 +63,21 @@ static char *work_tree;
 static const char *git_dir;
 static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
 
+/* Repository-local GIT_* environment variables */
+const char *const local_repo_env[] = {
+	ALTERNATE_DB_ENVIRONMENT,
+	CONFIG_ENVIRONMENT,
+	DB_ENVIRONMENT,
+	GIT_DIR_ENVIRONMENT,
+	GIT_WORK_TREE_ENVIRONMENT,
+	GRAFT_ENVIRONMENT,
+	INDEX_ENVIRONMENT,
+	NO_REPLACE_OBJECTS_ENVIRONMENT,
+	NULL
+};
+
+const unsigned int local_repo_env_size = ARRAY_SIZE(local_repo_env);
+
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
-- 
1.7.0.212.g4e217.dirty
Junio C Hamano· Feb 24, 2010, 15:58 UTC · re: Giuseppe Bilotta · lore

Re: [PATCHv4 1/6] environment: static list of repo-local env vars

Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
Show 14 quoted lines
> +/* Repository-local GIT_* environment variables */
> +const char *const local_repo_env[] = {
> +	ALTERNATE_DB_ENVIRONMENT,
> +	CONFIG_ENVIRONMENT,
> +	DB_ENVIRONMENT,
> +	GIT_DIR_ENVIRONMENT,
> +	GIT_WORK_TREE_ENVIRONMENT,
> +	GRAFT_ENVIRONMENT,
> +	INDEX_ENVIRONMENT,
> +	NO_REPLACE_OBJECTS_ENVIRONMENT,
> +	NULL
> +};
> +
> +const unsigned int local_repo_env_size = ARRAY_SIZE(local_repo_env);
This does not look very useful for two reasons.
 - It counts the NULL at the tail, so the number is one-off; you cannot
   say
	for (i = 0; i < local_repo_env_size; i++)
		do_something_to(local_repo_env[i]);
 - local_repo_env_size is not a compile time constant, so you cannot do:
	const char *env[local_repo_env_size + 20];
	memcpy(env, local_repo_env, sizeof(env[0]) * local_repo_env_size);
	for (i = local_repo_env_size; i < ARRAY_SIZE(env); i++)
		add_more_to(env + i, ...);
Giuseppe Bilotta· Feb 24, 2010, 19:20 UTC · re: Junio C Hamano · lore

Re: [PATCHv4 1/6] environment: static list of repo-local env vars

On Wed, Feb 24, 2010 at 4:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 24 quoted lines
> Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
>
>> +/* Repository-local GIT_* environment variables */
>> +const char *const local_repo_env[] = {
>> +     ALTERNATE_DB_ENVIRONMENT,
>> +     CONFIG_ENVIRONMENT,
>> +     DB_ENVIRONMENT,
>> +     GIT_DIR_ENVIRONMENT,
>> +     GIT_WORK_TREE_ENVIRONMENT,
>> +     GRAFT_ENVIRONMENT,
>> +     INDEX_ENVIRONMENT,
>> +     NO_REPLACE_OBJECTS_ENVIRONMENT,
>> +     NULL
>> +};
>> +
>> +const unsigned int local_repo_env_size = ARRAY_SIZE(local_repo_env);
>
> This does not look very useful for two reasons.
>
>  - It counts the NULL at the tail, so the number is one-off; you cannot
>   say
>
>        for (i = 0; i < local_repo_env_size; i++)
>                do_something_to(local_repo_env[i]);

This is obviously very easy to fix by decrementing the size by 1 in the definition.

Show 6 quoted lines
>  - local_repo_env_size is not a compile time constant, so you cannot do:
>
>        const char *env[local_repo_env_size + 20];
>        memcpy(env, local_repo_env, sizeof(env[0]) * local_repo_env_size);
>        for (i = local_repo_env_size; i < ARRAY_SIZE(env); i++)
>                add_more_to(env + i, ...);

Indeed, I was just discussing this on the other thread. I personally have no objection to using this C99 feature.

I guess #defining local_repo_env_size in cache.h, and keeping it up-to-date when local_repo_env is updated is the best alternative. This can be done in such a way that if it the array needs expansion, forgetting to update its size is catched at compile time, but not for contraction. Of course we don't expect that it will contract. The disadvantage of this is that a change in cache.h requires an almost complete recompile because almost everything depends on it. It's a minor disadvantage, when compared to other approaches (double-walking the list or other stuff).

(Of course, I still wonder what's wrong with C99 VLAs, but anyway).
-- 
Giuseppe "Oblomov" Bilotta
Junio C Hamano· Feb 24, 2010, 19:36 UTC · re: Giuseppe Bilotta · lore

Re: [PATCHv4 1/6] environment: static list of repo-local env vars

Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
Show 19 quoted lines
> On Wed, Feb 24, 2010 at 4:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
>>
>>> +/* Repository-local GIT_* environment variables */
>>> +const char *const local_repo_env[] = {
>>> +     ALTERNATE_DB_ENVIRONMENT,
>>> +     CONFIG_ENVIRONMENT,
>>> +     DB_ENVIRONMENT,
>>> +     GIT_DIR_ENVIRONMENT,
>>> +     GIT_WORK_TREE_ENVIRONMENT,
>>> +     GRAFT_ENVIRONMENT,
>>> +     INDEX_ENVIRONMENT,
>>> +     NO_REPLACE_OBJECTS_ENVIRONMENT,
>>> +     NULL
>>> +};
>>> +
>>> +const unsigned int local_repo_env_size = ARRAY_SIZE(local_repo_env);
>>
>> This does not look very useful for two reasons.

By the way, I said "frustrating" but didn't mean we should bend backwards. The earlier one is more than Ok, and is certainly better than having local_repo_env_size that is not very useful in practice.

Giuseppe Bilotta· Feb 24, 2010, 23:11 UTC · re: Junio C Hamano · lore

Re: [PATCHv4 1/6] environment: static list of repo-local env vars

On Wed, Feb 24, 2010 at 8:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 25 quoted lines
> Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
>
>> On Wed, Feb 24, 2010 at 4:58 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Giuseppe Bilotta <giuseppe.bilotta@gmail.com> writes:
>>>
>>>> +/* Repository-local GIT_* environment variables */
>>>> +const char *const local_repo_env[] = {
>>>> +     ALTERNATE_DB_ENVIRONMENT,
>>>> +     CONFIG_ENVIRONMENT,
>>>> +     DB_ENVIRONMENT,
>>>> +     GIT_DIR_ENVIRONMENT,
>>>> +     GIT_WORK_TREE_ENVIRONMENT,
>>>> +     GRAFT_ENVIRONMENT,
>>>> +     INDEX_ENVIRONMENT,
>>>> +     NO_REPLACE_OBJECTS_ENVIRONMENT,
>>>> +     NULL
>>>> +};
>>>> +
>>>> +const unsigned int local_repo_env_size = ARRAY_SIZE(local_repo_env);
>>>
>>> This does not look very useful for two reasons.
>
> By the way, I said "frustrating" but didn't mean we should bend backwards.
> The earlier one is more than Ok, and is certainly better than having
> local_repo_env_size that is not very useful in practice.

But I'd like a manually-set local_repo_env_size than the gimmicks necessary if it's not there (plus the runtime failure). If there are no other corrections, I'll send a new series (that also squashes the first two patches per Johannes' suggestion) and the #define

-- 
Giuseppe "Oblomov" Bilotta
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 2/6] connect: use static list of repo-local env vars

This adds the missing GIT_CONFIG to the list of env vars that should be cleared.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 connect.c |   13 +------------
 1 files changed, 1 insertions(+), 12 deletions(-)
diff --git a/connect.c b/connect.c
index 9f39038..94b3707 100644
--- a/connect.c
+++ b/connect.c
@@ -582,18 +582,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 		*arg++ = host;
 	}
 	else {
-		/* remove these from the environment */
-		const char *env[] = {
-			ALTERNATE_DB_ENVIRONMENT,
-			DB_ENVIRONMENT,
-			GIT_DIR_ENVIRONMENT,
-			GIT_WORK_TREE_ENVIRONMENT,
-			GRAFT_ENVIRONMENT,
-			INDEX_ENVIRONMENT,
-			NO_REPLACE_OBJECTS_ENVIRONMENT,
-			NULL
-		};
-		conn->env = env;
+		conn->env = local_repo_env;
 		conn->use_shell = 1;
 	}
 	*arg++ = cmd.buf;
-- 
1.7.0.212.g4e217.dirty
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 3/6] rev-parse: --local-env-vars option

This prints the list of repo-local environment variables.
Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 Documentation/git-rev-parse.txt |    6 ++++++
 builtin-rev-parse.c             |    8 ++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
index 1a613aa..8db600f 100644
--- a/Documentation/git-rev-parse.txt
+++ b/Documentation/git-rev-parse.txt
@@ -148,6 +148,12 @@ shown.  If the pattern does not contain a globbing character (`?`,
 --is-bare-repository::
 	When the repository is bare print "true", otherwise "false".
 
+--local-env-vars::
+	List the GIT_* environment variables that are local to the
+	repository (e.g. GIT_DIR or GIT_WORK_TREE, but not GIT_EDITOR).
+	Only the names of the variables are listed, not their value,
+	even if they are set.
+
 --short::
 --short=number::
 	Instead of outputting the full SHA1 values of object names try to
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index a8c5043..ab7b520 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -455,7 +455,15 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 	if (argc > 1 && !strcmp("--sq-quote", argv[1]))
 		return cmd_sq_quote(argc - 2, argv + 2);
 
+	if (argc > 0 && !strcmp("--local-env-vars", argv[1])) {
+		unsigned int i = 0;
+		const char *var;
+		while (var = local_repo_env[i++])
+			printf("%s\n", var);
+		return 0;
+	}
 	if (argc > 1 && !strcmp("-h", argv[1]))
+
 		usage(builtin_rev_parse_usage);
 
 	prefix = setup_git_directory();
-- 
1.7.0.212.g4e217.dirty
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 4/6] shell setup: clear_local_git_env() function

Introduce an auxiliary function to clear all repo-local environment variables. This should be invoked by any shell script that switches repository during execution, to ensure that the environment is clean and that things such as the git dir and worktree are set up correctly.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 git-sh-setup.sh |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index 7a09566..6131670 100644
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -172,6 +172,13 @@ get_author_ident_from_commit () {
 	LANG=C LC_ALL=C sed -ne "$pick_author_script"
 }
 
+# Clear repo-local GIT_* environment variables. Useful when switching to
+# another repository (e.g. when entering a submodule). See also the env
+# list in git_connect()
+clear_local_git_env() {
+	unset $(git rev-parse --local-env-vars)
+}
+
 # Make sure we are in a valid repository of a vintage we understand,
 # if we require to be in a git repository.
 if test -z "$NONGIT_OK"
-- 
1.7.0.212.g4e217.dirty
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 5/6] submodules: ensure clean environment when operating in a submodule

git-submodule used to take care of clearing GIT_DIR whenever it operated on a submodule index or configuration, but forgot to unset GIT_WORK_TREE or other repo-local variables. This would lead to failures e.g. when GIT_WORK_TREE was set.

This only happened in very unusual contexts such as operating on the main worktree from outside of it, but since "git-gui: set GIT_DIR and GIT_WORK_TREE after setup" (a9fa11fe5bd5978bb) such failures could also be provoked by invoking an external tool such as "git submodule update" from the Git Gui in a standard setup.

Solve by using the newly introduced clear_local_git_env() shell function to ensure that all repo-local environment variables are unset.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 git-submodule.sh |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/git-submodule.sh b/git-submodule.sh
index 5869c00..1ea4143 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -222,7 +222,7 @@ cmd_add()
 
 		module_clone "$path" "$realrepo" "$reference" || exit
 		(
-			unset GIT_DIR
+			clear_local_git_env
 			cd "$path" &&
 			# ash fails to wordsplit ${branch:+-b "$branch"...}
 			case "$branch" in
@@ -278,7 +278,7 @@ cmd_foreach()
 			name=$(module_name "$path")
 			(
 				prefix="$prefix$path/"
-				unset GIT_DIR
+				clear_local_git_env
 				cd "$path" &&
 				eval "$@" &&
 				if test -n "$recursive"
@@ -434,7 +434,7 @@ cmd_update()
 			module_clone "$path" "$url" "$reference"|| exit
 			subsha1=
 		else
-			subsha1=$(unset GIT_DIR; cd "$path" &&
+			subsha1=$(clear_local_git_env; cd "$path" &&
 				git rev-parse --verify HEAD) ||
 			die "Unable to find current revision in submodule path '$path'"
 		fi
@@ -454,7 +454,7 @@ cmd_update()
 
 			if test -z "$nofetch"
 			then
-				(unset GIT_DIR; cd "$path" &&
+				(clear_local_git_env; cd "$path" &&
 					git-fetch) ||
 				die "Unable to fetch in submodule path '$path'"
 			fi
@@ -477,14 +477,14 @@ cmd_update()
 				;;
 			esac
 
-			(unset GIT_DIR; cd "$path" && $command "$sha1") ||
+			(clear_local_git_env; cd "$path" && $command "$sha1") ||
 			die "Unable to $action '$sha1' in submodule path '$path'"
 			say "Submodule path '$path': $msg '$sha1'"
 		fi
 
 		if test -n "$recursive"
 		then
-			(unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
+			(clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
 			die "Failed to recurse into submodule path '$path'"
 		fi
 	done
@@ -492,7 +492,7 @@ cmd_update()
 
 set_name_rev () {
 	revname=$( (
-		unset GIT_DIR
+		clear_local_git_env
 		cd "$1" && {
 			git describe "$2" 2>/dev/null ||
 			git describe --tags "$2" 2>/dev/null ||
@@ -760,7 +760,7 @@ cmd_status()
 		else
 			if test -z "$cached"
 			then
-				sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
+				sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
 				set_name_rev "$path" "$sha1"
 			fi
 			say "+$sha1 $displaypath$revname"
@@ -770,7 +770,7 @@ cmd_status()
 		then
 			(
 				prefix="$displaypath/"
-				unset GIT_DIR
+				clear_local_git_env
 				cd "$path" &&
 				cmd_status $orig_args
 			) ||
@@ -821,7 +821,7 @@ cmd_sync()
 		if test -e "$path"/.git
 		then
 		(
-			unset GIT_DIR
+			clear_local_git_env
 			cd "$path"
 			remote=$(get_default_remote)
 			say "Synchronizing submodule url for '$name'"
-- 
1.7.0.212.g4e217.dirty
Giuseppe Bilotta· Feb 24, 2010, 07:23 UTC · re: Giuseppe Bilotta · lore

[PATCHv4 6/6] is_submodule_modified(): clear environment properly

Rather than only clearing GIT_INDEX_FILE, take the list of environment variables to clear from local_repo_env, appending the settings for GIT_DIR and GIT_WORK_TREE.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
---
 submodule.c |   23 +++++++++++++----------
 1 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/submodule.c b/submodule.c
index 7d70c4f..81cf05a 100644
--- a/submodule.c
+++ b/submodule.c
@@ -124,15 +124,21 @@ void show_submodule_summary(FILE *f, const char *path,
 int is_submodule_modified(const char *path)
 {
 	int len;
+	unsigned int i = 0;
 	struct child_process cp;
 	const char *argv[] = {
 		"status",
 		"--porcelain",
 		NULL,
 	};
-	char *env[4];
+	const char *env[local_repo_env_size+2];
 	struct strbuf buf = STRBUF_INIT;
 
+	/* Copy local_repo_env to env, letting i
+	   rest at the last NULL */
+	while (env[i] = local_repo_env[i])
+		++i; /* do nothing */
+
 	strbuf_addf(&buf, "%s/.git/", path);
 	if (!is_directory(buf.buf)) {
 		strbuf_release(&buf);
@@ -143,16 +149,14 @@ int is_submodule_modified(const char *path)
 	strbuf_reset(&buf);
 
 	strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
-	env[0] = strbuf_detach(&buf, NULL);
+	env[i++] = strbuf_detach(&buf, NULL);
 	strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
-	env[1] = strbuf_detach(&buf, NULL);
-	strbuf_addf(&buf, "GIT_INDEX_FILE");
-	env[2] = strbuf_detach(&buf, NULL);
-	env[3] = NULL;
+	env[i++] = strbuf_detach(&buf, NULL);
+	env[i] = NULL;
 
 	memset(&cp, 0, sizeof(cp));
 	cp.argv = argv;
-	cp.env = (const char *const *)env;
+	cp.env = env;
 	cp.git_cmd = 1;
 	cp.no_stdin = 1;
 	cp.out = -1;
@@ -165,9 +169,8 @@ int is_submodule_modified(const char *path)
 	if (finish_command(&cp))
 		die("git status --porcelain failed");
 
-	free(env[0]);
-	free(env[1]);
-	free(env[2]);
+	free((char *)env[--i]);
+	free((char *)env[--i]);
 	strbuf_release(&buf);
 	return len != 0;
 }
-- 
1.7.0.212.g4e217.dirty
Johannes Sixt· Feb 24, 2010, 08:06 UTC · re: Giuseppe Bilotta · lore

Re: [PATCHv4 6/6] is_submodule_modified(): clear environment properly

Giuseppe Bilotta schrieb:
> +	const char *env[local_repo_env_size+2];
Variable sized arrays are prohibited.
Show 7 quoted lines
>  	struct strbuf buf = STRBUF_INIT;
>  
> +	/* Copy local_repo_env to env, letting i
> +	   rest at the last NULL */
> +	while (env[i] = local_repo_env[i])
> +		++i; /* do nothing */
> +

This looks very inconsistent: At the one hand, you use l_r_e_size to allocate the space, but then you iterate over it assuming that the list is (also) NULL-terminated. But this is only a minor nit.

-- Hannes
Giuseppe Bilotta· Feb 24, 2010, 08:55 UTC · re: Johannes Sixt · lore

Re: [PATCHv4 6/6] is_submodule_modified(): clear environment properly

On Wed, Feb 24, 2010 at 9:06 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
> Giuseppe Bilotta schrieb:
>> +     const char *env[local_repo_env_size+2];
>
> Variable sized arrays are prohibited.

Ah, sorry. Is alloca() allowed? I don't see it being used anywhere else in the code, and malloc would be a little too much for this case.

Show 11 quoted lines
>>       struct strbuf buf = STRBUF_INIT;
>>
>> +     /* Copy local_repo_env to env, letting i
>> +        rest at the last NULL */
>> +     while (env[i] = local_repo_env[i])
>> +             ++i; /* do nothing */
>> +
>
> This looks very inconsistent: At the one hand, you use l_r_e_size to
> allocate the space, but then you iterate over it assuming that the list is
> (also) NULL-terminated. But this is only a minor nit.

Well, if you consider that using l-r-e-size is just a way to spare a double-walk, the inconsistency is tolerable. OTOH, in this particular case NULL-walking the list doesn't give the usual benefit of sparing a counter, so I could rework the patch to use a standard for loop. (I also notice that I forgot to remove the /* do nothing */ comment from the while(env[i] = local_repo_env[i++]) ; --i approach I was going with at first)

-- 
Giuseppe "Oblomov" Bilotta
Johannes Sixt· Feb 24, 2010, 09:18 UTC · re: Giuseppe Bilotta · lore

Re: [PATCHv4 6/6] is_submodule_modified(): clear environment properly

Giuseppe Bilotta schrieb:
Show 7 quoted lines
> On Wed, Feb 24, 2010 at 9:06 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
>> Giuseppe Bilotta schrieb:
>>> +     const char *env[local_repo_env_size+2];
>> Variable sized arrays are prohibited.
> 
> Ah, sorry. Is alloca() allowed? I don't see it being used anywhere
> else in the code, and malloc would be a little too much for this case.
in cache.h:

#define LOCAL_REPO_ENV_CAPACITY 20 const char *const l_r_e[];

in environtment.c:
const char *const l_r_e[LOCAL_REPO_ENV_CAPACITY] = { ..., NULL };

and the compiler will croak if the constant becomes too small. (Oh, I know, you must use the 'inconsistent mode' that I nit-picked if you use this approach.)

-- Hannes
Giuseppe Bilotta· Feb 24, 2010, 10:07 UTC · re: Johannes Sixt · lore

Re: [PATCHv4 6/6] is_submodule_modified(): clear environment properly

On Wed, Feb 24, 2010 at 10:18 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 17 quoted lines
> Giuseppe Bilotta schrieb:
>> On Wed, Feb 24, 2010 at 9:06 AM, Johannes Sixt <j.sixt@viscovery.net> wrote:
>>> Giuseppe Bilotta schrieb:
>>>> +     const char *env[local_repo_env_size+2];
>>> Variable sized arrays are prohibited.
>>
>> Ah, sorry. Is alloca() allowed? I don't see it being used anywhere
>> else in the code, and malloc would be a little too much for this case.
>
> in cache.h:
>
> #define LOCAL_REPO_ENV_CAPACITY 20
> const char *const l_r_e[];
>
> in environtment.c:
>
> const char *const l_r_e[LOCAL_REPO_ENV_CAPACITY] = { ..., NULL };

What I don't like about this approach is that (1) it allocates more memory than necessary and (2) it requires recompiling everything if LOCAL_REPO_ENV_CAPACITY changes (since it's defined in cache.h which is included basically everywhere. Admittedly (2) is _very_ unlikely to happen if the #define is set large enough, but still ...

> and the compiler will croak if the constant becomes too small. (Oh, I
> know, you must use the 'inconsistent mode' that I nit-picked if you use
> this approach.)

(BTW, that approach requires slightly less index juggling and less C code than a indexed for loop, so I kept it anyway, with a slightly more expanded comment).

-- 
Giuseppe "Oblomov" Bilotta

← back to recent threads