Commit 18e96ed
committed
cli: fix memory leak in volume status detail (#4292)
cli_volume_status_t has seven pointer fields, but they have two
different ownership models:
Owned (heap-allocated, caller must free):
- brick — GF_MALLOC before the loop
- pid_str — gf_asprintf each iteration
- free, total — gf_uint64_2human_readable inside cli_get_detail_status
Borrowed (point into dict internals, released by dict_unref at out:):
- fs_name, mount_options, device, inode_size — dict_get_str
The status variable is declared once outside the loop with = {0}.
Each iteration overwrites the owned pointers with new allocations.
Before this fix, only pid_str was freed inside the loop body and
only brick was freed at the out: label. The other two owned
pointers (free, total) were simply overwritten each iteration — the
previous values were lost, leaking two strings per brick.
The fix adds three things:
1. GF_FREE for free and total inside the loop, matching what was
already done for pid_str.
2. GF_FREE for pid_str, free, and total at the out: label, covering
the two goto-out error paths mid-iteration (gf_asprintf failure
at line 7592 and cli_get_detail_status failure at line 7597)
where the current iteration's allocations have not been freed yet.
3. Explicit NULLing after each in-loop GF_FREE. On the normal path
the loop finishes, execution falls through cont: to out:, which
calls GF_FREE on the same pointers again. Without NULLing, that
would be a double-free. With it, GF_FREE(NULL) is a no-op.
Signed-off-by: Thales Antunes de Oliveira Barretto <thales.barretto.git@gmail.com>1 parent ae1d696 commit 18e96ed
1 file changed
Lines changed: 8 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7601 | 7601 | | |
7602 | 7602 | | |
7603 | 7603 | | |
7604 | | - | |
7605 | 7604 | | |
| 7605 | + | |
| 7606 | + | |
| 7607 | + | |
| 7608 | + | |
| 7609 | + | |
7606 | 7610 | | |
7607 | 7611 | | |
7608 | 7612 | | |
| |||
7615 | 7619 | | |
7616 | 7620 | | |
7617 | 7621 | | |
| 7622 | + | |
| 7623 | + | |
| 7624 | + | |
7618 | 7625 | | |
7619 | 7626 | | |
7620 | 7627 | | |
| |||
0 commit comments