Code

check the return value of mpdclient_get_connection()
authorMax Kellermann <max@duempel.org>
Sat, 17 Oct 2009 22:32:37 +0000 (00:32 +0200)
committerMax Kellermann <max@duempel.org>
Sat, 17 Oct 2009 22:32:37 +0000 (00:32 +0200)
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
src/screen_artist.c
src/screen_browser.c
src/screen_client.c
src/screen_file.c
src/screen_outputs.c
src/screen_queue.c
src/screen_song.c
src/utils.c

index 3b8476daf3924b08aec875c17ac8c74f5a7c876e..54664e58e9bdb8c6a5ccc78a7ec0f4f0a505d5ff 100644 (file)
@@ -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))
index a3bda330ef9fb22877fe6ce3347418d46837cfe1..892587bcf03ee25f32c3d4fe736e5e76ae1bd474 100644 (file)
@@ -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);
index 8802e5f93bb1eab29ed8d8ace445048a8fea495f..45b782a079378b5d2c3fbe84cae10e211c727fd2 100644 (file)
@@ -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..."),
index 90364c0af0df2506daba2839a171beb385de7482..0b3484e3a371582f7590092055f8d3a00bd6886c 100644 (file)
@@ -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) {
index d9faa69ac66430032a736b971de0211b9a8db7dc..67dc3e081182da8addac2436b3b77b54103b1e8b 100644 (file)
@@ -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 =
index 22337da61f8920e554ce9fc109f5ad02ea8016bf..4a5b2f4cdaf4d9a24d2396ff26a6d0e59f6271ed 100644 (file)
@@ -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);
index 750abb01d451b784bb1256c532406748d1a9f5dd..7275734d08c70ce2ebe05555a855303c489098f3 100644 (file)
@@ -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
index 3696cd23957fe6ca30efa88a74b0fa25995078b2..4c774e8833b8fe30ece23b47d4c7eb063276b8d0 100644 (file)
@@ -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);
index 6042848bced918cab0378fdd4764a7014f7c826b..84db64a5b122211081fc0df2a02e206411befb51 100644 (file)
@@ -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) {