[Goodies-commits] r6444 - xfce4-mpc-plugin/trunk/panel-plugin

Landry Breuil landry at xfce.org
Mon Jan 12 21:56:35 CET 2009


Author: landry
Date: 2009-01-12 20:56:35 +0000 (Mon, 12 Jan 2009)
New Revision: 6444

Modified:
   xfce4-mpc-plugin/trunk/panel-plugin/simple-libmpd.c
   xfce4-mpc-plugin/trunk/panel-plugin/xfce4-mpc-plugin.c
Log:
Replace some sprintf/strdup by their safe equivalents (snprinf/strndup), i should have
done that before. It fixes an issue with looooong passwords handling, among others.
Diff from Simon Huggins/Debian package, Thanks!


Modified: xfce4-mpc-plugin/trunk/panel-plugin/simple-libmpd.c
===================================================================
--- xfce4-mpc-plugin/trunk/panel-plugin/simple-libmpd.c	2009-01-12 19:19:26 UTC (rev 6443)
+++ xfce4-mpc-plugin/trunk/panel-plugin/simple-libmpd.c	2009-01-12 20:56:35 UTC (rev 6444)
@@ -37,17 +37,15 @@
 #include <errno.h>
 #include <fcntl.h>
 
-#define STRLENGTH 32
-
 MpdObj* mpd_new(char* host, int port, char* pass)
 {
    MpdObj* mo = g_new0(MpdObj,1);
 
    DBG("host=%s, port=%d, pass=%s", host, port, pass);
 
-   mo->host = g_strndup(host,STRLENGTH);
+   mo->host = g_strdup(host);
    mo->port = port;
-   mo->pass = g_strndup(pass,STRLENGTH);
+   mo->pass = g_strdup(pass);
    mo->socket = 0;
    mo->status = 0;
    mo->repeat = 0;
@@ -508,7 +506,7 @@
    char outbuf[15];
    /* write setvol 'newvol' to socket */
    DBG("!");
-   sprintf(outbuf,"setvol %d\n",newvol);
+   snprintf(outbuf, sizeof(outbuf), "setvol %d\n",newvol);
    mpd_send_single_cmd(mo,outbuf);
 }
 
@@ -528,7 +526,7 @@
 {
    char outbuf[15];
    DBG("!");
-   sprintf(outbuf,"random %d\n",random);
+   snprintf(outbuf, sizeof(outbuf), "random %d\n",random);
    return mpd_send_single_cmd(mo,outbuf);
 
 }
@@ -537,7 +535,7 @@
 {
    char outbuf[15];
    DBG("!");
-   sprintf(outbuf,"repeat %d\n",repeat);
+   snprintf(outbuf, sizeof(outbuf), "repeat %d\n",repeat);
    return mpd_send_single_cmd(mo,outbuf);
 }
 
@@ -584,7 +582,7 @@
 {
    char outbuf[15];
    DBG("!");
-   sprintf(outbuf,"playid %d\n",id);
+   snprintf(outbuf, sizeof(outbuf), "playid %d\n",id);
    return mpd_send_single_cmd(mo,outbuf);
 }
 
@@ -597,9 +595,16 @@
 void mpd_send_password(MpdObj* mo)
 {
    DBG("!");
-   char outbuf[30];
+   char outbuf[256];
    /* write password 'password' to socket */
-   sprintf(outbuf,"password %s\n",mo->pass);
+   int wrote = snprintf(outbuf, sizeof(outbuf), "password %s\n",mo->pass);
+   if (wrote > 255) {
+	/* the password is too long to fit though there doesn't seem to be a
+	 * nice way to report this error :-/ */
+	fprintf(stderr, "xfce4-mpc-plugin: password too long!\n");
+	mo->error = MPD_ERROR_SYSTEM;
+	return;
+   }
    mpd_send_single_cmd(mo,outbuf);
 }
 
@@ -607,14 +612,14 @@
 {
    DBG("! new hostname=%s",host);
    g_free(mo->host);
-   mo->host = g_strndup(host,STRLENGTH);
+   mo->host = g_strdup(host);
 }
 
 void mpd_set_password(MpdObj* mo, char* pass)
 {
    DBG("! new password=%s",pass);
    g_free(mo->pass);
-   mo->pass = g_strndup(pass,STRLENGTH);
+   mo->pass = g_strdup(pass);
 }
 
 void mpd_set_port(MpdObj* mo,int port)

Modified: xfce4-mpc-plugin/trunk/panel-plugin/xfce4-mpc-plugin.c
===================================================================
--- xfce4-mpc-plugin/trunk/panel-plugin/xfce4-mpc-plugin.c	2009-01-12 19:19:26 UTC (rev 6443)
+++ xfce4-mpc-plugin/trunk/panel-plugin/xfce4-mpc-plugin.c	2009-01-12 20:56:35 UTC (rev 6444)
@@ -29,7 +29,6 @@
 #define DEFAULT_MPD_HOST "localhost"
 #define DEFAULT_MPD_PORT 6600
 #define DIALOG_ENTRY_WIDTH 15
