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.
This commit is contained in:
Axel authored and GitHub committed 2025-06-23 22:07:52 +08:00
1 parent 835f11595f
commit fb5a51e775
2 files changed
+6 -7

No files matched your search

-3
View File
@@ -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
{
+6 -4
View File
@@ -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.