Skip to content

Commit

Permalink
in_node_exporter_metrics: check return values in ne_utils_file_read_sds.
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan committed Aug 7, 2023
1 parent 553983b commit 2719499
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions plugins/in_node_exporter_metrics/ne_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,13 @@ int ne_utils_file_read_lines(const char *mount, const char *path, struct mk_list
}

/*
* Read a file and every non-empty line is stored as a flb_slist_entry in the
* given list.
* Read a file and store the first line as a string.
*/
int ne_utils_file_read_sds(const char *mount,
const char *path,
const char *join_a,
const char *join_b,
flb_sds_t *str)
const char *join_a,
const char *join_b,
flb_sds_t *str)
{
int fd;
int len;
Expand All @@ -211,15 +210,27 @@ int ne_utils_file_read_sds(const char *mount,
flb_sds_cat_safe(&p, path, len);

if (join_a) {
flb_sds_cat_safe(&p, "/", 1);
if (flb_sds_cat_safe(&p, "/", 1) < 0) {
flb_sds_destroy(p);
return -1;
}
len = strlen(join_a);
flb_sds_cat_safe(&p, join_a, len);
if (flb_sds_cat_safe(&p, join_a, len) < 0) {
flb_sds_destroy(p);
return -1;
}
}

if (join_b) {
flb_sds_cat_safe(&p, "/", 1);
if (flb_sds_cat_safe(&p, "/", 1) < 0) {
flb_sds_destroy(p);
return -1;
}
len = strlen(join_b);
flb_sds_cat_safe(&p, join_b, len);
if (flb_sds_cat_safe(&p, join_b, len) < 0) {
flb_sds_destroy(p);
return -1;
}
}

fd = open(p, O_RDONLY);
Expand All @@ -238,9 +249,9 @@ int ne_utils_file_read_sds(const char *mount,
close(fd);

for (i = bytes-1; i > 0; i--) {
if (tmp[i] != '\n' && tmp[i] != '\r') {
break;
}
if (tmp[i] != '\n' && tmp[i] != '\r') {
break;
}
}

*str = flb_sds_create_len(tmp, i+1);
Expand Down

0 comments on commit 2719499

Please sign in to comment.