bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: segmentation fault inside waddch_literal()


From: Tetsuo Handa
Subject: Re: segmentation fault inside waddch_literal()
Date: Fri, 16 Jul 2021 14:41:10 +0900
User-agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0

On 2021/07/16 3:51, Thomas Dickey wrote:
> On Thu, Jul 15, 2021 at 10:03:01PM +0900, Tetsuo Handa wrote:
>> Hello.
>>
>> I received a bug report that my program crashes on multiple terminals
>> including TERM=xterm-256color and TERM=linux .
> ...
>> Since it seems that the result differs depending on build config
>> options, I worry that this bug might involve memory corruption.
>>
>> Any idea how to debug this problem? (I'm not familiar with ncurses.)
> 
> I don't see a sample program which reproduces the problem.
> 
> https://invisible-island.net/ncurses/ncurses.faq.html#how_to_report
> https://invisible-island.net/personal/bug-reports.html
> 

I managed to develop a sample program (shown below).
My program (which runs on Linux) is built with symbol _GNU_SOURCE defined
in order to make Unix/Linux specific extensions visible. As a result,
curses.h is #include'd with symbol _GNU_SOURCE defined.

When this sample is built with -D_GNU_SOURCE, segmentation fault happens.
When this sample is built without -D_GNU_SOURCE, it seems to work fine.
I have no idea why this makes run-time difference.

Please run this sample on a terminal with TERM=xterm-256color or TERM=linux
or TERM=xterm . Possibly reproducible on other terminals as well.

$ gcc --version
gcc (Mageia 10.3.0-1.mga8) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

----------------------------------------
/* gcc -Wall -O0 -g -DCOLOR_ON -D_GNU_SOURCE -o sample1 sample1.c -lncurses 
-ltinfo */

#include <curses.h>

enum color_type {
        COLOR_DOMAIN_HEAD = 1,
        COLOR_DOMAIN_CURSOR,
        COLOR_DEFAULT_COLOR,
        COLOR_DISP_ERR
};

#ifdef COLOR_ON

static attr_t saved_color = COLOR_DEFAULT_COLOR;

static void editpolicy_color_init(void)
{
        static struct ccs_color_env_t {
                enum color_type tag;
                short int fore;
                short int back;
        } color_env[] = {
                { COLOR_DOMAIN_HEAD,   COLOR_BLACK, COLOR_GREEN },
                { COLOR_DOMAIN_CURSOR, COLOR_BLACK, COLOR_GREEN },
                { COLOR_DEFAULT_COLOR, COLOR_WHITE, COLOR_BLACK },
        };
        int i;

        start_color();
        for (i = 0; i < 3; i++) {
                struct ccs_color_env_t *colorp = &color_env[i];
                init_pair(colorp->tag, colorp->fore, colorp->back);
        }
        init_pair(COLOR_DISP_ERR, COLOR_RED, COLOR_BLACK); /* error message */
        bkgdset(A_NORMAL | COLOR_PAIR(COLOR_DEFAULT_COLOR) | ' ');
}

static void editpolicy_color_change(const attr_t attr, const _Bool flg)
{
        if (flg)
                attron(COLOR_PAIR(attr));
        else
                attroff(COLOR_PAIR(attr));
}

static void editpolicy_attr_change(const attr_t attr, const _Bool flg)
{
        if (flg)
                attron(attr);
        else
                attroff(attr);
}

static void editpolicy_sttr_save(void) { saved_color = getattrs(stdscr); }

static void editpolicy_sttr_restore(void) { attrset(saved_color); }

static void editpolicy_line_draw(void)
{
        int y;
        int x;

        getyx(stdscr, y, x);
        move(y, x);
        chgat(-1, A_NORMAL, COLOR_DOMAIN_CURSOR, NULL);
        touchwin(stdscr);
}

#else

static void editpolicy_color_init(void) { }
static void editpolicy_color_change(const attr_t attr, const _Bool flg) { }
static void editpolicy_attr_change(const attr_t attr, const _Bool flg) { }
static void editpolicy_sttr_save(void) { }
static void editpolicy_sttr_restore(void) { }
static void editpolicy_line_draw(void) { }

#endif

#define CCS_HEADER_LINES 3

static void show_current(void)
{
        move(2, 0);
        clrtoeol();
        editpolicy_attr_change(A_REVERSE, true);
        printw("<kernel>");
        editpolicy_attr_change(A_REVERSE, false);
        move(CCS_HEADER_LINES, 0);
        editpolicy_line_draw();
        refresh();
}

static int window_height;

static void show_list(void)
{
        int i;

        clear();
        move(0, 0);
        if (window_height < CCS_HEADER_LINES + 1) {
                printw("Please enlarge window.");
                clrtobot();
                refresh();
                return;
        }
        editpolicy_color_change(COLOR_DOMAIN_HEAD, true);
        printw("<<< Domain Transition Editor >>>      1 domain ");
        printw("   '?' for help");
        editpolicy_color_change(COLOR_DOMAIN_HEAD, false);
        for (i = 0; i < window_height - CCS_HEADER_LINES; i++) {
                const int index = i;
                if (index >= 1)
                        break;
                move(CCS_HEADER_LINES + i, 0);
                printw("              <kernel>");
                clrtoeol();
        }
        show_current();
}

static void select_item(void)
{
        int x;
        int y;

        getyx(stdscr, y, x);
        editpolicy_sttr_save();
        show_list();
        editpolicy_sttr_restore();
        move(y, x);
}

static void delete_entry(void)
{
        move(1, 0);
        editpolicy_color_change(COLOR_DISP_ERR, true);
        select_item();
        printw("Delete selected domain? ('Y'es/'N'o)");
        editpolicy_color_change(COLOR_DISP_ERR, false);
        clrtoeol();
        refresh();
}

int main(int argc, char *argv[])
{
        int x;

        initscr();
        editpolicy_color_init();
        cbreak();
        noecho();
        nonl();
        intrflush(stdscr, FALSE);
        keypad(stdscr, TRUE);
        getmaxyx(stdscr, window_height, x);

        show_list();
        delete_entry();

        clear();
        move(0, 0);
        refresh();
        endwin();
        return 0;
}
----------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]