Skip to content

Commit

Permalink
realloc( ptr, 0); is equivalent to free( ptr); and returns NULL on mo…
Browse files Browse the repository at this point in the history
…st systems... but not FreeBSD, and isn't guaranteed by the C standards. This caused an assertion to trigger during endwin() on FreeBSD.
  • Loading branch information
Bill-Gray committed Jun 7, 2023
1 parent 0a4e1e9 commit abbb6de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion curses.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Defined by this header:
#define PDC_VER_CHANGE 6
#define PDC_VER_YEAR 2023
#define PDC_VER_MONTH 06
#define PDC_VER_DAY 01
#define PDC_VER_DAY 07

#define PDC_STRINGIZE( x) #x
#define PDC_stringize( x) PDC_STRINGIZE( x)
Expand Down
26 changes: 24 additions & 2 deletions pdcurses/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,36 @@ void PDC_sync(WINDOW *win)
wsyncup(win);
}

/* See the OpenBSD and FreeBSD reallocarray() extension. This is similar, but
exists for all platforms, and always frees the pointer and returns NULL for a
zero-byte allocation. realloc() does this on most platforms, but not FreeBSD,
and it's not guaranteed in the C specifications. */

static void *PDC_realloc_array( void *ptr, const size_t nmemb, const size_t size)
{
if( !nmemb || !size)
{
free( ptr);
ptr = NULL;
}
else
{
const size_t nbytes = nmemb * size;

assert( nbytes / size == nmemb);
ptr = realloc( ptr, nbytes);
}
return( ptr);
}

#define is_power_of_two( X) (!((X) & ((X) - 1)))

static void _resize_window_list( SCREEN *scr_ptr)
{
if( is_power_of_two( scr_ptr->opaque->n_windows))
scr_ptr->opaque->window_list =
(WINDOW **)realloc( scr_ptr->opaque->window_list,
scr_ptr->opaque->n_windows * 2 * sizeof( WINDOW *));
(WINDOW **)PDC_realloc_array( scr_ptr->opaque->window_list,
scr_ptr->opaque->n_windows * 2, sizeof( WINDOW *));
}

void PDC_add_window_to_list( WINDOW *win)
Expand Down

0 comments on commit abbb6de

Please sign in to comment.