Skip to content

Commit 4cdff50

Browse files
committed
Fix memory leaks and add resource cleanup in lsof_destroy
- Added sfile list cleanup in lsof_destroy() (aname, name, devnm fields) - Added hashSfile hash buckets cleanup (HbyFdi, HbyFrd, HbyFsd, HbyNm) - Added Lproc process array cleanup - Added path allocation tracking in ck_file_arg() to free on error - Added lsof_destroy() call before Exit() in main.c to ensure cleanup - Reset counters after cleanup (HbyFdiCt, HbyFrdCt, HbyFsdCt, HbyNmCt, Hs, Nlproc, procs_cap) These fixes address major memory leaks detected by AddressSanitizer when running with file arguments that don't exist or when processing processes.
1 parent 5c3045e commit 4cdff50

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

lib/lsof.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,35 @@ void lsof_destroy(struct lsof_context *ctx) {
767767
CLEAN(UdpSt);
768768
CLEAN(Pn);
769769

770+
/* Free sfile list */
771+
{
772+
struct sfile *sfp, *sfp_next;
773+
for (sfp = Sfile; sfp; sfp = sfp_next) {
774+
sfp_next = sfp->next;
775+
CLEAN(sfp->aname);
776+
CLEAN(sfp->name);
777+
CLEAN(sfp->devnm);
778+
CLEAN(sfp);
779+
}
780+
Sfile = NULL;
781+
}
782+
783+
/* Free hashSfile hash buckets */
784+
CLEAN(HbyFdi);
785+
HbyFdiCt = 0;
786+
CLEAN(HbyFrd);
787+
HbyFrdCt = 0;
788+
CLEAN(HbyFsd);
789+
HbyFsdCt = 0;
790+
CLEAN(HbyNm);
791+
HbyNmCt = 0;
792+
Hs = 0;
793+
794+
/* Free process array */
795+
CLEAN(Lproc);
796+
Nlproc = 0;
797+
ctx->procs_cap = 0;
798+
770799
CLEAN(ctx);
771800
}
772801

lib/misc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ void Exit(struct lsof_context *ctx, enum ExitStatus xv) /* exit() value */
768768
DCpath[DCpathX]);
769769
#endif /* defined(HASDCACHE) */
770770

771+
lsof_destroy(ctx);
771772
exit(xv);
772773
}
773774

src/arg.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ int ck_file_arg(struct lsof_context *ctx, int i, /* first file argument index */
121121
char *ap, *fnm, *fsnm, *path;
122122
short err = 0;
123123
int fsm, ftype, j, k;
124+
int path_allocated = 0;
124125
MALLOC_S l;
125126
struct mounts *mp;
126127
static struct mounts **mmp = (struct mounts **)NULL;
@@ -145,13 +146,15 @@ int ck_file_arg(struct lsof_context *ctx, int i, /* first file argument index */
145146
* Loop through arguments.
146147
*/
147148
for (; i < ac; i++) {
148-
if (rs && (ac == 1) && (i == 0))
149+
if (rs && (ac == 1) && (i == 0)) {
149150
path = av[i];
150-
else {
151+
path_allocated = 0;
152+
} else {
151153
if (!(path = Readlink(ctx, av[i]))) {
152154
ErrStat = 1;
153155
continue;
154156
}
157+
path_allocated = 1;
155158
}
156159
/*
157160
* Remove terminating `/' characters from paths longer than one.
@@ -172,6 +175,7 @@ int ck_file_arg(struct lsof_context *ctx, int i, /* first file argument index */
172175
(void)strncpy(ap, path, k);
173176
ap[k] = '\0';
174177
path = ap;
178+
path_allocated = 1;
175179
}
176180
}
177181
/*
@@ -267,6 +271,8 @@ int ck_file_arg(struct lsof_context *ctx, int i, /* first file argument index */
267271
(void)fprintf(stderr, ": %s\n", strerror(en));
268272
}
269273
Sfile = sfp->next;
274+
if (path_allocated)
275+
(void)free((FREE_P *)path);
270276
(void)free((FREE_P *)sfp);
271277
ErrStat = 1;
272278
continue;
@@ -561,7 +567,6 @@ int ctrl_dcache(struct lsof_context *ctx, /* context */
561567
}
562568
#endif /* defined(HASDCACHE) */
563569

564-
565570
#if defined(HASEOPT)
566571
/*
567572
* enter_efsys() -- enter path of file system whose kernel blocks are to be
@@ -2156,9 +2161,9 @@ lkup_hostnm(char *hn, /* host name */
21562161

21572162
if (!he || !he->h_addr)
21582163
return (he);
2159-
/*
2160-
* Copy first hostname structure address to destination structure.
2161-
*/
2164+
/*
2165+
* Copy first hostname structure address to destination structure.
2166+
*/
21622167

21632168
#if defined(HASIPv6)
21642169
if (n->af != he->h_addrtype)

src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,8 +1353,9 @@ int main(int argc, char *argv[]) {
13531353
else
13541354
slp = (struct lproc **)realloc((MALLOC_P *)slp, len);
13551355
if (!slp) {
1356-
(void)fprintf(stderr, "%s: no space for %zu sort pointers\n",
1357-
Pn, Nlproc);
1356+
(void)fprintf(stderr,
1357+
"%s: no space for %zu sort pointers\n", Pn,
1358+
Nlproc);
13581359
Error(ctx);
13591360
}
13601361
}

0 commit comments

Comments
 (0)