commit cac2ac13bd99c53ca562157a99befd92ad643a1d
parent 89d58af7a14ee86a7d44d01c069d60fc7307f84f
Author: lostd <lostd@2f30.org>
Date: Sun, 20 Nov 2016 13:18:21 +0000
Fix null pointer dereference in status
Also make checking if the connection is alive more robust
and consistent.
Diffstat:
M | mpd.c | | | 27 | +++++++++++++++++---------- |
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/mpd.c b/mpd.c
@@ -25,27 +25,36 @@ mpdread(void *arg, char *buf, size_t len)
struct mpdarg *mpdarg = arg;
static int frame = 0;
+#define CHECK_CONNECTION(conn) \
+ if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { \
+ warnx("mpd_connection_get_error: %s", \
+ mpd_connection_get_error_message(conn)); \
+ goto out; \
+ }
+
if (conn == NULL) {
conn = mpd_connection_new(mpdarg->host, mpdarg->port, 0);
if (conn == NULL)
return -1;
- if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
- goto out;
+ CHECK_CONNECTION(conn);
}
mpd_send_status(conn);
status = mpd_recv_status(conn);
+ if (status == NULL) {
+ CHECK_CONNECTION(conn);
+ mpd_response_finish(conn);
+ return -1;
+ }
state = mpd_status_get_state(status);
mpd_status_free(status);
- if (!mpd_response_finish(conn))
- goto out;
+ mpd_response_finish(conn);
if (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE)
return -1;
mpd_send_current_song(conn);
song = mpd_recv_song(conn);
if (song == NULL) {
- if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
- goto out;
- /* if no song is playing, reuse connection next time */
+ CHECK_CONNECTION(conn);
+ mpd_response_finish(conn);
return -1;
}
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
@@ -58,11 +67,9 @@ mpdread(void *arg, char *buf, size_t len)
strlcpy(buf, anim[frame++ % LEN(anim)], len);
}
mpd_song_free(song);
- if (!mpd_response_finish(conn))
- goto out;
+ mpd_response_finish(conn);
return 0;
out:
- warnx("failed to talk to mpd");
mpd_connection_free(conn);
conn = NULL;
return -1;