teknoraver / rpms / systemd

Forked from rpms/systemd a month ago
Clone

Blame SOURCES/0154-shared-specifier-treat-NULL-the-same-as.patch

594167
From c36354b26c757e526e9f3d8c5bc78aa36f095f61 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Thu, 20 Jan 2022 15:47:22 +0100
594167
Subject: [PATCH] shared/specifier: treat NULL the same as ""
594167
594167
We would busily allocate an empty string to concatenate all of it's
594167
zero characters to the output. Let's make things a bit simpler by letting
594167
the specifier functions return NULL to mean "nothing to append".
594167
594167
(cherry picked from commit 01c69460811f64e416c3e4a545ef84787bb6700b)
594167
594167
Related: #2082131
594167
---
594167
 src/shared/specifier.c    | 56 ++++++++++++++-------------------------
594167
 src/test/test-specifier.c |  9 ++++---
594167
 2 files changed, 25 insertions(+), 40 deletions(-)
594167
594167
diff --git a/src/shared/specifier.c b/src/shared/specifier.c
594167
index 1fd76b1d15..f8ab98541f 100644
594167
--- a/src/shared/specifier.c
594167
+++ b/src/shared/specifier.c
594167
@@ -35,7 +35,6 @@
594167
 int specifier_printf(const char *text, size_t max_length, const Specifier table[], const char *root, const void *userdata, char **ret) {
594167
         _cleanup_free_ char *result = NULL;
594167
         bool percent = false;
594167
-        const char *f;
594167
         size_t l;
594167
         char *t;
594167
         int r;
594167
@@ -48,8 +47,10 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
594167
                 return -ENOMEM;
594167
         t = result;
594167
 
594167
-        for (f = text; *f != '\0'; f++, l--) {
594167
+        for (const char *f = text; *f != '\0'; f++, l--) {
594167
                 if (percent) {
594167
+                        percent = false;
594167
+
594167
                         if (*f == '%')
594167
                                 *(t++) = '%';
594167
                         else {
594167
@@ -66,6 +67,8 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
594167
                                         r = i->lookup(i->specifier, i->data, root, userdata, &w);
594167
                                         if (r < 0)
594167
                                                 return r;
594167
+                                        if (isempty(w))
594167
+                                                continue;
594167
 
594167
                                         j = t - result;
594167
                                         k = strlen(w);
594167
@@ -82,8 +85,6 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
594167
                                         *(t++) = *f;
594167
                                 }
594167
                         }
594167
-
594167
-                        percent = false;
594167
                 } else if (*f == '%')
594167
                         percent = true;
594167
                 else
594167
@@ -108,11 +109,13 @@ int specifier_printf(const char *text, size_t max_length, const Specifier table[
594167
 /* Generic handler for simple string replacements */
594167
 
594167
 int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        char *n;
594167
+        char *n = NULL;
594167
 
594167
-        n = strdup(strempty(data));
594167
-        if (!n)
594167
-                return -ENOMEM;
594167
+        if (!isempty(data)) {
594167
+                n = strdup(data);
594167
+                if (!n)
594167
+                        return -ENOMEM;
594167
+        }
594167
 
594167
         *ret = n;
594167
         return 0;
594167
@@ -186,10 +189,8 @@ int specifier_short_host_name(char specifier, const void *data, const char *root
594167
 int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
         struct utsname uts;
594167
         char *n;
594167
-        int r;
594167
 
594167
-        r = uname(&uts;;
594167
-        if (r < 0)
594167
+        if (uname(&uts) < 0)
594167
                 return -errno;
594167
 
594167
         n = strdup(uts.release);
594167
@@ -211,47 +212,31 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
594167
         return 0;
594167
 }
594167
 
594167
-static int specifier_os_release_common(const char *field, const char *root, char **ret) {
594167
-        char *t = NULL;
594167
-        int r;
594167
-
594167
-        r = parse_os_release(root, field, &t);
594167
-        if (r < 0)
594167
-                return r;
594167
-        if (!t) {
594167
-                /* fields in /etc/os-release might quite possibly be missing, even if everything is entirely
594167
-                 * valid otherwise. Let's hence return "" in that case. */
594167
-                t = strdup("");
594167
-                if (!t)
594167
-                        return -ENOMEM;
594167
-        }
594167
-
594167
-        *ret = t;
594167
-        return 0;
594167
-}
594167
+/* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
594167
+ * otherwise. We'll return an empty value or NULL in that case from the functions below. */
594167
 
594167
 int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("ID", root, ret);
594167
+        return parse_os_release(root, "ID", ret);
594167
 }
594167
 
594167
 int specifier_os_version_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("VERSION_ID", root, ret);
594167
+        return parse_os_release(root, "VERSION_ID", ret);
594167
 }
594167
 
594167
 int specifier_os_build_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("BUILD_ID", root, ret);
594167
+        return parse_os_release(root, "BUILD_ID", ret);
594167
 }
594167
 
594167
 int specifier_os_variant_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("VARIANT_ID", root, ret);
594167
+        return parse_os_release(root, "VARIANT_ID", ret);
594167
 }
594167
 
594167
 int specifier_os_image_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("IMAGE_ID", root, ret);
594167
+        return parse_os_release(root, "IMAGE_ID", ret);
594167
 }
