10000 Factor out read_text_file_contents to ensure that data read from text files is a valid C string. by igorpeshansky · Pull Request #3359 · collectd/collectd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Factor out read_text_file_contents to ensure that data read from text files is a valid C string. #3359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,10 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */

snprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply, basename);

status = (int)read_file_contents(filename, buffer, buffer_size - 1);
status = (int)read_text_file_contents(filename, buffer, buffer_size);
if (status < 0)
return status;

buffer[status] = '\0';

strstripnewline(buffer);
return 0;
} /* }}} int sysfs_file_to_buffer */
Expand Down
5 changes: 2 additions & 3 deletions src/processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,10 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) {

snprintf(filename, sizeof(filename), "/proc/%li/stat", pid);

status = read_file_contents(filename, buffer, sizeof(buffer) - 1);
status = read_text_file_contents(filename, buffer, sizeof(buffer));
if (status <= 0)
return -1;
buffer_len = (size_t)status;
buffer[buffer_len] = 0;

/* The name of the process is enclosed in parens. Since the name can
* contain parens itself, spaces, numbers and pretty much everything
Expand Down Expand Up @@ -1569,7 +1568,7 @@ static char *ps_get_cmdline(long pid,

snprintf(path, sizeof(path), "/proc/%li/psinfo", pid);

status = read_file_contents(path, (void *)&info, sizeof(info));
status = read_file_contents(path, &info, sizeof(info));
if ((status < 0) || (((size_t)status) != sizeof(info))) {
ERROR("processes plugin: Unexpected return value "
"while reading \"%s\": "
Expand Down
2 changes: 1 addition & 1 deletion src/thermal.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
if ((len < 0) || ((size_t)len >= sizeof(filename)))
return -1;

len = (ssize_t)read_file_contents(filename, data, sizeof(data));
len = (ssize_t)read_text_file_contents(filename, data, sizeof(data));
if ((len > 0) && ((size_t)len > sizeof(str_temp)) && (data[--len] == '\n') &&
(!strncmp(data, str_temp, sizeof(str_temp) - 1))) {
char *endptr = NULL;
Expand Down
12 changes: 11 additions & 1 deletion src/utils/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ int walk_directory(const char *dir, dirwalk_callback_f callback,
return 0;
}

ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) {
ssize_t read_file_contents(const char *filename, void *buf, size_t bufsize) {
FILE *fh;
ssize_t ret;

Expand All @@ -1281,6 +1281,16 @@ ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) {
return ret;
}

ssize_t read_text_file_contents(const char *filename, char *buf,
size_t bufsize) {
ssize_t ret = read_file_contents(filename, buf, bufsize - 1);
if (ret < 0)
return ret;

buf[ret] = '\0';
return ret + 1;
}

counter_t counter_diff(counter_t old_value, counter_t new_value) {
counter_t diff;

Expand Down
6 changes: 5 additions & 1 deletion src/utils/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
int walk_directory(const char *dir, dirwalk_callback_f callback,
void *user_data, int hidden);
/* Returns the number of bytes read or negative on error. */
ssize_t read_file_contents(char const *filename, char *buf, size_t bufsize);
ssize_t read_file_contents(char const *filename, void *buf, size_t bufsize);
/* Writes the contents of the file into the buffer with a trailing NUL.
* Returns the number of bytes written to the buffer or negative on error. */
ssize_t read_text_file_contents(char const *filename, char *buf,
size_t bufsize);

counter_t counter_diff(counter_t old_value, counter_t new_value);

Expand Down
0