From 323ee425fe07485577efee5636ba47efcc8b19fc Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 18 Oct 2009 00:32:37 +0200 Subject: [PATCH] check the return value of mpdclient_get_connection() When leaving idle mode, the idle callback (which is invoked indirectly by mpdclient_get_connection()) may close the connection. Checking mpdclient_is_connected() before mpdclient_get_connection() is pointless for that reason. --- src/player_command.c | 38 +++++++++++++++++++++++++++++++++++--- src/screen_artist.c | 3 +++ src/screen_browser.c | 3 +++ src/screen_client.c | 4 ++++ src/screen_file.c | 8 +++++--- src/screen_outputs.c | 10 ++++++---- src/screen_queue.c | 9 +++++++-- src/screen_song.c | 6 ++++-- src/utils.c | 4 ++-- 9 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/player_command.c b/src/player_command.c index 3b8476d..54664e5 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -37,13 +37,12 @@ commit_seek(struct mpdclient *c) if (seek_id < 0) return; - if (!mpdclient_is_connected(c)) { + connection = mpdclient_get_connection(c); + if (connection == NULL) { seek_id = -1; return; } - connection = mpdclient_get_connection(c); - if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song)) if (!mpd_run_seek_id(connection, seek_id, seek_target_time)) mpdclient_handle_error(c); @@ -102,12 +101,18 @@ handle_player_command(struct mpdclient *c, command_t cmd) */ case CMD_PAUSE: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_pause(connection, mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) mpdclient_handle_error(c); break; case CMD_STOP: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_stop(connection)) mpdclient_handle_error(c); break; @@ -129,6 +134,9 @@ handle_player_command(struct mpdclient *c, command_t cmd) break; case CMD_TRACK_NEXT: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_next(connection)) mpdclient_handle_error(c); break; @@ -147,11 +155,17 @@ handle_player_command(struct mpdclient *c, command_t cmd) break; case CMD_TRACK_PREVIOUS: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_previous(connection)) mpdclient_handle_error(c); break; case CMD_SHUFFLE: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (mpd_run_shuffle(connection)) screen_status_message(_("Shuffled playlist")); else @@ -159,35 +173,53 @@ handle_player_command(struct mpdclient *c, command_t cmd) break; case CMD_CLEAR: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (mpdclient_cmd_clear(c)) screen_status_message(_("Cleared playlist")); break; case CMD_REPEAT: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_repeat(connection, !mpd_status_get_repeat(c->status))) mpdclient_handle_error(c); break; case CMD_RANDOM: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_random(connection, !mpd_status_get_random(c->status))) mpdclient_handle_error(c); break; case CMD_SINGLE: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_single(connection, !mpd_status_get_single(c->status))) mpdclient_handle_error(c); break; case CMD_CONSUME: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_consume(connection, !mpd_status_get_consume(c->status))) mpdclient_handle_error(c); break; case CMD_CROSSFADE: connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (!mpd_run_crossfade(connection, mpd_status_get_crossfade(c->status) > 0 ? 0 : options.crossfade_time)) diff --git a/src/screen_artist.c b/src/screen_artist.c index a3bda33..892587b 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -418,6 +418,9 @@ add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter) assert(filter != NULL); + if (connection == NULL) + return; + str = utf8_to_locale(_filter); if (table == MPD_TAG_ALBUM) screen_status_printf("Adding album %s...", str); diff --git a/src/screen_browser.c b/src/screen_browser.c index 8802e5f..45b782a 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -123,6 +123,9 @@ load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist) { struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; + if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) { char *filename = utf8_to_locale(mpd_playlist_get_path(playlist)); screen_status_printf(_("Loading playlist %s..."), diff --git a/src/screen_client.c b/src/screen_client.c index 90364c0..0b3484e 100644 --- a/src/screen_client.c +++ b/src/screen_client.c @@ -31,6 +31,8 @@ _screen_auth(struct mpdclient *c, gint recursion) char *password; connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; mpd_connection_clear_error(connection); if (recursion > 2) @@ -80,6 +82,8 @@ screen_database_update(struct mpdclient *c, const char *path) assert(mpdclient_is_connected(c)); connection = mpdclient_get_connection(c); + if (connection == NULL) + return; id = mpd_run_update(connection, path); if (id == 0) { diff --git a/src/screen_file.c b/src/screen_file.c index d9faa69..67dc3e0 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -56,10 +56,9 @@ screen_file_load_list(struct mpdclient *c, struct filelist *filelist) { struct mpd_connection *connection; - if (!mpdclient_is_connected(c)) - return; - connection = mpdclient_get_connection(c); + if (connection == NULL) + return; mpd_send_list_meta(connection, current_path); filelist_recv(filelist, connection); @@ -209,6 +208,9 @@ handle_delete(struct mpdclient *c) char *str, *buf; int key; + if (connection == NULL) + return; + list_window_get_range(browser.lw, &range); for (unsigned i = range.start; i < range.end; ++i) { struct filelist_entry *entry = diff --git a/src/screen_outputs.c b/src/screen_outputs.c index 22337da..4a5b2f4 100644 --- a/src/screen_outputs.c +++ b/src/screen_outputs.c @@ -52,11 +52,13 @@ toggle_output(struct mpdclient *c, unsigned int output_index) assert(mpd_outputs != NULL); - if (!mpdclient_is_connected(c) || - output_index >= mpd_outputs->len) + if (output_index >= mpd_outputs->len) return false; connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; + output = g_ptr_array_index(mpd_outputs, output_index); if (!mpd_output_get_enabled(output)) { @@ -113,10 +115,10 @@ fill_outputs_list(struct mpdclient *c) assert(mpd_outputs != NULL); - if (!mpdclient_is_connected(c)) + connection = mpdclient_get_connection(c); + if (connection == NULL) return; - connection = mpdclient_get_connection(c); mpd_send_outputs(connection); while ((output = mpd_recv_output(connection)) != NULL) { g_ptr_array_add(mpd_outputs, output); diff --git a/src/screen_queue.c b/src/screen_queue.c index 750abb0..7275734 100644 --- a/src/screen_queue.c +++ b/src/screen_queue.c @@ -309,9 +309,11 @@ playlist_save(struct mpdclient *c, char *name, char *defaultname) /* send save command to mpd */ - filename_utf8 = locale_to_utf8(filename); - connection = mpdclient_get_connection(c); + if (connection == NULL) + return -1; + + filename_utf8 = locale_to_utf8(filename); if (!mpd_run_save(connection, filename_utf8)) { if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER && mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST && @@ -750,6 +752,9 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd) break; connection = mpdclient_get_connection(c); + if (connection == NULL) + return true; + if (mpd_run_shuffle_range(connection, range.start, range.end)) screen_status_message(_("Shuffled playlist")); else diff --git a/src/screen_song.c b/src/screen_song.c index 3696cd2..4c774e8 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -331,6 +331,8 @@ screen_song_add_stats(struct mpd_connection *connection) static void screen_song_update(struct mpdclient *c) { + struct mpd_connection *connection; + /* Clear all lines */ for (guint i = 0; i < current.lines->len; ++i) g_free(g_ptr_array_index(current.lines, i)); @@ -368,8 +370,8 @@ screen_song_update(struct mpdclient *c) } /* Add some statistics about mpd */ - if (mpdclient_is_connected(c) && - !screen_song_add_stats(mpdclient_get_connection(c))) + connection = mpdclient_get_connection(c); + if (connection != NULL && !screen_song_add_stats(connection)) mpdclient_handle_error(c); list_window_set_length(lw, current.lines->len); diff --git a/src/utils.c b/src/utils.c index 6042848..84db64a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -79,10 +79,10 @@ gcmp_list_from_path(struct mpdclient *c, const gchar *path, struct mpd_connection *connection; struct mpd_entity *entity; - if (!mpdclient_is_connected(c)) + connection = mpdclient_get_connection(c); + if (connection == NULL) return list; - connection = mpdclient_get_connection(c); mpd_send_list_meta(connection, path); while ((entity = mpd_recv_entity(connection)) != NULL) { -- 2.30.2