[systemd-devel] [PATCH] core: avoid needless strdup by working with strings directly

Shawn Landden shawnlandden at gmail.com
Wed Aug 15 18:13:40 PDT 2012


---
 src/core/main.c |   51 ++++++++++++++++++++++-----------------------------
 1 file changed, 22 insertions(+), 29 deletions(-)

diff --git a/src/core/main.c b/src/core/main.c
index cdd77c1..e9b656b 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -339,12 +339,11 @@ static int parse_proc_cmdline_word(const char *word) {
                 else
                         arg_default_std_error = r;
         } else if (startswith(word, "systemd.setenv=")) {
-                char *cenv, *eq;
+                const char *cenv;
+                char *eq;
                 int r;
 
-                cenv = strdup(word + 15);
-                if (!cenv)
-                        return -ENOMEM;
+                cenv = word + 15;
 
                 eq = strchr(cenv, '=');
                 if (!eq) {
@@ -352,12 +351,12 @@ static int parse_proc_cmdline_word(const char *word) {
                         if (r < 0)
                                 log_warning("unsetenv failed %m. Ignoring.");
                 } else {
-                        *eq = 0;
+                        *eq = '\0';
                         r = setenv(cenv, eq + 1, 1);
+                        *eq = '=';
                         if (r < 0)
                                 log_warning("setenv failed %m. Ignoring.");
                 }
-                free(cenv);
 
         } else if (startswith(word, "systemd.") ||
                    (in_initrd() && startswith(word, "rd.systemd."))) {
@@ -490,15 +489,14 @@ static int config_parse_cpu_affinity2(
         assert(rvalue);
 
         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
-                char *t;
                 int r;
                 unsigned cpu;
+                char t;
 
-                if (!(t = strndup(w, l)))
-                        return log_oom();
-
-                r = safe_atou(t, &cpu);
-                free(t);
+                t = w[l+1];
+                w[l+1] = '\0';
+                r = safe_atou(w, &cpu);
+                w[l+1] = t;
 
                 if (!c)
                         if (!(c = cpu_set_malloc(&ncpus)))
@@ -564,14 +562,12 @@ static int config_parse_join_controllers(
         free_join_controllers();
 
         FOREACH_WORD_QUOTED(w, length, rvalue, state) {
-                char *s, **l;
-
-                s = strndup(w, length);
-                if (!s)
-                        return log_oom();
+                char **l, q;
 
-                l = strv_split(s, ",");
-                free(s);
+                q = w[length+1];
+                w[length+1] = '\0';
+                l = strv_split(w, ",");
+                w[length+1] = q;
 
                 strv_uniq(l);
 
@@ -704,7 +700,7 @@ static int parse_config_file(void) {
 }
 
 static int parse_proc_cmdline(void) {
-        char *line, *w, *state;
+        char *line, *word, *state;
         int r;
         size_t l;
 
@@ -718,19 +714,16 @@ static int parse_proc_cmdline(void) {
                 return 0;
         }
 
-        FOREACH_WORD_QUOTED(w, l, line, state) {
-                char *word;
-
-                if (!(word = strndup(w, l))) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+        FOREACH_WORD_QUOTED(word, l, line, state) {
+                char c;
 
+                c = word[l+1];
+                word[l+1] = '\0';
                 r = parse_proc_cmdline_word(word);
-                free(word);
+                word[l+1] = c;
 
                 if (r < 0) {
-                        log_error("Failed on cmdline argument %s: %s", word, strerror(-r));
+                        log_error("Failed on cmdline argument %.*s: %s", (int)l, word, strerror(-r));
                         goto finish;
                 }
         }
-- 
1.7.9.5



More information about the systemd-devel mailing list