From 254f6648bdcd05aa477da05627d1f99b528ec894 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 10 Oct 2009 16:13:55 +0200 Subject: [PATCH] list_window: don't invoke callback out-of-range For screen rows after the end of the list, don't invoke the callback. We know the length of the list, and we know that the callback will return NULL anyway. Optimization: after the end of the list, call wclrtobot() to clear all of the remaining visible (empty) lines. --- src/list_window.c | 49 +++++++++++++++++++++++++++----------------- src/mpdclient.c | 2 +- src/screen_artist.c | 3 +-- src/screen_browser.c | 4 ++-- src/screen_help.c | 7 +++---- src/screen_keydef.c | 33 +++++++++++++++-------------- src/screen_outputs.c | 4 +--- src/screen_play.c | 4 ++-- src/screen_search.c | 3 +-- src/screen_song.c | 3 +-- src/screen_text.c | 3 +-- 11 files changed, 61 insertions(+), 54 deletions(-) diff --git a/src/list_window.c b/src/list_window.c index 3c7c29a..c3ecc72 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -418,21 +418,25 @@ list_window_paint(struct list_window *lw, char *second_column = NULL; highlight = false; - label = callback(lw->start + i, &highlight, &second_column, callback_data); wmove(lw->w, i, 0); - if (label) { - list_window_paint_row(lw->w, i, lw->cols, - show_cursor && - lw->start + i >= lw->selected_start && - lw->start + i <= lw->selected_end, - highlight, - label, second_column); - - if (second_column != NULL) - g_free(second_column); - } else - wclrtoeol(lw->w); + if (lw->start + i >= lw->length) { + wclrtobot(lw->w); + break; + } + + label = callback(lw->start + i, &highlight, &second_column, callback_data); + assert(label != NULL); + + list_window_paint_row(lw->w, i, lw->cols, + show_cursor && + lw->start + i >= lw->selected_start && + lw->start + i <= lw->selected_end, + highlight, + label, second_column); + + if (second_column != NULL) + g_free(second_column); } if (options.hardware_cursor && lw->selected >= lw->start && @@ -455,7 +459,10 @@ list_window_find(struct list_window *lw, const char *label; do { - while ((label = callback(i,&h,NULL,callback_data))) { + while (i < lw->length) { + label = callback(i, &h, NULL, callback_data); + assert(label != NULL); + if (str && label && match_line(label, str)) { lw->selected = i; if(!lw->range_selection || i > lw->selected_end) @@ -497,7 +504,10 @@ list_window_rfind(struct list_window *lw, return false; do { - while (i >= 0 && (label = callback(i,&h,NULL,callback_data))) { + while (i >= 0) { + label = callback(i, &h, NULL, callback_data); + assert(label != NULL); + if( str && label && match_line(label, str) ) { lw->selected = i; if(!lw->range_selection || i > (int)lw->selected_end) @@ -528,11 +538,13 @@ list_window_jump(struct list_window *lw, const char *str) { bool h; - unsigned i = 0; const char *label; - while ((label = callback(i,&h,NULL,callback_data))) { - if (label && label[0] == '[') + for (unsigned i = 0; i < lw->length; ++i) { + label = callback(i, &h, NULL, callback_data); + assert(label != NULL); + + if (label[0] == '[') label++; #ifndef NCMPC_MINI if (str && label && @@ -549,7 +561,6 @@ list_window_jump(struct list_window *lw, lw->selected_start = i; return true; } - i++; } return false; } diff --git a/src/mpdclient.c b/src/mpdclient.c index d000df1..a82278a 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -136,7 +136,7 @@ mpdclient_disconnect(struct mpdclient *c) c->song = NULL; /* everything has changed after a disconnect */ - c->idle |= MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST| + c->events |= MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST| MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT| MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE; } diff --git a/src/screen_artist.c b/src/screen_artist.c index 6169775..cb6629a 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -79,8 +79,7 @@ screen_artist_lw_callback(unsigned idx, G_GNUC_UNUSED bool *highlight, --idx; } - if (idx >= list->len) - return NULL; + assert(idx < list->len); str_utf8 = g_ptr_array_index(list, idx); assert(str_utf8 != NULL); diff --git a/src/screen_browser.c b/src/screen_browser.c index dd97bb2..a19fa84 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -80,8 +80,8 @@ browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_c const struct filelist_entry *entry; const struct mpd_entity *entity; - if (fl == NULL || idx >= filelist_length(fl)) - return NULL; + assert(fl != NULL); + assert(idx < filelist_length(fl)); entry = filelist_get(fl, idx); assert(entry != NULL); diff --git a/src/screen_help.c b/src/screen_help.c index eb738c3..cf2a40f 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -25,6 +25,8 @@ #include +#include + typedef struct { signed char highlight; command_t command; @@ -177,8 +179,6 @@ static help_text_row_t help_text[] = { #endif }; -#define help_text_rows (sizeof(help_text) / sizeof(help_text[0])) - static struct list_window *lw; static const char * @@ -186,8 +186,7 @@ list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char** second_column, { static char buf[512]; - if (idx >= help_text_rows) - return NULL; + assert(idx < G_N_ELEMENTS(help_text)); if (help_text[idx].highlight) *highlight = true; diff --git a/src/screen_keydef.c b/src/screen_keydef.c index 6585bc4..71efe48 100644 --- a/src/screen_keydef.c +++ b/src/screen_keydef.c @@ -26,6 +26,7 @@ #include "screen.h" #include "screen_utils.h" +#include #include #include #include @@ -194,33 +195,35 @@ list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char** sc, G_GNUC_UNU static char buf[BUFSIZE]; if (subcmd < 0) { - if (idx < (unsigned)command_list_length) { - if (cmds[idx].flags & COMMAND_KEY_CONFLICT) - *highlight = true; - return cmds[idx].name; - } else if (idx == LIST_ITEM_APPLY()) + if (idx == LIST_ITEM_APPLY()) return LIST_ITEM_APPLY_LABEL; else if (idx == LIST_ITEM_SAVE()) return LIST_ITEM_SAVE_LABEL; + + assert(idx < (unsigned)command_list_length); + + if (cmds[idx].flags & COMMAND_KEY_CONFLICT) + *highlight = true; + return cmds[idx].name; } else { if (idx == 0) return "[..]"; idx--; - if (idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0) { - g_snprintf(buf, - BUFSIZE, "%d. %-20s (%d) ", - idx + 1, - key2str(cmds[subcmd].keys[idx]), - cmds[subcmd].keys[idx]); - return buf; - } else if (idx == subcmd_addpos) { + if (idx == subcmd_addpos) { g_snprintf(buf, BUFSIZE, "%d. %s", idx + 1, _("Add new key")); return buf; } - } - return NULL; + assert(idx < MAX_COMMAND_KEYS && cmds[subcmd].keys[idx] > 0); + + g_snprintf(buf, + BUFSIZE, "%d. %-20s (%d) ", + idx + 1, + key2str(cmds[subcmd].keys[idx]), + cmds[subcmd].keys[idx]); + return buf; + } } static void diff --git a/src/screen_outputs.c b/src/screen_outputs.c index 846a22b..a1ee33d 100644 --- a/src/screen_outputs.c +++ b/src/screen_outputs.c @@ -133,9 +133,7 @@ outputs_list_callback(unsigned int output_index, bool *highlight, struct mpd_output *output; assert(mpd_outputs != NULL); - - if (output_index >= mpd_outputs->len) - return NULL; + assert(output_index < mpd_outputs->len); output = g_ptr_array_index(mpd_outputs, output_index); diff --git a/src/screen_play.c b/src/screen_play.c index 05457f7..37825e6 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -136,8 +136,8 @@ list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED static char songname[MAX_SONG_LENGTH]; struct mpd_song *song; - if (playlist == NULL || idx >= playlist_length(playlist)) - return NULL; + assert(playlist != NULL); + assert(idx < playlist_length(playlist)); song = playlist_get(playlist, idx); if ((int)mpd_song_get_id(song) == current_song_id) diff --git a/src/screen_search.c b/src/screen_search.c index dc4b89d..24a6a91 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -111,8 +111,7 @@ static const char * lw_search_help_callback(unsigned idx, G_GNUC_UNUSED bool *highlight, G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data) { - if (idx >= G_N_ELEMENTS(help_text)) - return NULL; + assert(idx < G_N_ELEMENTS(help_text)); return help_text[idx]; } diff --git a/src/screen_song.c b/src/screen_song.c index 28d7802..b7ccaf0 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -82,8 +82,7 @@ screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight, static char buffer[256]; char *value; - if (idx >= current.lines->len) - return NULL; + assert(idx < current.lines->len); value = utf8_to_locale(g_ptr_array_index(current.lines, idx)); g_strlcpy(buffer, value, sizeof(buffer)); diff --git a/src/screen_text.c b/src/screen_text.c index 4660f28..a80b562 100644 --- a/src/screen_text.c +++ b/src/screen_text.c @@ -87,8 +87,7 @@ screen_text_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight, static char buffer[256]; char *value; - if (idx >= text->lines->len) - return NULL; + assert(idx < text->lines->len); value = utf8_to_locale(g_ptr_array_index(text->lines, idx)); g_strlcpy(buffer, value, sizeof(buffer)); -- 2.30.2