[easytag/wip/application-window: 23/23] Use g_format_size() instead of custom code



commit 0e9fb783d6449db7a2f70887935557e9bc77e0b2
Author: David King <amigadave amigadave com>
Date:   Mon Feb 3 21:37:20 2014 +0000

    Use g_format_size() instead of custom code
    
    Replace Convert_Size() and Convert_Size_1() with g_format_size() to
    avoid potential invalid memory accesses.
    
    Found with Coverity (CID 1163338 and CID 1163339).

 src/cddb_dialog.c        |    2 +-
 src/et_core.c            |    4 +-
 src/flac_header.c        |    4 +-
 src/misc.c               |   48 ----------------------------------------------
 src/misc.h               |    2 -
 src/monkeyaudio_header.c |    4 +-
 src/mp4_header.c         |    4 +-
 src/mpeg_header.c        |    4 +-
 src/musepack_header.c    |    4 +-
 src/ogg_header.c         |    4 +-
 src/picture.c            |    2 +-
 src/wavpack_header.c     |    4 +-
 12 files changed, 18 insertions(+), 68 deletions(-)
---
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index 5080cae..dca3fdb 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -1063,7 +1063,7 @@ Cddb_Write_Result_To_File (EtCDDBDialog *self,
             //g_print("\nLine : %lu : %s\n",bytes_read,cddb_out);
 
             // Display message
-            size_str = Convert_Size_1(*bytes_read_total);
+            size_str = g_format_size (*bytes_read_total);
             msg = g_strdup_printf(_("Receiving data (%s)…"),size_str);
             gtk_statusbar_push (GTK_STATUSBAR (priv->status_bar),
                                 priv->status_bar_context, msg);
diff --git a/src/et_core.c b/src/et_core.c
index 9250ee3..22981b6 100644
--- a/src/et_core.c
+++ b/src/et_core.c
@@ -3142,8 +3142,8 @@ ET_Display_File_Info_To_UI(ET_File_Info *ETFileInfo)
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/flac_header.c b/src/flac_header.c
index 3293b42..535f7b0 100644
--- a/src/flac_header.c
+++ b/src/flac_header.c
@@ -218,8 +218,8 @@ gboolean Flac_Header_Display_File_Info_To_UI (gchar *filename_utf8, ET_File_Info
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/misc.c b/src/misc.c
index 6566a23..82f0f8b 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -896,54 +896,6 @@ gchar *Check_If_Executable_Exists (const gchar *program)
 
 }
 
-
-
-/*
- * The returned string must be freed after use
- */
-gchar *
-Convert_Size (gsize size)
-{
-    /* Translators: file size (bytes, kilobytes, megabytes, gigabytes,
-     * terabytes). */
-    const gchar *units_tab[] = { N_("B"), N_("kB"), N_("MB"), N_("GB"), N_("TB")};
-    gsize i;
-
-    for (i = 0; size / 1024 && i < G_N_ELEMENTS (units_tab); i++)
-    {
-        size /= 1024;
-    }
-
-    return g_strdup_printf ("%.1" G_GSIZE_FORMAT " %s", size, _(units_tab[i]));
-}
-
-/*
- * Same as before except that if value in MB, we display 3 numbers after the
- * comma. The returned string must be freed after use.
- */
-gchar *
-Convert_Size_1 (gsize size)
-{
-    /* Translators: file size (bytes, kilobytes, megabytes, gigabytes,
-     * terabytes). */
-    const gchar *units_tab[] = { N_("B"), N_("kB"), N_("MB"), N_("GB"), N_("TB")};
-    guint i;
-
-    for (i = 0; size / 1024 && i < G_N_ELEMENTS (units_tab); i++)
-    {
-        size /= 1024;
-    }
-
-    /* FIXME: Separator should be taken from the locale. */
-    /* For large values : display 3 number afer the separator. */
-    if (i >= 2)
-        return g_strdup_printf ("%.3" G_GSIZE_FORMAT " %s", size,
-                                _(units_tab[i]));
-    else
-        return g_strdup_printf ("%.1" G_GSIZE_FORMAT " %s", size,
-                                _(units_tab[i]));
-}
-
 /*
  * Convert a series of seconds into a readable duration
  * Remember to free the string that is returned
diff --git a/src/misc.h b/src/misc.h
index 4aa9b8a..56c2843 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -64,8 +64,6 @@ void Run_Audio_Player_Using_Selection (void);
 void Run_Audio_Player_Using_Browser_Artist_List (void);
 void Run_Audio_Player_Using_Browser_Album_List  (void);
 
-gchar *Convert_Size (gsize size);
-gchar *Convert_Size_1 (gsize size);
 gchar *Convert_Duration (gulong duration);
 
 goffset et_get_file_size (const gchar *filename);
diff --git a/src/monkeyaudio_header.c b/src/monkeyaudio_header.c
index 04bce1f..30f9122 100644
--- a/src/monkeyaudio_header.c
+++ b/src/monkeyaudio_header.c
@@ -101,8 +101,8 @@ gboolean Mac_Header_Display_File_Info_To_UI (gchar *filename_utf8, ET_File_Info
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/mp4_header.c b/src/mp4_header.c
index 6ff90d0..f9952a2 100644
--- a/src/mp4_header.c
+++ b/src/mp4_header.c
@@ -151,8 +151,8 @@ gboolean Mp4_Header_Display_File_Info_To_UI(gchar *filename, ET_File_Info *ETFil
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     if (size)  g_free(size);
diff --git a/src/mpeg_header.c b/src/mpeg_header.c
index 9cd34b9..ba46ff4 100644
--- a/src/mpeg_header.c
+++ b/src/mpeg_header.c
@@ -219,8 +219,8 @@ gboolean Mpeg_Header_Display_File_Info_To_UI(gchar *filename_utf8, ET_File_Info
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/musepack_header.c b/src/musepack_header.c
index 339e85e..84f6be3 100644
--- a/src/musepack_header.c
+++ b/src/musepack_header.c
@@ -104,8 +104,8 @@ gboolean Mpc_Header_Display_File_Info_To_UI (gchar *filename_utf8, ET_File_Info
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/ogg_header.c b/src/ogg_header.c
index 7fbd397..915cb3d 100644
--- a/src/ogg_header.c
+++ b/src/ogg_header.c
@@ -414,8 +414,8 @@ gboolean Ogg_Header_Display_File_Info_To_UI (gchar *filename, ET_File_Info *ETFi
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);
diff --git a/src/picture.c b/src/picture.c
index 557fadb..646ed0e 100644
--- a/src/picture.c
+++ b/src/picture.c
@@ -901,7 +901,7 @@ Picture_Info (Picture *pic)
         desc = "";
 
     type = Picture_Type_String(pic->type);
-    size_str = Convert_Size_1((gfloat)pic->size);
+    size_str = g_format_size (pic->size);
 
     s = g_string_new(0);
     // Behaviour following the tag type...
diff --git a/src/wavpack_header.c b/src/wavpack_header.c
index 61da89e..3a0fb3a 100644
--- a/src/wavpack_header.c
+++ b/src/wavpack_header.c
@@ -91,8 +91,8 @@ gboolean Wavpack_Header_Display_File_Info_To_UI(gchar *filename_utf8, ET_File_In
     g_free(text);
 
     /* Size */
-    size  = Convert_Size(ETFileInfo->size);
-    size1 = Convert_Size(ETCore->ETFileDisplayedList_TotalSize);
+    size  = g_format_size (ETFileInfo->size);
+    size1 = g_format_size (ETCore->ETFileDisplayedList_TotalSize);
     text  = g_strdup_printf("%s (%s)",size,size1);
     gtk_label_set_text(GTK_LABEL(SizeValueLabel),text);
     g_free(size);


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