wrapper: supply xsetenv
PATCH 1/2 wrapper: supply xsetenv
--- git-compat-util.h | 1 + wrapper.c | 6 ++++++ 2 files changed, 7 insertions(+), 0 deletions(-)
git-compat-util.h +1 -0
@@ -439,6 +439,7 @@ extern int xdup(int fd);extern FILE *xfdopen (int fd,const char *mode);extern int xmkstemp (char *template);extern int xmkstemp_mode (char *template,int mode); +extern int xsetenv (const char *name,const char *val,int override);extern int odb_mkstemp (char *template,size_t limit,const char *pattern);extern int odb_pack_keep (char *name,size_t namesz,unsigned char *sha1);
wrapper.c +6 -0
@@ -381,3 +381,9 @@ int remove_or_warn(unsigned int mode, const char *file) {return S_ISGITLINK (mode) ?rmdir_or_warn (file) :unlink_or_warn (file); } + +int xsetenv (const char *name,const char *val,int overwrite) +{ +if (setenv (name, val, overwrite)) +die_errno ("setenv failed" ); +}
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
PATCH 2/2 change all unchecked calls to setenv to xsetenv
This avoids us from accidentally dropping state, possibly leading to unexpected behaviour.
---
While reviewing some patches for Git for Windows, I realized that we almost never check the return-value from setenv. This can lead to quite surprising errors in unusual sitations. Mostly, an error would probably be preferred. So here we go.
However, I'm not at all convinced myself that all of these make sense; in particular settings like GIT_EDITOR and GIT_PAGER could perhaps benefit from having a warning printed rather than a hard error.
Thoughts?
builtin/clone.c | 2 +- builtin/commit.c | 6 +++--- builtin/help.c | 4 ++-- builtin/init-db.c | 2 +- builtin/merge.c | 4 ++-- builtin/notes.c | 2 +- builtin/remote-ext.c | 4 ++-- builtin/revert.c | 2 +- config.c | 2 +- exec_cmd.c | 4 ++-- git.c | 18 +++++++++--------- pager.c | 2 +- setup.c | 6 +++--- 13 files changed, 29 insertions(+), 29 deletions(-)
builtin/clone.c +1 -1
@@ -566,7 +566,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)atexit (remove_junk);sigchain_push_common (remove_junk_on_signal); -setenv (CONFIG_ENVIRONMENT ,mkpath ("%s/config" , git_dir),1 ); +xsetenv (CONFIG_ENVIRONMENT ,mkpath ("%s/config" , git_dir),1 );if (safe_create_leading_directories_const (git_dir) <0 )die (_ ("could not create leading directories of '%s'" ), git_dir);
builtin/commit.c +3 -3
@@ -361,13 +361,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,die (_ ("unable to create temporary index" )); old_index_env =getenv (INDEX_ENVIRONMENT ); -setenv (INDEX_ENVIRONMENT , index_lock.filename,1 ); +xsetenv (INDEX_ENVIRONMENT , index_lock.filename,1 );if (interactive_add (argc, argv, prefix, patch_interactive) !=0 )die (_ ("interactive add failed" ));if (old_index_env && *old_index_env) -setenv (INDEX_ENVIRONMENT , old_index_env,1 ); +xsetenv (INDEX_ENVIRONMENT , old_index_env,1 );else unsetenv (INDEX_ENVIRONMENT );
@@ -1023,7 +1023,7 @@ static int parse_and_validate_options(int argc, const char *argv[],if (edit_flag) use_editor =1 ;if (!use_editor) -setenv ("GIT_EDITOR" ,":" ,1 ); +xsetenv ("GIT_EDITOR" ,":" ,1 );/* Sanity check options */ if (amend && !current_head)
builtin/help.c +2 -2
@@ -330,7 +330,7 @@ static void setup_man_path(void)if (old_path)strbuf_addstr (&new_path, old_path); -setenv ("MANPATH" , new_path.buf,1 ); +xsetenv ("MANPATH" , new_path.buf,1 );strbuf_release (&new_path); }
@@ -371,7 +371,7 @@ static void show_man_page(const char *git_cmd)static void show_info_page (const char *git_cmd) {const char *page =cmd_to_page (git_cmd); -setenv ("INFOPATH" ,system_path (GIT_INFO_PATH ),1 ); +xsetenv ("INFOPATH" ,system_path (GIT_INFO_PATH ),1 );execlp ("info" ,"info" ,"gitman" , page, (char *)NULL );die ("no info viewer handled the request" ); }
builtin/init-db.c +1 -1
@@ -537,7 +537,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)if (is_bare_repository_cfg ==1 ) {static char git_dir[PATH_MAX +1 ]; -setenv (GIT_DIR_ENVIRONMENT , +xsetenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)), argc >0 ); }
builtin/merge.c +2 -2
@@ -1257,7 +1257,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)strbuf_addstr (&buf,"merge" );for (i =0 ; i < argc; i++)strbuf_addf (&buf," %s" , argv[i]); -setenv ("GIT_REFLOG_ACTION" , buf.buf,0 ); +xsetenv ("GIT_REFLOG_ACTION" , buf.buf,0 );strbuf_reset (&buf);for (i =0 ; i < argc; i++) {
@@ -1267,7 +1267,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) remotes = &commit_list_insert (commit, remotes)->next;strbuf_addf (&buf,"GITHEAD_%s" ,sha1_to_hex (commit->object.sha1)); -setenv (buf.buf, argv[i],1 ); +xsetenv (buf.buf, argv[i],1 );strbuf_reset (&buf); if (merge_remote_util(commit) && merge_remote_util(commit)->obj &&
builtin/notes.c +1 -1
@@ -1076,7 +1076,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)struct strbuf sb =STRBUF_INIT ;strbuf_addstr (&sb, override_notes_ref);expand_notes_ref (&sb); -setenv ("GIT_NOTES_REF" , sb.buf,1 ); +xsetenv ("GIT_NOTES_REF" , sb.buf,1 );strbuf_release (&sb); }
builtin/remote-ext.c +2 -2
@@ -38,8 +38,8 @@ static char *strip_escapes(const char *str, const char *service, psoff =4 ;/* Pass the service to command. */ -setenv ("GIT_EXT_SERVICE" , service,1 ); -setenv ("GIT_EXT_SERVICE_NOPREFIX" , service + psoff,1 ); +xsetenv ("GIT_EXT_SERVICE" , service,1 ); +xsetenv ("GIT_EXT_SERVICE_NOPREFIX" , service + psoff,1 );/* Scan the length of argument. */ while (str[rpos] && (escape || str[rpos] !=' ' )) {
builtin/revert.c +1 -1
@@ -1007,7 +1007,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)struct commit_list *cur;int res; -setenv (GIT_REFLOG_ACTION ,action_name (opts),0 ); +xsetenv (GIT_REFLOG_ACTION ,action_name (opts),0 );if (opts->allow_ff)assert (!(opts->signoff || opts->no_commit || opts->record_origin || opts->edit));
config.c +1 -1
@@ -43,7 +43,7 @@ void git_config_push_parameter(const char *text)strbuf_addch (&env,' ' ); }sq_quote_buf (&env, text); -setenv (CONFIG_DATA_ENVIRONMENT , env.buf,1 ); +xsetenv (CONFIG_DATA_ENVIRONMENT , env.buf,1 );strbuf_release (&env); }
exec_cmd.c +2 -2
@@ -63,7 +63,7 @@ void git_set_argv_exec_path(const char *exec_path)/* * Propagate this setting to external programs. */ -setenv (EXEC_PATH_ENVIRONMENT , exec_path,1 ); +xsetenv (EXEC_PATH_ENVIRONMENT , exec_path,1 ); }
@@ -108,7 +108,7 @@ void setup_path(void)else strbuf_addstr (&new_path, _PATH_DEFPATH); -setenv ("PATH" , new_path.buf,1 ); +xsetenv ("PATH" , new_path.buf,1 );strbuf_release (&new_path); }
git.c +9 -9
@@ -54,7 +54,7 @@ int check_pager_config(const char *cmd)static void commit_pager_choice (void ) {switch (use_pager) {case 0 : -setenv ("GIT_PAGER" ,"cat" ,1 ); +xsetenv ("GIT_PAGER" ,"cat" ,1 );break ;case 1 :setup_pager ();
@@ -109,7 +109,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--no-replace-objects" )) { read_replace_refs =0 ; -setenv (NO_REPLACE_OBJECTS_ENVIRONMENT ,"1" ,1 ); +xsetenv (NO_REPLACE_OBJECTS_ENVIRONMENT ,"1" ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--git-dir" )) {
@@ -117,13 +117,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No directory given for --git-dir.\n" );usage (git_usage_string); } -setenv (GIT_DIR_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_DIR_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--git-dir=" )) { -setenv (GIT_DIR_ENVIRONMENT , cmd +10 ,1 ); +xsetenv (GIT_DIR_ENVIRONMENT , cmd +10 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--namespace" )) {
@@ -131,13 +131,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No namespace given for --namespace.\n" );usage (git_usage_string); } -setenv (GIT_NAMESPACE_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_NAMESPACE_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--namespace=" )) { -setenv (GIT_NAMESPACE_ENVIRONMENT , cmd +12 ,1 ); +xsetenv (GIT_NAMESPACE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--work-tree" )) {
@@ -145,19 +145,19 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No directory given for --work-tree.\n" );usage (git_usage_string); } -setenv (GIT_WORK_TREE_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--work-tree=" )) { -setenv (GIT_WORK_TREE_ENVIRONMENT , cmd +12 ,1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--bare" )) {static char git_dir[PATH_MAX +1 ]; is_bare_repository_cfg =1 ; -setenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)),0 ); +xsetenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)),0 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"-c" )) {
pager.c +1 -1
@@ -76,7 +76,7 @@ void setup_pager(void)if (!pager)return ; -setenv ("GIT_PAGER_IN_USE" ,"true" ,1 ); +xsetenv ("GIT_PAGER_IN_USE" ,"true" ,1 );/* spawn the pager */ pager_argv[0 ] = pager;
setup.c +3 -3
@@ -309,7 +309,7 @@ void setup_work_tree(void)* if $GIT_WORK_TREE is set relative */ if (getenv (GIT_WORK_TREE_ENVIRONMENT )) -setenv (GIT_WORK_TREE_ENVIRONMENT ,"." ,1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT ,"." ,1 );set_git_dir (relative_path (git_dir, work_tree)); initialized =1 ;
@@ -683,9 +683,9 @@ const char *setup_git_directory_gently(int *nongit_ok) prefix =setup_git_directory_gently_1 (nongit_ok);if (prefix) -setenv ("GIT_PREFIX" , prefix,1 ); +xsetenv ("GIT_PREFIX" , prefix,1 );else -setenv ("GIT_PREFIX" ,"" ,1 ); +xsetenv ("GIT_PREFIX" ,"" ,1 );if (startup_info) { startup_info->have_repository = !nongit_ok || !*nongit_ok;
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
Re: [PATCH/RFC 2/2] change all unchecked calls to setenv to xsetenv
Quoted 11 lines · Erik Faye-Lund
While reviewing some patches for Git for Windows, I realized that we almost never check the return-value from setenv. This can lead to quite surprising errors in unusual sitations. Mostly, an error would probably be preferred. So here we go.
However, I'm not at all convinced myself that all of these make sense; in particular settings like
GIT_EDITORandGIT_PAGERcould perhaps benefit from having a warning printed rather than a hard error.Thoughts?
I wrote almost the same patch once[1], but failed to actually push it through to acceptance. There weren't any objections, just that nobody really cared. I think it's a reasonable thing to do. The chances of setenv failing are very low, but the consequences could be quite bad.
There is also a call to putenv in git.c which should be checked (or could arguably just be converted to setenv).
-Peff
[1] http://thread.gmane.org/gmane.comp.version-control.git/134466
Re: [PATCH/RFC 2/2] change all unchecked calls to setenv to xsetenv
Quoted 12 lines · Jeff King
Quoted 10 lines · Erik Faye-Lund
While reviewing some patches for Git for Windows, I realized that we almost never check the return-value from setenv. This can lead to quite surprising errors in unusual sitations. Mostly, an error would probably be preferred. So here we go.
However, I'm not at all convinced myself that all of these make sense; in particular settings like
GIT_EDITORandGIT_PAGERcould perhaps benefit from having a warning printed rather than a hard error.
Quoted 4 lines
Quoted 1 line
Thoughts?
I wrote almost the same patch once[1], but failed to actually push it through to acceptance.
In both cases, the patches are _designed_ to fail to attract any attention. Your earlier one had this preamble:
Here is a patch. I still feel a little silly writing this. The chances that you will run out of memory doing setenv but _not_ doing any of the other git operations seems very low.
which rather *loudly* says "ignore me, please!" ;-)
Erik's patch this round is no better; if its log message said something like "On platform X, the environment space is merely nKB and setenv has much higher chance of failing than on typical Linux boxes", it would have been a no brainer for me to respond with "makes perfect sense but don't we also use putenv?" immediately.
Re: [PATCH/RFC 2/2] change all unchecked calls to setenv to xsetenv
Quoted 26 lines · Junio C Hamano
Quoted 12 lines · Jeff King
Quoted 10 lines · Erik Faye-Lund
While reviewing some patches for Git for Windows, I realized that we almost never check the return-value from setenv. This can lead to quite surprising errors in unusual sitations. Mostly, an error would probably be preferred. So here we go.
However, I'm not at all convinced myself that all of these make sense; in particular settings like
GIT_EDITORandGIT_PAGERcould perhaps benefit from having a warning printed rather than a hard error.Quoted 4 lines
Quoted 1 line
Thoughts?
I wrote almost the same patch once[1], but failed to actually push it through to acceptance.
In both cases, the patches are _designed_ to fail to attract any attention. Your earlier one had this preamble:
Here is a patch. I still feel a little silly writing this. The chances that you will run out of memory doing setenv but _not_ doing any of the other git operations seems very low.
Quoted 8 lines
which rather *loudly* says "ignore me, please!" ;-)
Erik's patch this round is no better; if its log message said something like "On platform X, the environment space is merely nKB and setenv has much higher chance of failing than on typical Linux boxes", it would have been a no brainer for me to respond with "makes perfect sense but don't we also use putenv?" immediately.
It could be because I treated this completely like a theoretical patch; I haven't seen it actually happen.
But you are right, Windows 32 kB environment limit makes this much more likely than your average Linux box. So perhaps I should add a notice about that in the next round...
wrapper.c int remove_or_warn(unsigned int mode, const char *file) · line 389
Quoted 5 lines · Erik Faye-Lund
wrapper.c +5
-0+int xsetenv (const char *name,const char *val,int overwrite) +{ +if (setenv (name, val, overwrite)) +die_errno ("setenv failed" ); +}
It probably doesn't matter, because the error condition is almost certainly just "oops, we ran out of memory". But you could also print the name of the variable being set, which may give the user a clue to some misconfiguration (e.g., trying to put some extremely long value into the environment).
-Peff
Quoted 13 lines · Jeff King
wrapper.c int remove_or_warn(unsigned int mode, const char *file) · line 389
Quoted 5 lines · Erik Faye-Lund
wrapper.c +5
-0+int xsetenv (const char *name,const char *val,int overwrite) +{ +if (setenv (name, val, overwrite)) +die_errno ("setenv failed" ); +}It probably doesn't matter, because the error condition is almost certainly just "oops, we ran out of memory". But you could also print the name of the variable being set, which may give the user a clue to some misconfiguration (e.g., trying to put some extremely long value into the environment).
Do we have enough memory to format that message in that situation ;-)?
Quoted 16 lines · Junio C Hamano
Quoted 13 lines · Jeff King
wrapper.c int remove_or_warn(unsigned int mode, const char *file) · line 389
Quoted 5 lines · Erik Faye-Lund
wrapper.c +5
-0+int xsetenv (const char *name,const char *val,int overwrite) +{ + if (setenv(name, val, overwrite)) + die_errno("setenv failed"); +}It probably doesn't matter, because the error condition is almost certainly just "oops, we ran out of memory". But you could also print the name of the variable being set, which may give the user a clue to some misconfiguration (e.g., trying to put some extremely long value into the environment).
Quoted 1 line
Do we have enough memory to format that message in that situation ;-)?
We could. Running out of environment space is not the same as running out of memory. For instance, Windows has a maximum environment size of 32 kB. Older Linux kernels maxed out at 128 kB.
So I think it's a good idea to at least try. The worst that can happen is another, less descriptive error, no?
Quoted 1 line · Erik Faye-Lund
Older Linux kernels maxed out at 128 kB.
But you wouldn't detect that at setenv time.
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."