From 648294227e8247f729d8cca927d3445ab1836f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 23 Oct 2016 11:43:27 -0400 Subject: tree-wide: drop NULL sentinel from strjoin This makes strjoin and strjoina more similar and avoids the useless final argument. spatch -I . -I ./src -I ./src/basic -I ./src/basic -I ./src/shared -I ./src/shared -I ./src/network -I ./src/locale -I ./src/login -I ./src/journal -I ./src/journal -I ./src/timedate -I ./src/timesync -I ./src/nspawn -I ./src/resolve -I ./src/resolve -I ./src/elogind -I ./src/core -I ./src/core -I ./src/libudev -I ./src/udev -I ./src/udev/net -I ./src/udev -I ./src/libelogind/sd-bus -I ./src/libelogind/sd-event -I ./src/libelogind/sd-login -I ./src/libelogind/sd-netlink -I ./src/libelogind/sd-network -I ./src/libelogind/sd-hwdb -I ./src/libelogind/sd-device -I ./src/libelogind/sd-id128 -I ./src/libelogind-network --sp-file coccinelle/strjoin.cocci --in-place $(git ls-files src/*.c) git grep -e '\bstrjoin\b.*NULL' -l|xargs sed -i -r 's/strjoin\((.*), NULL\)/strjoin(\1)/' This might have missed a few cases (spatch has a really hard time dealing with _cleanup_ macros), but that's no big issue, they can always be fixed later. --- src/basic/fileio.c | 149 +++++++++++++---------------------------------------- 1 file changed, 36 insertions(+), 113 deletions(-) (limited to 'src/basic/fileio.c') diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 5b20fbb49..f4915b28a 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -585,9 +585,14 @@ fail: return r; } -static int check_utf8ness_and_warn( +static int parse_env_file_push( const char *filename, unsigned line, - const char *key, char *value) { + const char *key, char *value, + void *userdata, + int *n_pushed) { + + const char *k; + va_list aq, *ap = userdata; if (!utf8_is_valid(key)) { _cleanup_free_ char *p = NULL; @@ -605,23 +610,6 @@ static int check_utf8ness_and_warn( return -EINVAL; } - return 0; -} - -static int parse_env_file_push( - const char *filename, unsigned line, - const char *key, char *value, - void *userdata, - int *n_pushed) { - - const char *k; - va_list aq, *ap = userdata; - int r; - - r = check_utf8ness_and_warn(filename, line, key, value); - if (r < 0) - return r; - va_copy(aq, *ap); while ((k = va_arg(aq, const char *))) { @@ -674,19 +662,27 @@ static int load_env_file_push( char *p; int r; - r = check_utf8ness_and_warn(filename, line, key, value); - if (r < 0) - return r; + if (!utf8_is_valid(key)) { + _cleanup_free_ char *t = utf8_escape_invalid(key); + + log_error("%s:%u: invalid UTF-8 for key '%s', ignoring.", strna(filename), line, t); + return -EINVAL; + } + + if (value && !utf8_is_valid(value)) { + _cleanup_free_ char *t = utf8_escape_invalid(value); + + log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, t); + return -EINVAL; + } - p = strjoin(key, "=", value); + p = strjoin(key, "=", strempty(value)); if (!p) return -ENOMEM; - r = strv_env_replace(m, p); - if (r < 0) { - free(p); + r = strv_consume(m, p); + if (r < 0) return r; - } if (n_pushed) (*n_pushed)++; @@ -720,9 +716,19 @@ static int load_env_file_push_pairs( char ***m = userdata; int r; - r = check_utf8ness_and_warn(filename, line, key, value); - if (r < 0) - return r; + if (!utf8_is_valid(key)) { + _cleanup_free_ char *t = utf8_escape_invalid(key); + + log_error("%s:%u: invalid UTF-8 for key '%s', ignoring.", strna(filename), line, t); + return -EINVAL; + } + + if (value && !utf8_is_valid(value)) { + _cleanup_free_ char *t = utf8_escape_invalid(value); + + log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, t); + return -EINVAL; + } r = strv_extend(m, key); if (r < 0) @@ -761,51 +767,6 @@ int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ** return 0; } -static int merge_env_file_push( - const char *filename, unsigned line, - const char *key, char *value, - void *userdata, - int *n_pushed) { - - char ***env = userdata; - char *expanded_value; - - assert(env); - - if (!value) { - log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key); - return 0; - } - - if (!env_name_is_valid(key)) { - log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key); - return 0; - } - - expanded_value = replace_env(value, *env, - REPLACE_ENV_USE_ENVIRONMENT| - REPLACE_ENV_ALLOW_BRACELESS| - REPLACE_ENV_ALLOW_EXTENDED); - if (!expanded_value) - return -ENOMEM; - - free_and_replace(value, expanded_value); - - return load_env_file_push(filename, line, key, value, env, n_pushed); -} - -int merge_env_file( - char ***env, - FILE *f, - const char *fname) { - - /* NOTE: this function supports braceful and braceless variable expansions, - * plus "extended" substitutions, unlike other exported parsing functions. - */ - - return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL); -} - static void write_env_var(FILE *f, const char *v) { const char *p; @@ -1385,25 +1346,6 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) { return fd; } -int open_serialization_fd(const char *ident) { - int fd = -1; - - fd = memfd_create(ident, MFD_CLOEXEC); - if (fd < 0) { - const char *path; - - path = getpid() == 1 ? "/run/systemd" : "/tmp"; - fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC); - if (fd < 0) - return fd; - - log_debug("Serializing %s to %s.", ident, path); - } else - log_debug("Serializing %s to memfd.", ident); - - return fd; -} - int link_tmpfile(int fd, const char *path, const char *target) { assert(fd >= 0); @@ -1472,22 +1414,3 @@ int read_nul_string(FILE *f, char **ret) { return 0; } - -int mkdtemp_malloc(const char *template, char **ret) { - char *p; - - assert(template); - assert(ret); - - p = strdup(template); - if (!p) - return -ENOMEM; - - if (!mkdtemp(p)) { - free(p); - return -errno; - } - - *ret = p; - return 0; -} -- cgit v1.2.3