From f9c11816cba57f597c4ee000cf0fa69d9aad12a8 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Thu, 3 Aug 2023 10:08:18 +0200 Subject: [PATCH] erts: Fix prim_tty nif to compile with GNU termcap termcap.h contains the prototypes of the functions we need and is available in both curses and GNU termcap. However, GNU termcap has tputs defined as returning void intead of int, so we handle that by ignoring the return value. tputs cannot fail with the input we give it. closes #7381 --- erts/emulator/nifs/common/prim_tty_nif.c | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/erts/emulator/nifs/common/prim_tty_nif.c b/erts/emulator/nifs/common/prim_tty_nif.c index d5010bdf6803..84c42f444b89 100644 --- a/erts/emulator/nifs/common/prim_tty_nif.c +++ b/erts/emulator/nifs/common/prim_tty_nif.c @@ -43,8 +43,7 @@ #include #ifdef HAVE_TERMCAP #include -#include -#include +#include #endif #ifndef __WIN32__ #include @@ -710,11 +709,12 @@ static ERL_NIF_TERM tty_tgetstr_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM /* tgetstr seems to use a lot of stack buffer space, so buff needs to be relatively "small" */ char *str = NULL; - char buff[BUFSIZ] = {0}; + char buff_area[BUFSIZ] = {0}; + char *buff = (char*)buff_area; if (!enif_inspect_iolist_as_binary(env, argv[0], &TERM)) return enif_make_badarg(env); - str = tgetstr((char*)TERM.data, (char**)&buff); + str = tgetstr((char*)TERM.data, &buff); if (!str) return atom_false; enif_alloc_binary(strlen(str), &ret); memcpy(ret.data, str, strlen(str)); @@ -742,8 +742,11 @@ static int tty_puts_putc(int c) { static ERL_NIF_TERM tty_tgoto_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { #ifdef HAVE_TERMCAP ErlNifBinary TERM; + ERL_NIF_TERM ret; char *ent; int value1 = 0, value2 = 0; + unsigned char *buff; + if (!enif_inspect_iolist_as_binary(env, argv[0], &TERM) || (argc > 1 && !enif_get_int(env, argv[1], &value1)) || (argc > 2 && !enif_get_int(env, argv[2], &value2)) @@ -753,14 +756,12 @@ static ERL_NIF_TERM tty_tgoto_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM a if (!ent) return make_errno_error(env, "tgoto"); tputs_buffer_index = 0; - if (tputs(ent, 1, tty_puts_putc)) { - return make_errno_error(env, "tputs"); - } else { - ERL_NIF_TERM ret; - unsigned char *buff = enif_make_new_binary(env, tputs_buffer_index, &ret); - memcpy(buff, tputs_buffer, tputs_buffer_index); - return enif_make_tuple2(env, atom_ok, ret); - } + (void)tputs(ent, 1, tty_puts_putc); /* tputs only fails if ent is null, + which is cannot be. */ + + buff = enif_make_new_binary(env, tputs_buffer_index, &ret); + memcpy(buff, tputs_buffer, tputs_buffer_index); + return enif_make_tuple2(env, atom_ok, ret); #else return make_enotsup(env); #endif