! Please note that this is a snapshot of our old Bugzilla server, which is read only since May 29, 2020. Please go to gitlab.xfce.org for our new server !
[patch] remove unnecessary strdup
Status:
RESOLVED: FIXED
Product:
Xfce4-battery-plugin
Component:
General

Comments

Description Ivan Middleton 2019-03-14 20:57:29 CET
Here's a quick patch for an unnecessary string duplication I found in battery.c:

--- panel-plugin/battery.c                                         
+++ panel-plugin/battery.c                                         
@@ -296,8 +296,7 @@
         }

         if (acline && new_state != BM_MISSING) {
-            gchar *tmp = g_strdup(icon_name);
-            g_free(icon_name);
+            gchar *tmp = icon_name;
             new_state++;
             icon_name = g_strconcat(tmp, "-charging", NULL);
             g_free(tmp);

I found this because I noticed a memory leak in this code, which has already been fixed in the following commit:

https://git.xfce.org/panel-plugins/xfce4-battery-plugin/commit/panel-plugin/battery.c?id=b32c015ed36c0f6e9e053351482303a15615847a

It's not necessary to duplicate the original string. One only needs a copy of the pointer to it, so that it can be freed after its pointer is adjusted.
Comment 1 Git Bot editbugs 2019-03-16 03:50:15 CET
Andre Miranda referenced this bugreport in commit 695d02fd13a5394b81d02262dd1edb887034a0d6

Avoid unnecessary strdup (Bug #15196)

https://git.xfce.org/panel-plugins/xfce4-battery-plugin/commit?id=695d02fd13a5394b81d02262dd1edb887034a0d6
Comment 2 Andre Miranda editbugs 2019-03-16 03:53:04 CET
I just introduced the missing g_free but didn't notice the strdup was unnecessary, good catch!
Thank you.

Bug #15196

Reported by:
Ivan Middleton
Reported on: 2019-03-14
Last modified on: 2019-03-16

People

Assignee:
Xfce-Goodies Maintainers
CC List:
2 users

Version

Attachments

Additional information