From 26342982e13ed2377c9dc8ee0783a4f2affc3504 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sat, 21 Oct 2023 08:55:54 -0700 Subject: [PATCH] update_view_title: Fix NULL dereference at startup (#1293) update_view_title can be called with a `struct view` where `line` is NULL, and `lines` is 0. Add a check for the value of line where it's first used. --- src/view.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/view.c b/src/view.c index e64ef19f9..bf86fe3ac 100644 --- a/src/view.c +++ b/src/view.c @@ -687,7 +687,6 @@ void update_view_title(struct view *view) { WINDOW *window = view->title; - struct line *line = &view->line[view->pos.lineno]; unsigned int view_lines, lines; int update_increment = view_has_flags(view, VIEW_LOG_LIKE | VIEW_GREP_LIKE) ? 100 @@ -707,16 +706,18 @@ update_view_title(struct view *view) wprintw(window, " %s", view->ref); } - if (!view_has_flags(view, VIEW_CUSTOM_STATUS) && view_has_line(view, line) && - line->lineno) { - wprintw(window, " - %s %u of %zu", - view->ops->type, - line->lineno, - MAX(line->lineno, - view->pipe - ? update_increment * - (size_t) ((view->lines - view->custom_lines) / update_increment) - : view->lines - view->custom_lines)); + if (!view_has_flags(view, VIEW_CUSTOM_STATUS) && view->line != NULL) { + struct line *line = &view->line[view->pos.lineno]; + if (view_has_line(view, line) && line->lineno) { + wprintw(window, " - %s %u of %zu", + view->ops->type, + line->lineno, + MAX(line->lineno, + view->pipe + ? update_increment * + (size_t) ((view->lines - view->custom_lines) / update_increment) + : view->lines - view->custom_lines)); + } } if (view->pipe) {