From: Zbigniew Jędrzejewski-Szmek Date: Fri, 31 Jul 2020 09:19:25 +0000 (+0200) Subject: core/load-fragment: use extract_first_word() X-Git-Tag: v247-rc1~275^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7896ad8f6601c8e8ffec5ca6bd9a035f9b5de805;p=thirdparty%2Fsystemd.git core/load-fragment: use extract_first_word() This is much nicer, and also fixes a potential overflow when we used 'word' in log_error() as if it was a NUL-terminated string. --- diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index a93f12b27c4..7c5730ac0e8 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -4430,15 +4430,13 @@ int config_parse_set_status( void *data, void *userdata) { - size_t l; - const char *word, *state; - int r; ExitStatusSet *status_set = data; + int r; assert(filename); assert(lvalue); assert(rvalue); - assert(data); + assert(status_set); /* Empty assignment resets the list */ if (isempty(rvalue)) { @@ -4446,25 +4444,26 @@ int config_parse_set_status( return 0; } - FOREACH_WORD(word, l, rvalue, state) { - _cleanup_free_ char *temp; + for (const char *p = rvalue;;) { + _cleanup_free_ char *word = NULL; Bitmap *bitmap; - temp = strndup(word, l); - if (!temp) - return log_oom(); + r = extract_first_word(&p, &word, NULL, 0); + if (r < 0) + return log_error_errno(r, "Failed to parse %s: %m", lvalue); + if (r == 0) + return 0; /* We need to call exit_status_from_string() first, because we want * to parse numbers as exit statuses, not signals. */ - r = exit_status_from_string(temp); + r = exit_status_from_string(word); if (r >= 0) { assert(r >= 0 && r < 256); bitmap = &status_set->status; } else { - r = signal_from_string(temp); - - if (r <= 0) { + r = signal_from_string(word); + if (r < 0) { log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word); continue; @@ -4476,10 +4475,6 @@ int config_parse_set_status( if (r < 0) return log_error_errno(r, "Failed to set signal or status %s: %m", word); } - if (!isempty(state)) - log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring."); - - return 0; } int config_parse_namespace_path_strv(