util-lib: drop trailing non-printable characters from cmdline (#3512)
If max_length is equal or greater than cmdline length all trailing non-printable characters are dropped. If max_length is 0 it should do the same. This should also fix cmdline truncation if the last character is not '\0'. Fixes #3469.
This commit is contained in:
parent
e7b90ddc34
commit
ba4cd7e257
|
@ -102,6 +102,7 @@ int get_process_comm(pid_t pid, char **name) {
|
||||||
|
|
||||||
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
|
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
|
||||||
_cleanup_fclose_ FILE *f = NULL;
|
_cleanup_fclose_ FILE *f = NULL;
|
||||||
|
bool space = false;
|
||||||
char *r = NULL, *k;
|
char *r = NULL, *k;
|
||||||
const char *p;
|
const char *p;
|
||||||
int c;
|
int c;
|
||||||
|
@ -128,14 +129,21 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
r[len++] = isprint(c) ? c : ' ';
|
if (isprint(c)) {
|
||||||
}
|
if (space) {
|
||||||
|
r[len++] = ' ';
|
||||||
|
space = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
r[len++] = c;
|
||||||
|
} else
|
||||||
|
space = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
r[len-1] = 0;
|
r[len] = 0;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
bool space = false;
|
|
||||||
size_t left;
|
size_t left;
|
||||||
|
|
||||||
r = new(char, max_length);
|
r = new(char, max_length);
|
||||||
|
|
Loading…
Reference in a new issue