-#define STRLENGTH 32
 
 #include "xfce4-mpc-plugin.h"
 
@@ -107,7 +106,7 @@
    mpc->show_frame = xfce_rc_read_bool_entry (rc, "show_frame", TRUE);
    mpc->client_appl = g_strdup(xfce_rc_read_entry (rc, "client_appl",  ""));
    label = gtk_bin_get_child(GTK_BIN(mpc->appl));
-   g_sprintf(str, "%s %s", _("Launch"), mpc->client_appl);
+   g_snprintf(str, sizeof(str), "%s %s", _("Launch"), mpc->client_appl);
    gtk_label_set_text(GTK_LABEL(label),str);
    DBG ("Settings : %s@%s:%d\nframe:%d\nappl:%s", mpc->mpd_password, mpc->mpd_host, mpc->mpd_port, mpc->show_frame, mpc->client_appl);
    xfce_rc_close (rc);
@@ -165,12 +164,12 @@
    char str[30];
 
    t_mpc *mpc = dialog->mpc;
-   mpc->mpd_host = g_strndup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_host)),STRLENGTH);
+   mpc->mpd_host = g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_host)));
    mpc->mpd_port = atoi(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_port)));
-   mpc->mpd_password = g_strndup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_password)),STRLENGTH);
-   mpc->client_appl = g_strndup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_client_appl)),STRLENGTH);
+   mpc->mpd_password = g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_password)));
+   mpc->client_appl = g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->textbox_client_appl)));
    label = gtk_bin_get_child(GTK_BIN(mpc->appl));
-   g_sprintf(str, "%s %s", _("Launch"), mpc->client_appl);
+   g_snprintf(str, sizeof(str), "%s %s", _("Launch"), mpc->client_appl);
    gtk_label_set_text(GTK_LABEL(label),str);
 
    DBG ("Apply: host=%s, port=%d, passwd=%s, appl=%s", mpc->mpd_host, mpc->mpd_port, mpc->mpd_password, mpc->client_appl);
@@ -311,13 +310,13 @@
 {
    /* buf may contain stuff, care to append text */
    if (!song->artist || !song->title)
-      g_sprintf(str,"%s%s", str, song->file);
+      g_snprintf(str, sizeof(str), "%s%s", str, song->file);
    else if (!song->album)
-      g_sprintf(str,"%s%s - %s", str, song->artist, song->title);
+      g_snprintf(str, sizeof(str), "%s%s - %s", str, song->artist, song->title);
    else if (!song->track)
-      g_sprintf(str,"%s%s - %s -/- %s", str, song->artist, song->album, song->title);
+      g_snprintf(str, sizeof(str), "%s%s - %s -/- %s", str, song->artist, song->album, song->title);
    else
-      g_sprintf(str,"%s%s - %s -/- (#%s) %s", str, song->artist, song->album, song->track, song->title);
+      g_snprintf(str, sizeof(str), "%s%s - %s -/- (#%s) %s", str, song->artist, song->album, song->track, song->title);
 }
 
 static void
@@ -337,28 +336,28 @@
       }
    }
 
-   g_sprintf(str, "Volume : %d%%", mpd_status_get_volume(mpc->mo));
+   g_snprintf(str, sizeof(str), "Volume : %d%%", mpd_status_get_volume(mpc->mo));
 
    switch (mpd_player_get_state(mpc->mo))
    {
       case MPD_PLAYER_PLAY:
-         g_sprintf(str, "%s - Mpd Playing\n",str);
+         g_snprintf(str, sizeof(str), "%s - Mpd Playing\n",str);
          break;
       case MPD_PLAYER_PAUSE:
-         g_sprintf(str, "%s - Mpd Paused\n",str);
+         g_snprintf(str, sizeof(str), "%s - Mpd Paused\n",str);
          break;
       case MPD_PLAYER_STOP:
-         g_sprintf(str, "%s - Mpd Stopped\n",str);
+         g_snprintf(str, sizeof(str), "%s - Mpd Stopped\n",str);
          break;
       default:
-         g_sprintf(str, "%s - Mpd state ?\n",str);
+         g_snprintf(str, sizeof(str), "%s - Mpd state ?\n",str);
          break;
    }
    song = mpd_playlist_get_current_song(mpc->mo);
    if (song && song->id != -1)
       format_song_display(song, str);
    else
-      g_sprintf(str,"%sFailed to get song info ?", str);
+      g_snprintf(str, sizeof(str), "%sFailed to get song info ?", str);
 
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mpc->random), mpd_player_get_random(mpc->mo));
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mpc->repeat), mpd_player_get_repeat(mpc->mo));




More information about the Goodies-commits mailing list