Code

screen: check MPD status only if connected
authorMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 23:15:20 +0000 (01:15 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 23:15:20 +0000 (01:15 +0200)
Fix several segmentation faults: when the connection to the MPD server
is lost, there were NULL pointer dereferences because
client->status==NULL.  Check before accessing it.

src/screen.c
src/screen_file.c
src/screen_play.c

index 14ffffe6b5828dff9bf4177ad58945bc5cd27925..beddb95b823c6cc91f617662fcaedfd4cdb9aa2f 100644 (file)
@@ -231,7 +231,7 @@ paint_top_window2(const char *header, mpdclient_t *c)
                waddstr(w, _(":Lyrics  "));
 #endif
        }
-       if (c->status->volume==MPD_STATUS_NO_VOLUME) {
+       if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
                g_snprintf(buf, 32, _("Volume n/a "));
        } else {
                g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
@@ -240,14 +240,17 @@ paint_top_window2(const char *header, mpdclient_t *c)
        mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
 
        flags[0] = 0;
-       if( c->status->repeat )
-               g_strlcat(flags, "r", sizeof(flags));
-       if( c->status->random )
-               g_strlcat(flags, "z", sizeof(flags));;
-       if( c->status->crossfade )
-               g_strlcat(flags, "x", sizeof(flags));
-       if( c->status->updatingDb )
-               g_strlcat(flags, "U", sizeof(flags));
+       if (c->status != NULL) {
+               if (c->status->repeat)
+                       g_strlcat(flags, "r", sizeof(flags));
+               if (c->status->random)
+                       g_strlcat(flags, "z", sizeof(flags));;
+               if (c->status->crossfade)
+                       g_strlcat(flags, "x", sizeof(flags));
+               if (c->status->updatingDb)
+                       g_strlcat(flags, "U", sizeof(flags));
+       }
+
        colors_use(w, COLOR_LINE);
        mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
        if (flags[0]) {
@@ -278,7 +281,8 @@ paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
                wclrtoeol(w);
        }
 
-       if (prev_volume!=c->status->volume || full_repaint)
+       if ((c->status != NULL && prev_volume != c->status->volume) ||
+           full_repaint)
                paint_top_window2(header, c);
 }
 
