[easytag] Fix several CDDB response memory leaks



commit ac3c5a6f7e56b287ab2c2ec8c801b4cd0b88b06b
Author: David King <amigadave amigadave com>
Date:   Tue Nov 4 16:42:34 2014 +0000

    Fix several CDDB response memory leaks

 src/cddb_dialog.c |  100 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 74 insertions(+), 26 deletions(-)
---
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index 642b1f1..c459d28 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -831,7 +831,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
     CddbAlbum *cddbalbum = NULL;
     GList     *TrackOffsetList = NULL;
     gchar     *cddb_in, *cddb_out = NULL;
-    gchar     *cddb_end_str, *msg, *copy, *valid;
+    gchar *msg, *copy, *valid;
+    const gchar CDDB_END_STR[] = ".";
     gchar     *proxy_auth;
     gchar     *cddb_server_name;
     gint       cddb_server_port;
@@ -996,25 +997,50 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                    }
                }else
                {
-                       // For freedb
-                       if ( Cddb_Read_Http_Header(&file,&cddb_out) <= 0
-                     || Cddb_Read_Cddb_Header(&file,&cddb_out) <= 0 )
-                   {
-                       gchar *msg = g_strdup_printf (_("The server returned a bad response ‘%s’"),
+            /* For freedb. */
+            if (Cddb_Read_Http_Header (&file, &cddb_out) <= 0)
+            {
+                gchar *msg = g_strdup_printf (_("The server returned a bad response ‘%s’"),
                                                       cddb_out);
-                       gtk_statusbar_push(GTK_STATUSBAR(priv->status_bar),priv->status_bar_context,msg);
-                       Log_Print(LOG_ERROR,"%s",msg);
-                       g_free(msg);
-                       g_free(cddb_out);
-                       if (file)
-                           fclose(file);
-                       return FALSE;
+                gtk_statusbar_push (GTK_STATUSBAR (priv->status_bar),
+                                    priv->status_bar_context, msg);
+                Log_Print (LOG_ERROR, "%s", msg);
+
+                g_free (msg);
+                g_free (cddb_out);
+
+                if (file)
+                {
+                    fclose (file);
+                }
+
+                return FALSE;
+            }
+
+            g_free (cddb_out);
+
+            if (Cddb_Read_Cddb_Header (&file, &cddb_out) <= 0)
+            {
+                gchar *msg = g_strdup_printf (_("The server returned a bad response ‘%s’"),
+                                                      cddb_out);
+                gtk_statusbar_push (GTK_STATUSBAR (priv->status_bar),
+                                    priv->status_bar_context, msg);
+                Log_Print (LOG_ERROR, "%s", msg);
+
+                g_free (msg);
+                g_free (cddb_out);
+
+                if (file)
+                {
+                    fclose (file);
+                }
+
+                return FALSE;
                    }
                }
         g_free(cddb_out);
 
     }
-    cddb_end_str = g_strdup(".");
 
     while (!priv->stop_searching && Cddb_Read_Line (&file, &cddb_out) > 0)
     {
@@ -1029,12 +1055,16 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
          * end of data is in the last block read. In this case, the last line
          * will be a single '.'
          */
-        if (strlen(cddb_out)<=3 && strstr(cddb_out,cddb_end_str)!=NULL)
+        if (strlen (cddb_out) <= 3 && strstr (cddb_out, CDDB_END_STR) != NULL)
+        {
+            g_free (cddb_out);
             break;
+        }
 
         if ( strstr(cddb_out,"Track frame offsets")!=NULL ) // We read the Track frame offset
         {
             read_track_offset = TRUE; // The next reads are for the tracks offset
+            g_free (cddb_out);
             continue;
 
         }else if (read_track_offset) // We are reading a track offset? (generates TrackOffsetList)
@@ -1048,6 +1078,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
             {
                 read_track_offset = FALSE; // No more track offset
             }
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strstr(cddb_out,"Disc length: ")!=NULL ) // Length of album (in second)
@@ -1059,6 +1091,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                 cddbtrackframeoffset->offset = cddbalbum->duration * 75; // It's the last offset
                 TrackOffsetList = g_list_append(TrackOffsetList,cddbtrackframeoffset);
             }
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strncmp(cddb_out,"DTITLE=",7)==0 ) // "Artist / Album" names
@@ -1091,6 +1125,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                 cddbalbum->album = g_strconcat(cddbalbum->album,valid,NULL);
                 g_free(copy);
             }
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strncmp(cddb_out,"DYEAR=",6)==0 ) // Year
@@ -1098,6 +1134,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
             valid = Try_To_Validate_Utf8_String(cddb_out+6); // '6' to skip 'DYEAR='
             if (g_utf8_strlen(valid, -1))
                 cddbalbum->year = valid;
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strncmp(cddb_out,"DGENRE=",7)==0 ) // Genre
@@ -1105,6 +1143,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
             valid = Try_To_Validate_Utf8_String(cddb_out+7); // '7' to skip 'DGENRE='
             if (g_utf8_strlen(valid, -1))
                 cddbalbum->genre = valid;
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strncmp(cddb_out,"TTITLE",6)==0 ) // Track title (for exemple : TTITLE10=xxxx)
@@ -1120,6 +1160,7 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                 cddbtrackalbum->track_name = Try_To_Validate_Utf8_String(copy+1);
             }else
             {
+                g_free (cddb_out);
                 continue;
             }
 