594167
 
594167
 int specifier_os_image_version(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-        return specifier_os_release_common("IMAGE_VERSION", root, ret);
594167
+        return parse_os_release(root, "IMAGE_VERSION", ret);
594167
 }
594167
 
594167
 int specifier_group_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
@@ -291,7 +276,6 @@ int specifier_user_name(char specifier, const void *data, const char *root, cons
594167
 }
594167
 
594167
 int specifier_user_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
594167
-
594167
         if (asprintf(ret, UID_FMT, getuid()) < 0)
594167
                 return -ENOMEM;
594167
 
594167
diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c
594167
index 40957eeb59..dda993ce9d 100644
594167
--- a/src/test/test-specifier.c
594167
+++ b/src/test/test-specifier.c
594167
@@ -56,6 +56,7 @@ TEST(specifier_printf) {
594167
         static const Specifier table[] = {
594167
                 { 'X', specifier_string,         (char*) "AAAA" },
594167
                 { 'Y', specifier_string,         (char*) "BBBB" },
594167
+                { 'e', specifier_string,         NULL           },
594167
                 COMMON_SYSTEM_SPECIFIERS,
594167
                 {}
594167
         };
594167
@@ -63,21 +64,21 @@ TEST(specifier_printf) {
594167
         _cleanup_free_ char *w = NULL;
594167
         int r;
594167
 
594167
-        r = specifier_printf("xxx a=%X b=%Y yyy", SIZE_MAX, table, NULL, NULL, &w);
594167
+        r = specifier_printf("xxx a=%X b=%Y e=%e yyy", SIZE_MAX, table, NULL, NULL, &w);
594167
         assert_se(r >= 0);
594167
         assert_se(w);
594167
 
594167
         puts(w);
594167
-        assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
594167
+        assert_se(streq(w, "xxx a=AAAA b=BBBB e= yyy"));
594167
 
594167
         free(w);
594167
-        r = specifier_printf("machine=%m, boot=%b, host=%H, version=%v, arch=%a", SIZE_MAX, table, NULL, NULL, &w);
594167
+        r = specifier_printf("machine=%m, boot=%b, host=%H, version=%v, arch=%a, empty=%e", SIZE_MAX, table, NULL, NULL, &w);
594167
         assert_se(r >= 0);
594167
         assert_se(w);
594167
         puts(w);
594167
 
594167
         w = mfree(w);
594167
-        specifier_printf("os=%o, os-version=%w, build=%B, variant=%W", SIZE_MAX, table, NULL, NULL, &w);
594167
+        specifier_printf("os=%o, os-version=%w, build=%B, variant=%W, empty=%e%e%e", SIZE_MAX, table, NULL, NULL, &w);
594167
         if (w)
594167
                 puts(w);
594167
 }