From: Bart De Vos Date: Mon, 26 Aug 2019 13:38:12 +0000 (+0200) Subject: plugin: processes: use a const ref string and strlen i.s.o. magic offset X-Git-Tag: collectd-5.11.0~8^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f93354d30a6b0c574f3a09094e1a4982bfd96adf;p=thirdparty%2Fcollectd.git plugin: processes: use a const ref string and strlen i.s.o. magic offset The simple parser is based on 'strstr', whereas the actual data does include a white-space character to separate the 'id' (procs_running) from the 'value' (a number). Yet parsing the number can off course only take place 'after' the white-space. Subsequently, not finding a 'white-space' should be treated as an error condition. Adding the 'white-space' in const string and defining the offset based on 'strlen' fixes this properly. --- diff --git a/src/processes.c b/src/processes.c index f86cd39b1..2cf88a2fe 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1431,7 +1431,7 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) { static int procs_running(void) { char buffer[4096] = {}; - + char id[] = "procs_running "; /* white space terminated */ char *running; ssize_t status; @@ -1441,13 +1441,18 @@ static int procs_running(void) { return -1; } - running = strstr(buffer, "procs_running"); + /* the data contains : + * the literal string 'procs_running', + * a whitespace + * the number of running processes. + * The parser does include the white-space character. + */ + running = strstr(buffer, id); if (!running) { WARNING("procs_running not found, assuming 1"); return 1; } - - running += 14; + running += strlen(id); return atoi(running); }