Merge pull request #26958 from yuwata/nulstr-optionally-drop-trailing-nulstr
nulstr: make strv_parse_nulstr() optionally drop trailing NULs
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
|
||||
char** strv_parse_nulstr(const char *s, size_t l) {
|
||||
char** strv_parse_nulstr_full(const char *s, size_t l, bool drop_trailing_nuls) {
|
||||
/* l is the length of the input data, which will be split at NULs into elements of the resulting
|
||||
* strv. Hence, the number of items in the resulting strv will be equal to one plus the number of NUL
|
||||
* bytes in the l bytes starting at s, unless s[l-1] is NUL, in which case the final empty string is
|
||||
@@ -18,6 +18,10 @@ char** strv_parse_nulstr(const char *s, size_t l) {
|
||||
|
||||
assert(s || l <= 0);
|
||||
|
||||
if (drop_trailing_nuls)
|
||||
while (l > 0 && s[l-1] == '\0')
|
||||
l--;
|
||||
|
||||
if (l <= 0)
|
||||
return new0(char*, 1);
|
||||
|
||||
|
||||
@@ -20,7 +20,10 @@ static inline bool nulstr_contains(const char *nulstr, const char *needle) {
|
||||
return nulstr_get(nulstr, needle);
|
||||
}
|
||||
|
||||
char** strv_parse_nulstr(const char *s, size_t l);
|
||||
char** strv_parse_nulstr_full(const char *s, size_t l, bool drop_trailing_nuls);
|
||||
static inline char** strv_parse_nulstr(const char *s, size_t l) {
|
||||
return strv_parse_nulstr_full(s, l, false);
|
||||
}
|
||||
char** strv_split_nulstr(const char *s);
|
||||
int strv_make_nulstr(char * const *l, char **p, size_t *n);
|
||||
int set_make_nulstr(Set *s, char **ret, size_t *ret_size);
|
||||
|
||||
@@ -222,18 +222,12 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags
|
||||
|
||||
_cleanup_strv_free_ char **args = NULL;
|
||||
|
||||
args = strv_parse_nulstr(t, k);
|
||||
/* Drop trailing NULs, otherwise strv_parse_nulstr() adds additional empty strings at the end.
|
||||
* See also issue #21186. */
|
||||
args = strv_parse_nulstr_full(t, k, /* drop_trailing_nuls = */ true);
|
||||
if (!args)
|
||||
return -ENOMEM;
|
||||
|
||||
/* Drop trailing empty strings. See issue #21186. */
|
||||
STRV_FOREACH_BACKWARDS(p, args) {
|
||||
if (!isempty(*p))
|
||||
break;
|
||||
|
||||
*p = mfree(*p);
|
||||
}
|
||||
|
||||
ans = quote_command_line(args, shflags);
|
||||
if (!ans)
|
||||
return -ENOMEM;
|
||||
|
||||
Reference in New Issue
Block a user