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

Shawn Landden shawnlandden at gmail.com
Thu Aug 16 10:23:09 PDT 2012


config_parse_join_controllers() not converted cause that is a const char *
---
 src/core/main.c |   38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/src/core/main.c b/src/core/main.c
index cdd77c1..528cfec 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -237,7 +237,7 @@ static int set_default_unit(const char *u) {
         return 0;
 }
 
-static int parse_proc_cmdline_word(const char *word) {
+static int parse_proc_cmdline_word(char *word) {
 
         static const char * const rlmap[] = {
                 "emergency", SPECIAL_EMERGENCY_TARGET,
@@ -342,9 +342,7 @@ static int parse_proc_cmdline_word(const char *word) {
                 char *cenv, *eq;
                 int r;
 
-                cenv = strdup(word + 15);
-                if (!cenv)
-                        return -ENOMEM;
+                cenv = word + 15;
 
                 eq = strchr(cenv, '=');
                 if (!eq) {
@@ -352,12 +350,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 +488,14 @@ static int config_parse_cpu_affinity2(
         assert(rvalue);
 
         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
-                char *t;
                 int r;
                 unsigned cpu;
+                char ch;
 
-                if (!(t = strndup(w, l)))
-                        return log_oom();
-
-                r = safe_atou(t, &cpu);
-                free(t);
+                ch = w[l+1];
+                w[l+1] = '\0';
+                r = safe_atou(w, &cpu);
+                w[l+1] = ch;
 
                 if (!c)
                         if (!(c = cpu_set_malloc(&ncpus)))
@@ -704,7 +701,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 +715,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 ch;
 
+                ch = word[l+1];
+                word[l+1] = '\0';
                 r = parse_proc_cmdline_word(word);
-                free(word);
+                word[l+1] = ch;
 
                 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