From 55662513e14c026eb4cfa044ed772b31be5d6ab9 Mon Sep 17 00:00:00 2001 From: Chris Davis Date: Mon, 16 Jun 2014 17:36:16 -0700 Subject: regedit: handle awkward window sizes better This fixes some assertion failures and an infinte loop that occurs when the terminal window is shrunk down far enough to the point regedit can't fit everything on screen. Signed-off-by: Chris Davis Reviewed-by: Andreas Schneider Reviewed-by: Michael Adam --- source3/utils/regedit_list.c | 23 +++++++++++------------ source3/utils/regedit_treeview.c | 7 +++++++ source3/utils/regedit_valuelist.c | 7 +++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/source3/utils/regedit_list.c b/source3/utils/regedit_list.c index 417dea8abc..ac4e24091c 100644 --- a/source3/utils/regedit_list.c +++ b/source3/utils/regedit_list.c @@ -271,6 +271,7 @@ static WERROR put_data(struct multilist *list) return WERR_OK; } +#define MIN_WIDTH 3 static struct multilist_column *find_widest_column(struct multilist *list) { unsigned col; @@ -285,6 +286,10 @@ static struct multilist_column *find_widest_column(struct multilist *list) } } + if (colp->width < MIN_WIDTH) { + return NULL; + } + return colp; } @@ -295,6 +300,7 @@ static WERROR calc_column_widths(struct multilist *list) size_t len; const char *item; size_t width, total_width, overflow; + struct multilist_column *colp; /* calculate the maximum widths for each column */ for (col = 0; col < list->ncols; ++col) { @@ -335,19 +341,12 @@ static WERROR calc_column_widths(struct multilist *list) } overflow = total_width - list->window_width; - /* the window is so narrow that no amount of trimming will - help fit the data. just give up. */ - if (overflow > width) { - return WERR_OK; - } - /* keep trimming from the widest column until the row fits */ - while (overflow) { - struct multilist_column *colp = find_widest_column(list); - size_t max_trim = colp->width / 3; - size_t trim = MIN(overflow, max_trim); - colp->width -= trim; - overflow -= trim; + /* attempt to trim as much as possible to fit all the columns to + the window */ + while (overflow && (colp = find_widest_column(list))) { + colp->width--; + overflow--; } return WERR_OK; diff --git a/source3/utils/regedit_treeview.c b/source3/utils/regedit_treeview.c index 47765c16de..9962ad99fb 100644 --- a/source3/utils/regedit_treeview.c +++ b/source3/utils/regedit_treeview.c @@ -445,7 +445,14 @@ void tree_view_resize(struct tree_view *view, int nlines, int ncols, WINDOW *nwin, *nsub; nwin = newwin(nlines, ncols, begin_y, begin_x); + if (nwin == NULL) { + return; + } nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1); + if (nsub == NULL) { + delwin(nwin); + return; + } replace_panel(view->panel, nwin); delwin(view->sub); delwin(view->window); diff --git a/source3/utils/regedit_valuelist.c b/source3/utils/regedit_valuelist.c index 1c80c0911b..9557c8ca6a 100644 --- a/source3/utils/regedit_valuelist.c +++ b/source3/utils/regedit_valuelist.c @@ -183,7 +183,14 @@ void value_list_resize(struct value_list *vl, int nlines, int ncols, WINDOW *nwin, *nsub; nwin = newwin(nlines, ncols, begin_y, begin_x); + if (nwin == NULL) { + return; + } nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1); + if (nsub == NULL) { + delwin(nwin); + return; + } replace_panel(vl->panel, nwin); delwin(vl->sub); delwin(vl->window); -- cgit