commit fb5a51e77570ce2b548d22d250d503102aeac2fe
parent 835f11595fb057197fc867a137f0d5bb56e9dfcd
Author: Axel <axelhjq@gmail.com>
Date: Mon, 23 Jun 2025 16:07:52 +0200
fix(tui): avoid memory leak and compiler warning on Windows (#34225)
Problem: On Windows, the value of `term` is overwritten without freeing
the old allocated value, which may lead to a memory leak.
GCC also gives a "incompatible pointer type" warning about
passing `*term` to os_tty_guess_term().
Solution: Don't override the old allocated value, and copy the guessed
value to `term` if its old value is NULL.
Diffstat:
2 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
@@ -147,9 +147,6 @@ bool os_env_exists(const char *name, bool nonempty)
/// Sets an environment variable.
///
/// Windows (Vim-compat): Empty string (:let $FOO="") undefines the env var.
-///
-/// @warning Existing pointers to the result of os_getenv("foo") are
-/// INVALID after os_setenv("foo", …).
int os_setenv(const char *name, const char *value, int overwrite)
FUNC_ATTR_NONNULL_ALL
{
diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c
@@ -386,10 +386,12 @@ static void terminfo_start(TUIData *tui)
char *term = os_getenv("TERM");
#ifdef MSWIN
- os_tty_guess_term(&term, tui->out_fd);
- os_setenv("TERM", term, 1);
- // Old os_getenv() pointer is invalid after os_setenv(), fetch it again.
- term = os_getenv("TERM");
+ const char *guessed_term = NULL;
+ os_tty_guess_term(&guessed_term, tui->out_fd);
+ if (term == NULL && guessed_term != NULL) {
+ term = xstrdup(guessed_term);
+ os_setenv("TERM", guessed_term, 1);
+ }
#endif
// Set up unibilium/terminfo.