@@ -287,7 +291,7 @@ paint_progress_window(mpdclient_t *c)
 {
        double p;
        int width;
-       int elapsedTime = c->status->elapsedTime;
+       int elapsedTime;
 
        if (c->status==NULL || IS_STOPPED(c->status->state)) {
                mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
@@ -298,6 +302,8 @@ paint_progress_window(mpdclient_t *c)
 
        if (c->song && seek_id == c->song->id)
                elapsedTime = seek_target_time;
+       else
+               elapsedTime = c->status->elapsedTime;
 
        p = ((double) elapsedTime) / ((double) c->status->totalTime);
 
@@ -328,7 +334,7 @@ paint_status_window(mpdclient_t *c)
        wclrtoeol(w);
        colors_use(w, COLOR_STATUS_BOLD);
 
-       switch(status->state) {
+       switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
        case MPD_STATUS_STATE_PLAY:
                str = _("Playing:");
                break;
@@ -347,7 +353,8 @@ paint_status_window(mpdclient_t *c)
 
        /* create time string */
        memset(screen.buf, 0, screen.buf_size);
-       if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
+       if (status != NULL && (IS_PLAYING(status->state) ||
+                              IS_PAUSED(status->state))) {
                if (status->totalTime > 0) {
                        /*checks the conf to see whether to display elapsed or remaining time */
                        if(!strcmp(options.timedisplay_type,"elapsed"))
@@ -381,7 +388,8 @@ paint_status_window(mpdclient_t *c)
        }
 
        /* display song */
-       if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
+       if (status != NULL && (IS_PLAYING(status->state) ||
+                              IS_PAUSED(status->state))) {
                char songname[MAX_SONGNAME_LENGTH];
                int width = COLS-x-my_strlen(screen.buf);
 
@@ -682,35 +690,37 @@ screen_update(mpdclient_t *c)
                return screen_paint(c);
 
        /* print a message if mpd status has changed */
-       if (repeat < 0) {
-               repeat = c->status->repeat;
-               random_enabled = c->status->random;
-               crossfade = c->status->crossfade;
-               dbupdate = c->status->updatingDb;
-       }
+       if (c->status != NULL) {
+               if (repeat < 0) {
+                       repeat = c->status->repeat;
+                       random_enabled = c->status->random;
+                       crossfade = c->status->crossfade;
+                       dbupdate = c->status->updatingDb;
+               }
 
-       if (repeat != c->status->repeat)
-               screen_status_printf(c->status->repeat ?
-                                    _("Repeat is on") :
-                                    _("Repeat is off"));
+               if (repeat != c->status->repeat)
+                       screen_status_printf(c->status->repeat ?
+                                            _("Repeat is on") :
+                                            _("Repeat is off"));
 
-       if (random_enabled != c->status->random)
-               screen_status_printf(c->status->random ?
-                                    _("Random is on") :
-                                    _("Random is off"));
+               if (random_enabled != c->status->random)
+                       screen_status_printf(c->status->random ?
+                                            _("Random is on") :
+                                            _("Random is off"));
 
-       if (crossfade != c->status->crossfade)
-               screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
+               if (crossfade != c->status->crossfade)
+                       screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
 
-       if (dbupdate && dbupdate != c->status->updatingDb) {
-               screen_status_printf(_("Database updated!"));
-               mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
-       }
+               if (dbupdate && dbupdate != c->status->updatingDb) {
+                       screen_status_printf(_("Database updated!"));
+                       mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
+               }
 
-       repeat = c->status->repeat;
-       random_enabled = c->status->random;
-       crossfade = c->status->crossfade;
-       dbupdate = c->status->updatingDb;
+               repeat = c->status->repeat;
+               random_enabled = c->status->random;
+               crossfade = c->status->crossfade;
+               dbupdate = c->status->updatingDb;
+       }
 
        /* update title/header window */
        if (welcome && screen.last_cmd==CMD_NONE &&
@@ -798,6 +808,9 @@ screen_get_mouse_event(mpdclient_t *c,
 static int
 screen_client_cmd(mpdclient_t *c, command_t cmd)
 {
+       if (c->connection == NULL || c->status == NULL)
+               return 0;
+
        switch(cmd) {
        case CMD_PLAY:
                mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
index 7465ff1255703d3f47b0a7884579f09c99e0d713..92d89232b768989ee5e1a4858544d6416311f2df 100644 (file)
@@ -706,6 +706,9 @@ browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
                        screen_status_printf(_("Screen updated!"));
                        return 1;
                case CMD_DB_UPDATE:
+                       if (c->status == NULL)
+                               return 1;
+
                        if( !c->status->updatingDb )
                                {
                                        if( mpdclient_cmd_db_update_utf8(c,filelist->path)==0 )
index 75462ccf95481e7c60dd48743c5d6cbbba1e7b7f..c89d150cff17c59beeaae70eddb5ff9ae095ca84 100644 (file)
@@ -85,9 +85,10 @@ list_callback(unsigned idx, int *highlight, void *data)
 
        song = playlist_get(&c->playlist, idx);
 
-       if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) {
+       if (c->song != NULL && song->id == c->song->id &&
+           c->status != NULL && !IS_STOPPED(c->status->state))
                *highlight = 1;
-       }
+
        strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
        return songname;
 }
@@ -99,7 +100,8 @@ center_playing_item(mpdclient_t *c)
        unsigned offset = lw->selected - lw->start;
        int idx;
 
-       if (!c->song || length<lw->rows || IS_STOPPED(c->status->state))
+       if (!c->song || length < lw->rows ||
+           c->status == NULL || IS_STOPPED(c->status->state))
                return;
 
        /* try to center the song that are playing */
@@ -378,7 +380,8 @@ static void
 play_update(screen_t *screen, mpdclient_t *c)
 {
        /* hide the cursor when mpd are playing and the user are inactive */
-       if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
+       if (options.hide_cursor > 0 &&
+           (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) &&
            time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
                lw->flags |= LW_HIDE_CURSOR;
        } else {