Skip to content

Commit 06d68b0

Browse files
committed
Add comprehensive cleanup for lproc and lfile structures in lsof_destroy
- Free all lproc array elements including: - cmd (command name) - cntx (SELinux context) - tcmd (task command name) - zn (zone name) - Free all lfile structures in each process's file list including: - nm (name) - nma (NAME column addition) - dev_ch (device character) - fsdir (file system directory) - fsdev (file system device) - Properly iterate through linked lists before freeing This fixes major memory leaks when processing processes and their files, improving test pass rate from 4 to 30 out of 40 tests.
1 parent 4cdff50 commit 06d68b0

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/lsof.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,35 @@ void lsof_destroy(struct lsof_context *ctx) {
791791
HbyNmCt = 0;
792792
Hs = 0;
793793

794-
/* Free process array */
795-
CLEAN(Lproc);
794+
/* Free process array and all associated resources */
795+
if (Lproc) {
796+
for (i = 0; i < Nlproc; i++) {
797+
struct lproc *lp = &Lproc[i];
798+
struct lfile *lf, *lf_next;
799+
/* Free command name */
800+
CLEAN(lp->cmd);
801+
#if defined(HASSELINUX)
802+
CLEAN(lp->cntx);
803+
#endif
804+
#if defined(HASTASKS)
805+
CLEAN(lp->tcmd);
806+
#endif
807+
#if defined(HASZONES)
808+
CLEAN(lp->zn);
809+
#endif
810+
/* Free file list */
811+
for (lf = lp->file; lf; lf = lf_next) {
812+
lf_next = lf->next;
813+
CLEAN(lf->nm);
814+
CLEAN(lf->nma);
815+
CLEAN(lf->dev_ch);
816+
CLEAN(lf->fsdir);
817+
CLEAN(lf->fsdev);
818+
CLEAN(lf);
819+
}
820+
}
821+
CLEAN(Lproc);
822+
}
796823
Nlproc = 0;
797824
ctx->procs_cap = 0;
798825

0 commit comments

Comments
 (0)