@@ -1153,6 +1194,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                 }
                 cddbalbum->track_list = g_list_append(cddbalbum->track_list,cddbtrackalbum);
             }
+
+            g_free (cddb_out);
             continue;
 
         }else if ( strncmp(cddb_out,"EXTD=",5)==0 ) // Extended album data
@@ -1167,12 +1210,13 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
                 cddbalbum->year = g_strdup_printf("%d",atoi(year_ptr+5));
             if (genre_ptr && cddbalbum->genre)
                 cddbalbum->genre = g_strdup(Id3tag_Genre_To_String(atoi(genre_ptr+5)));
+
+            g_free (cddb_out);
             continue;
         }
 
         g_free(cddb_out);
     }
-    g_free(cddb_end_str);
 
     // Close file opened for reading lines
     if (file)
@@ -3140,11 +3184,17 @@ Cddb_Read_Cddb_Header (FILE **file, gchar **cddb_out)
     if ( Cddb_Read_Line(file,cddb_out) < 0 )
         return -1; // Error!
 
-    // Some requests receive some strange data (arbitrary : less than 10 chars.)
-    // at the beginning (2 or 3 characters)... So we read one line more...
-    if ( !*cddb_out || strlen(*cddb_out) < 10 )
-        if ( Cddb_Read_Line(file,cddb_out) < 0 )
-            return -1; // Error!
+    /* Some requests receive some strange data (arbitrary: less than 10 chars.)
+     * at the beginning (2 or 3 characters)... So we read one line more... */
+    if (!*cddb_out || strlen (*cddb_out) < 10)
+    {
+        g_free (*cddb_out);
+
+        if (Cddb_Read_Line (file, cddb_out) < 0)
+        {
+            return -1; /* Error! */
+        }
+    }
 
     //g_print("Cddb Header : %s\n",*cddb_out);
 
@@ -3215,7 +3265,7 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
     gint   server_try = 0;
     GString *query_string;
     gchar *cddb_discid;
-    gchar *cddb_end_str;
+    const gchar CDDB_END_STR[] = ".";
 
     guint total_frames = 150;   /* First offset is (almost) always 150 */
     guint disc_length  = 2;     /* and 2s elapsed before first track */
@@ -3503,8 +3553,6 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
             }
             g_free(cddb_out);
 
-            cddb_end_str = g_strdup(".");
-
             /*
              * Format :
              * For Freedb, Gnudb, the lines to read are like :
@@ -3528,7 +3576,8 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
                  * end of data is in the last block read. In this case, the last line
                  * will be a single '.'
                  */
-                if ( cddb_out_tmp && strlen(cddb_out_tmp)<=3 && strstr(cddb_out_tmp,cddb_end_str)!=NULL )
+                if (cddb_out_tmp && strlen (cddb_out_tmp) <= 3
+                    && strstr (cddb_out_tmp, CDDB_END_STR) != NULL)
                 {
                     g_free (cddb_out);
                     break;
@@ -3582,7 +3631,6 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
 
                 g_free(cddb_out);
             }
-            g_free(cddb_end_str);
             g_free(cddb_server_name);
             g_free(cddb_server_cgi_path);
             g_free (proxy_hostname);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]