iptstate icon indicating copy to clipboard operation
iptstate copied to clipboard

TIOCGWINSZ problem in 2.2.7 from pull request #11 SenH:fix_segfault_terminal_width

Open florian-lutz opened this issue 1 year ago • 0 comments

Hello, I found a problem in 2.2.7 with TIOCGWINSZ from pull request https://github.com/jaymzh/iptstate/pull/11 SenH:fix_segfault_terminal_width

The return valueof ioctl(0, TIOCGWINSZ, &w); is not checked. In a situation of input/output redirection (without a terminal) the function can fail with ENOTTY, and if also the COLS environment variable is not set (for example when used with https://github.com/kisst/zabbix-iptstate), then w.ws_col is used in its uninitialized state, normally producing a very large value for maxx. This leads to a long running loop, outputting spaces to fit a terminal with "millionsof columns". I didn't wait for it to finish... I could reproduce it on the command line:

unset COLS ; echo | /usr/sbin/iptstate -1

I want to propose the reintroduction of a hard-coded default value for maxx with a value of 85 as discussed in the comments to the pull request https://github.com/jaymzh/iptstate/pull/11, while also checking the return value of ioctl().

screensize_t get_size(const bool &single)
{
  int maxx = 0, maxy = 0;
  if (!single) {
    getmaxyx(stdscr, maxy, maxx);
  } else {
    maxx = 85;

    // https://stackoverflow.com/questions/1022957/
    struct winsize w;
    if (ioctl(0, TIOCGWINSZ, &w) == 0)
      maxx = w.ws_col;

    if (getenv("COLS"))
      maxx = atoi(getenv("COLS"));
  }

  screensize_t a;
  a.x = maxx;
  a.y = maxy;

  return a;
}

florian-lutz avatar Jan 22 '24 12:01 florian-lutz