summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-04-01 16:23:47 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-04-04 15:18:00 +0200
commit68ecb48b90bd88c92253729304cac4647e143b8d (patch)
treed07105c7b18e0c3ba9fc7350ce921d955b04fc7c /src
parent4a055e5a3e8c8559c326dd4b3e01edbbcc70a86b (diff)
downloadsystemd-68ecb48b90bd88c92253729304cac4647e143b8d.tar.gz
core: simplify unit_escape_setting()
The function had a provision for NULL input, and would return NULL, but that looks like an error and all callers pass in a non-NULL arg and report oom on NULL. So assert that the input is non-NULL. All callers specifed the output buffer, so we can simplify the logic to only make an allocation if appropriate and change the return type to 'const *'. No functional change.
Diffstat (limited to 'src')
-rw-r--r--src/core/unit.c26
-rw-r--r--src/core/unit.h2
2 files changed, 11 insertions, 17 deletions
diff --git a/src/core/unit.c b/src/core/unit.c
index ce7b5a177c..e0deccc7b9 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -4310,20 +4310,18 @@ static const char* unit_drop_in_dir(Unit *u, UnitWriteFlags flags) {
return NULL;
}
-char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
+const char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
+ assert(s);
assert(!FLAGS_SET(flags, UNIT_ESCAPE_EXEC_SYNTAX | UNIT_ESCAPE_C));
+ assert(buf);
_cleanup_free_ char *t = NULL;
- if (!s)
- return NULL;
-
- /* Escapes the input string as requested. Returns the escaped string. If 'buf' is specified then the
- * allocated return buffer pointer is also written to *buf, except if no escaping was necessary, in
- * which case *buf is set to NULL, and the input pointer is returned as-is. This means the return
- * value always contains a properly escaped version, but *buf when passed only contains a pointer if
- * an allocation was necessary. If *buf is not specified, then the return value always needs to be
- * freed. Callers can use this to optimize memory allocations. */
+ /* Returns a string with any escaping done. If no escaping was necessary, *buf is set to NULL, and
+ * the input pointer is returned as-is. If an allocation was needed, the return buffer pointer is
+ * written to *buf. This means the return value always contains a properly escaped version, but *buf
+ * only contains a pointer if an allocation was made. Callers can use this to optimize memory
+ * allocations. */
if (flags & UNIT_ESCAPE_SPECIFIERS) {
t = specifier_escape(s);
@@ -4353,12 +4351,8 @@ char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
s = t;
}
- if (buf) {
- *buf = TAKE_PTR(t);
- return (char*) s;
- }
-
- return TAKE_PTR(t) ?: strdup(s);
+ *buf = TAKE_PTR(t);
+ return s;
}
char* unit_concat_strv(char **l, UnitWriteFlags flags) {
diff --git a/src/core/unit.h b/src/core/unit.h
index 513c8181f5..420405b2b7 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -964,7 +964,7 @@ ExecRuntime *unit_get_exec_runtime(Unit *u) _pure_;
int unit_setup_exec_runtime(Unit *u);
-char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf);
+const char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf);
char* unit_concat_strv(char **l, UnitWriteFlags flags);
int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data);