From 2ebaa01347b2a0624bb7f6e2e2d861cb5bc1d01e Mon Sep 17 00:00:00 2001 From: Kate Hsuan Date: Fri, 28 Mar 2025 15:23:20 +0800 Subject: [PATCH] linux: up-device-supply-battery: Fix mem leak when refreshing battery info The AC was plugged and unplugged repeatedly, and hit this leak in up_device_supply_battery_refresh(). Four variables in UpBatteryInfo need to be free. ... ... ==2825085== 1,152 bytes in 9 blocks are definitely lost in loss record 1,851 of 1,865 ==2825085== at 0x484CE40: realloc (vg_replace_malloc.c:1801) ==2825085== by 0x4916ADA: g_realloc (gmem.c:171) ==2825085== by 0x4939E06: g_string_expand (gstring.c:82) ==2825085== by 0x4939EA1: g_string_sized_new (gstring.c:113) ==2825085== by 0x11262C: up_device_get_id (up-device.c:347) ==2825085== by 0x113136: up_device_notify (up-device.c:241) ==2825085== by 0x4A1B811: g_closure_invoke (gclosure.c:833) ==2825085== by 0x4A4C0C1: signal_emit_unlocked_R.isra.0 (gsignal.c:3735) ==2825085== by 0x4A3CCE8: signal_emit_valist_unlocked (gsignal.c:3534) ==2825085== by 0x4A3CF71: g_signal_emit_valist (gsignal.c:3277) ==2825085== by 0x4A3D033: g_signal_emit (gsignal.c:3597) ==2825085== by 0x4A27C75: g_object_dispatch_properties_changed.lto_priv.0 (gobject.c:1827) ==2825085== ==2825085== LEAK SUMMARY: ==2825085== definitely lost: 3,663 bytes in 333 blocks ==2825085== indirectly lost: 0 bytes in 0 blocks ==2825085== possibly lost: 960 bytes in 3 blocks ==2825085== still reachable: 172,351 bytes in 2,205 blocks ==2825085== suppressed: 0 bytes in 0 blocks ==2825085== Reachable blocks (those to which a pointer was found) are not shown. ==2825085== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==2825085== ==2825085== For lists of detected and suppressed errors, rerun with: -s ==2825085== ERROR SUMMARY: 20 errors from 20 contexts (suppressed: 0 from 0) Resolves: #305 --- src/linux/up-device-supply-battery.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/linux/up-device-supply-battery.c b/src/linux/up-device-supply-battery.c index e48f622..04b9f6b 100644 --- a/src/linux/up-device-supply-battery.c +++ b/src/linux/up-device-supply-battery.c @@ -194,6 +194,10 @@ up_device_supply_battery_refresh (UpDevice *device, GUdevDevice *native; UpBatteryInfo info = { 0 }; UpBatteryValues values = { 0 }; + g_autofree gchar *vendor = NULL; + g_autofree gchar *model = NULL; + g_autofree gchar *serial = NULL; + g_autofree gchar *technology = NULL; native = G_UDEV_DEVICE (up_device_get_native (device)); @@ -211,9 +215,13 @@ up_device_supply_battery_refresh (UpDevice *device, return TRUE; } - info.vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer")); - info.model = up_make_safe_string (get_sysfs_attr_uncached (native, "model_name")); - info.serial = up_make_safe_string (get_sysfs_attr_uncached (native, "serial_number")); + vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer")); + model = up_make_safe_string (get_sysfs_attr_uncached (native, "model_name")); + serial = up_make_safe_string (get_sysfs_attr_uncached (native, "serial_number")); + + info.vendor = vendor; + info.model = model; + info.serial = serial; info.voltage_design = up_device_supply_battery_get_design_voltage (self, native); info.charge_cycles = g_udev_device_get_sysfs_attr_as_int_uncached (native, "cycle_count"); @@ -228,7 +236,8 @@ up_device_supply_battery_refresh (UpDevice *device, info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full") / 1000000.0; info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full_design") / 1000000.0; } - info.technology = up_convert_device_technology (get_sysfs_attr_uncached (native, "technology")); + technology = get_sysfs_attr_uncached (native, "technology"); + info.technology = up_convert_device_technology (technology); if (up_device_supply_battery_get_charge_control_limits (native, &info)) { info.charge_control_supported = TRUE;