Skip to content

Commit fd3d7f3

Browse files
committed
stdlib: fix memory leak issue with setupterm call from prim_tty_nif
1 parent 85d6340 commit fd3d7f3

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

erts/emulator/nifs/common/prim_tty_nif.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,13 +766,19 @@ static ERL_NIF_TERM setlocale_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM a
766766
return atom_false;
767767
#endif
768768
}
769-
769+
#ifdef HAVE_TERMCAP
770+
static TERMINAL *saved_term = NULL;
771+
#endif
770772
static ERL_NIF_TERM tty_setupterm_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
771773
#ifdef HAVE_TERMCAP
772774
int errret;
773775
if (setupterm(NULL, -1, &errret) < 0) {
774776
return make_errno_error(env, "setupterm");
775777
}
778+
if (saved_term) {
779+
del_curterm(saved_term);
780+
}
781+
saved_term = cur_term;
776782
return atom_ok;
777783
#else
778784
return make_enotsup(env);
@@ -1257,7 +1263,12 @@ static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
12571263

12581264
static void unload(ErlNifEnv* env, void* priv_data)
12591265
{
1260-
1266+
#ifdef HAVE_TERMCAP
1267+
if (saved_term) {
1268+
del_curterm(saved_term);
1269+
saved_term = NULL;
1270+
}
1271+
#endif
12611272
}
12621273

12631274
static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data,

0 commit comments

Comments
 (0)