git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH, maint] setup: make sure git_dir path is in a permanent buffer, getenv(3) case
@ 2010-11-11 18:08 Kirill Smelkov
  2010-11-11 18:17 ` Jonathan Nieder
  0 siblings, 1 reply; 6+ messages in thread
From: Kirill Smelkov @ 2010-11-11 18:08 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, msysgit, Kirill Smelkov, Jonathan Nieder

getenv(3) returns not-permanent buffer which may be changed by e.g.
putenv(3) call (*).

In practice I've noticed this when trying to do `git commit -m abc`
inside msysgit under wine, getting

    $ git commit -m abc
    fatal: could not open 'DIR=.git/COMMIT_EDITMSG': No such file or directory
                           ^^^^
    (notice introduced 'DIR=' artifact.)

The problem was showing itself only with -m option, and actually, as
debugging showed, originally

    git_dir = getenv("GIT_DIR")

returned pointer to

        "GIT_DIR=.git\0"
                 ^
               git_dir

, we stored it in git_dir, than, after processing -m git-commit option,
we did setenv("GIT_EDITOR", ":") which as (*) says changed environment
variables memory layout - something like this

       "...\0GIT_DIR=.git\0"
                 ^
               git_dir

and oops - we got wrong git_dir.

Avoid that by strdupping getenv("GIT_DIR") result like we did in 06f354
(setup: make sure git dir path is in a permanent buffer). Unfortunately
this also shows that other getenv usage inside git needs auditing...

(*) from man 3 getenv:

       The implementation of getenv() is not required to  be  reentrant.   The
       string  pointed  to  by  the return value of getenv() may be statically
       allocated, and can be  modified  by  a  subsequent  call  to  getenv(),
       putenv(3), setenv(3), or unsetenv(3).

Cc: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Kirill Smelkov <kirr@mns.spb.ru>
---
 environment.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/environment.c b/environment.c
index eaf908b..d5021e8 100644
--- a/environment.c
+++ b/environment.c
@@ -88,6 +88,7 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
+	git_dir = git_dir ? xstrdup(git_dir) : NULL;
 	if (!git_dir) {
 		git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
 		git_dir = git_dir ? xstrdup(git_dir) : NULL;
-- 
1.7.3.2.161.g3089c

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-11-12 19:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-11 18:08 [PATCH, maint] setup: make sure git_dir path is in a permanent buffer, getenv(3) case Kirill Smelkov
2010-11-11 18:17 ` Jonathan Nieder
2010-11-12 14:03   ` Kirill Smelkov
2010-11-12 16:03     ` Jonathan Nieder
2010-11-12 17:20       ` Kirill Smelkov
2010-11-12 18:59         ` [PATCH] tests: add GETENV_POISON option to simulate unfriendly getenv() Jonathan Nieder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).