Commit b90b773c authored by Stephen Hemminger's avatar Stephen Hemminger

lnstat: fix error handling

Error handling was silent and had leaks.
Signed-off-by: default avatarStephen Hemminger <stephen@networkplumber.org>
parent e49b51d6
...@@ -172,8 +172,10 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file) ...@@ -172,8 +172,10 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
/* allocate */ /* allocate */
lf = malloc(sizeof(*lf)); lf = malloc(sizeof(*lf));
if (!lf) if (!lf) {
fprintf(stderr, "out of memory\n");
return NULL; return NULL;
}
/* initialize */ /* initialize */
memset(lf, 0, sizeof(*lf)); memset(lf, 0, sizeof(*lf));
...@@ -190,6 +192,7 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file) ...@@ -190,6 +192,7 @@ static struct lnstat_file *alloc_and_open(const char *path, const char *file)
/* open */ /* open */
lf->fp = fopen(lf->path, "r"); lf->fp = fopen(lf->path, "r");
if (!lf->fp) { if (!lf->fp) {
perror(lf->path);
free(lf); free(lf);
return NULL; return NULL;
} }
...@@ -256,12 +259,16 @@ struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files, ...@@ -256,12 +259,16 @@ struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
continue; continue;
lf = alloc_and_open(path, de->d_name); lf = alloc_and_open(path, de->d_name);
if (!lf) if (!lf) {
closedir(dir);
return NULL; return NULL;
}
/* fill in field structure */ /* fill in field structure */
if (lnstat_scan_fields(lf) < 0) if (lnstat_scan_fields(lf) < 0) {
closedir(dir);
return NULL; return NULL;
}
/* prepend to global list */ /* prepend to global list */
lf->next = lnstat_files; lf->next = lnstat_files;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment