Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 341b8fa

Browse filesBrowse files
committed
fix(env.c): drop envmap, callers must free os_getenv()
Problem: Issue neovim#32550 Due to the internal caching mechanism introduced in neovim#7920 libuv commands aren't issued directly checking actual settings, which is most reliable from all ways of access (:!printenv, :echo, :lua, etc.). Solution: Removing the cache, making sure that all memory allocations are safe. The caller must free the result of os_getenv() while new function os_getenv_noalloc() was introduced using NameBuff avoiding of memory allocation. In addition os_env_exists() got extended with a 'nonempty' parameter, allowing to differentiate between env variables being not set at all, set with an empty value, set with an actual value.
1 parent e5ddf7a commit 341b8fa
Copy full SHA for 341b8fa

File tree

Expand file treeCollapse file tree

25 files changed

+257
-146
lines changed
Filter options
Expand file treeCollapse file tree

25 files changed

+257
-146
lines changed

‎src/nvim/diff.c

Copy file name to clipboardExpand all lines: src/nvim/diff.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ static int diff_file(diffio_T *dio)
11431143
char *const cmd = xmalloc(len);
11441144

11451145
// We don't want $DIFF_OPTIONS to get in the way.
1146-
if (os_getenv("DIFF_OPTIONS")) {
1146+
if (os_env_exists("DIFF_OPTIONS", true)) {
11471147
os_unsetenv("DIFF_OPTIONS");
11481148
}
11491149

‎src/nvim/eval/funcs.c

Copy file name to clipboardExpand all lines: src/nvim/eval/funcs.c
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ static void f_exists(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
15561556
const char *p = tv_get_string(&argvars[0]);
15571557
if (*p == '$') { // Environment variable.
15581558
// First try "normal" environment variables (fast).
1559-
if (os_env_exists(p + 1)) {
1559+
if (os_env_exists(p + 1, false)) {
15601560
n = true;
15611561
} else {
15621562
// Try expanding things like $VIM and ${HOME}.
@@ -3881,9 +3881,9 @@ dict_T *create_environment(const dictitem_T *job_env, const bool clear_env, cons
38813881
size_t len = strlen(required_env_vars[i]);
38823882
dictitem_T *dv = tv_dict_find(env, required_env_vars[i], (ptrdiff_t)len);
38833883
if (!dv) {
3884-
const char *env_var = os_getenv(required_env_vars[i]);
3884+
char *env_var = os_getenv(required_env_vars[i]);
38853885
if (env_var) {
3886-
tv_dict_add_str(env, required_env_vars[i], len, env_var);
3886+
tv_dict_add_allocated_str(env, required_env_vars[i], len, env_var);
38873887
}
38883888
}
38893889
}

‎src/nvim/ex_docmd.c

Copy file name to clipboardExpand all lines: src/nvim/ex_docmd.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7810,8 +7810,8 @@ static void ex_checkhealth(exarg_T *eap)
78107810
return;
78117811
}
78127812

7813-
const char *vimruntime_env = os_getenv("VIMRUNTIME");
7814-
if (vimruntime_env == NULL) {
7813+
char *vimruntime_env = os_getenv_noalloc("VIMRUNTIME");
7814+
if (!vimruntime_env) {
78157815
emsg(_("E5009: $VIMRUNTIME is empty or unset"));
78167816
} else {
78177817
bool rtp_ok = NULL != strstr(p_rtp, vimruntime_env);

‎src/nvim/fileio.c

Copy file name to clipboardExpand all lines: src/nvim/fileio.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3278,7 +3278,7 @@ static void vim_mktempdir(void)
32783278
expand_env((char *)temp_dirs[i], tmp, TEMP_FILE_PATH_MAXLEN - 64);
32793279
if (!os_isdir(tmp)) {
32803280
if (strequal("$TMPDIR", temp_dirs[i])) {
3281-
if (!os_getenv("TMPDIR")) {
3281+
if (!os_env_exists("TMPDIR", true)) {
32823282
DLOG("$TMPDIR is unset");
32833283
} else {
32843284
WLOG("$TMPDIR tempdir not a directory (or does not exist): \"%s\"", tmp);

‎src/nvim/log.c

Copy file name to clipboardExpand all lines: src/nvim/log.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static bool v_do_log_to_file(FILE *log_file, int log_level, const char *context,
336336
// TODO(justinmk): expose this as v:name ?
337337
if (regen) {
338338
// Parent servername ($NVIM).
339-
const char *parent = path_tail(os_getenv(ENV_NVIM));
339+
const char *parent = path_tail(os_getenv_noalloc(ENV_NVIM));
340340
// Servername. Empty until starting=false.
341341
const char *serv = path_tail(get_vim_var_str(VV_SEND_SERVER));
342342
if (parent[0] != NUL) {

‎src/nvim/lua/executor.c

Copy file name to clipboardExpand all lines: src/nvim/lua/executor.c
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,7 @@ static bool nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
831831
void nlua_init(char **argv, int argc, int lua_arg0)
832832
{
833833
#ifdef NLUA_TRACK_REFS
834-
const char *env = os_getenv("NVIM_LUA_NOTRACK");
835-
if (!env || !*env) {
834+
if (os_env_exists("NVIM_LUA_NOTRACK", true)) {
836835
nlua_track_refs = true;
837836
}
838837
#endif
@@ -1283,7 +1282,7 @@ static int nlua_empty_dict_tostring(lua_State *lstate)
12831282
/// @param lstate Lua interpreter state.
12841283
static int nlua_getenv(lua_State *lstate)
12851284
{
1286-
lua_pushstring(lstate, os_getenv(luaL_checkstring(lstate, 1)));
1285+
lua_pushstring(lstate, os_getenv_noalloc(luaL_checkstring(lstate, 1)));
12871286
return 1;
12881287
}
12891288
#endif

‎src/nvim/main.c

Copy file name to clipboardExpand all lines: src/nvim/main.c
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,11 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
935935
if (!chan) {
936936
fprintf(stderr, "Remote ui failed to start: %s\n", connect_error);
937937
os_exit(1);
938-
} else if (strequal(server_addr, os_getenv("NVIM"))) {
938+
} else if (strequal(server_addr, os_getenv_noalloc("NVIM"))) {
939939
fprintf(stderr, "%s", "Cannot attach UI of :terminal child to its parent. ");
940940
fprintf(stderr, "%s\n", "(Unset $NVIM to skip this check)");
941941
os_exit(1);
942942
}
943-
944943
ui_client_channel_id = chan;
945944
return;
946945
}
@@ -2115,7 +2114,7 @@ static void source_startup_scripts(const mparm_T *const parmp)
21152114
static int execute_env(char *env)
21162115
FUNC_ATTR_NONNULL_ALL
21172116
{
2118-
const char *initstr = os_getenv(env);
2117+
char *initstr = os_getenv(env);
21192118
if (initstr == NULL) {
21202119
return FAIL;
21212120
}
@@ -2129,6 +2128,8 @@ static int execute_env(char *env)
21292128

21302129
estack_pop();
21312130
current_sctx = save_current_sctx;
2131+
2132+
xfree(initstr);
21322133
return OK;
21332134
}
21342135

‎src/nvim/mbyte.c

Copy file name to clipboardExpand all lines: src/nvim/mbyte.c
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,14 +2412,15 @@ char *enc_locale(void)
24122412
char buf[50];
24132413

24142414
const char *s;
2415+
24152416
#ifdef HAVE_NL_LANGINFO_CODESET
24162417
if (!(s = nl_langinfo(CODESET)) || *s == NUL)
24172418
#endif
24182419
{
24192420
if (!(s = setlocale(LC_CTYPE, NULL)) || *s == NUL) {
2420-
if ((s = os_getenv("LC_ALL"))) {
2421-
if ((s = os_getenv("LC_CTYPE"))) {
2422-
s = os_getenv("LANG");
2421+
if ((s = os_getenv_noalloc("LC_ALL"))) {
2422+
if ((s = os_getenv_noalloc("LC_CTYPE"))) {
2423+
s = os_getenv_noalloc("LANG");
24232424
}
24242425
}
24252426
}

‎src/nvim/memory.c

Copy file name to clipboardExpand all lines: src/nvim/memory.c
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,6 @@ void free_all_mem(void)
788788
free_all_marks();
789789
alist_clear(&global_alist);
790790
free_homedir();
791-
free_envmap();
792791
free_users();
793792
free_search_patterns();
794793
free_old_sub();

‎src/nvim/msgpack_rpc/server.c

Copy file name to clipboardExpand all lines: src/nvim/msgpack_rpc/server.c
+15-15Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,24 @@ bool server_init(const char *listen_addr)
4141
ga_init(&watchers, sizeof(SocketWatcher *), 1);
4242

4343
// $NVIM_LISTEN_ADDRESS (deprecated)
44-
if ((!listen_addr || listen_addr[0] == '\0') && os_env_exists(ENV_LISTEN)) {
45-
user_arg = kFalse; // User-provided env var.
46-
listen_addr = os_getenv(ENV_LISTEN);
47-
}
48-
4944
if (!listen_addr || listen_addr[0] == '\0') {
50-
user_arg = kNone; // Autogenerated server address.
51-
listen_addr = server_address_new(NULL);
45+
if (os_env_exists(ENV_LISTEN, true)) {
46+
user_arg = kFalse; // User-provided env var.
47+
listen_addr = os_getenv(ENV_LISTEN);
48+
} else {
49+
user_arg = kNone; // Autogenerated server address.
50+
listen_addr = server_address_new(NULL);
51+
}
5252
must_free = true;
5353
}
5454

5555
int rv = server_start(listen_addr);
5656

5757
// TODO(justinmk): this is for log_spec. Can remove this after nvim_log #7062 is merged.
58-
if (os_env_exists("__NVIM_TEST_LOG")) {
58+
if (os_env_exists("__NVIM_TEST_LOG", false)) {
5959
ELOG("test log message");
6060
}
6161

62-
if (must_free) {
63-
xfree((char *)listen_addr);
64-
}
65-
6662
if (rv == 0 || user_arg == kNone) {
6763
// The autogenerated servername can fail if the user has a broken $XDG_RUNTIME_DIR. #30282
6864
// But that is not fatal (startup will continue, logged in $NVIM_LOGFILE, empty v:servername).
@@ -78,12 +74,16 @@ bool server_init(const char *listen_addr)
7874
ok = false;
7975

8076
end:
81-
if (os_env_exists(ENV_LISTEN)) {
77+
if (os_env_exists(ENV_LISTEN, false)) {
8278
// Unset $NVIM_LISTEN_ADDRESS, it's a liability hereafter. It is "input only", it must not be
8379
// leaked to child jobs or :terminal.
8480
os_unsetenv(ENV_LISTEN);
8581
}
8682

83+
if (must_free) {
84+
xfree((char *)listen_addr);
85+
}
86+
8787
return ok;
8888
}
8989

@@ -120,12 +120,12 @@ char *server_address_new(const char *name)
120120
static uint32_t count = 0;
121121
char fmt[ADDRESS_MAX_SIZE];
122122
#ifdef MSWIN
123-
(void)get_appname(true);
123+
(void)get_appname(true); // Call to initialize NameBuf with appname
124124
int r = snprintf(fmt, sizeof(fmt), "\\\\.\\pipe\\%s.%" PRIu64 ".%" PRIu32,
125125
name ? name : NameBuff, os_get_pid(), count++);
126126
#else
127127
char *dir = stdpaths_get_xdg_var(kXDGRuntimeDir);
128-
(void)get_appname(true);
128+
(void)get_appname(true); // Call to initialize NameBuf with appname
129129
int r = snprintf(fmt, sizeof(fmt), "%s/%s.%" PRIu64 ".%" PRIu32,
130130
dir, name ? name : NameBuff, os_get_pid(), count++);
131131
xfree(dir);

‎src/nvim/option.c

Copy file name to clipboardExpand all lines: src/nvim/option.c
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,17 @@ static void set_init_default_shell(void)
186186
{
187187
// Find default value for 'shell' option.
188188
// Don't use it if it is empty.
189-
const char *shell = os_getenv("SHELL");
189+
char *shell = os_getenv("SHELL");
190190
if (shell != NULL) {
191191
if (vim_strchr(shell, ' ') != NULL) {
192192
const size_t len = strlen(shell) + 3; // two quotes and a trailing NUL
193193
char *const cmd = xmalloc(len);
194194
snprintf(cmd, len, "\"%s\"", shell);
195195
set_string_default(kOptShell, cmd, true);
196196
} else {
197-
set_string_default(kOptShell, (char *)shell, false);
197+
set_string_default(kOptShell, shell, false);
198198
}
199+
xfree(shell);
199200
}
200201
}
201202

@@ -409,7 +410,7 @@ void set_init_1(bool clean_arg)
409410
// abilities (bidi namely).
410411
// NOTE: mlterm's author is being asked to 'set' a variable
411412
// instead of an environment variable due to inheritance.
412-
if (os_env_exists("MLTERM")) {
413+
if (os_env_exists("MLTERM", false)) {
413414
set_option_value_give_err(kOptTermbidi, BOOLEAN_OPTVAL(true), 0);
414415
}
415416

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.