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.

...
<snip>
...
==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
This commit is contained in:
Kate Hsuan 2025-03-28 15:23:20 +08:00
parent df51aef98a
commit 2ebaa01347

View file

@ -194,6 +194,10 @@ up_device_supply_battery_refresh (UpDevice *device,
GUdevDevice *native; GUdevDevice *native;
UpBatteryInfo info = { 0 }; UpBatteryInfo info = { 0 };
UpBatteryValues values = { 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)); native = G_UDEV_DEVICE (up_device_get_native (device));
@ -211,9 +215,13 @@ up_device_supply_battery_refresh (UpDevice *device,
return TRUE; return TRUE;
} }
info.vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer")); vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer"));
info.model = up_make_safe_string (get_sysfs_attr_uncached (native, "model_name")); 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")); 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.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"); 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.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.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)) { if (up_device_supply_battery_get_charge_control_limits (native, &info)) {
info.charge_control_supported = TRUE; info.charge_control_supported = TRUE;