Skip to content

Commit

Permalink
Move SIGWINCH handling from handler to NCURSES
Browse files Browse the repository at this point in the history
Use NCURSES internal SIGWINCH handling to generate KEY_RESIZE.
Use KEY_RESIZE to trigger window resize instead of SIGWINCH.

This fixes a crash on some platforms during resize introduced
in b622084 - it's not safe to allocate or reallocate memory
during a signal handler.

fixes #51
  • Loading branch information
rsaxvc authored and prigaux committed Oct 29, 2021
1 parent 33a2122 commit 783fc81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
18 changes: 0 additions & 18 deletions display.c
Expand Up @@ -100,30 +100,12 @@ int computeCursorXPos(int cursor, int hexOrAscii)
/*******************************************************************************/
/* Curses functions */
/*******************************************************************************/
static void handleSigWinch(int sig)
{
/*Close and refresh window to get new size*/
endwin();
refresh();

/*Reset to trigger recalculation*/
lineLength = 0;

clear();
initDisplay();
readFile();
display();
}

void initCurses(void)
{
struct sigaction sa;
initscr();

memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handleSigWinch;
sigaction(SIGWINCH, &sa, NULL);

#ifdef HAVE_COLORS
if (colored) {
start_color();
Expand Down
16 changes: 14 additions & 2 deletions interact.c
Expand Up @@ -341,9 +341,21 @@ int key_to_function(int key)

switch (key)
{
case ERR:
case KEY_RESIZE:
/*no-op*/
/*Close and refresh window to get new size*/
endwin();
refresh();

/*Reset to trigger recalculation*/
lineLength = 0;

clear();
initDisplay();
readFile();
display();
break;

case ERR:
break;

case KEY_RIGHT:
Expand Down

0 comments on commit 783fc81

Please sign in to comment.