chiark / gitweb /
core/load-fragment: avoid allocating 0 bytes when given an invalid command
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 11 Jan 2015 21:57:02 +0000 (16:57 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 12 Jan 2015 04:41:42 +0000 (23:41 -0500)
With a command line like "@/something" we would allocate an array with
0 elements. Avoid that, and add a test too.

src/core/load-fragment.c
src/test/test-unit-file.c

index eea415883b53ada8ccd0456c8ff90719c71321e0..242b684ddf1b55e52eeec7e65b26b1b33e1eb791 100644 (file)
@@ -589,7 +589,8 @@ int config_parse_exec(const char *unit,
                 }
 
         found:
                 }
 
         found:
-                n = new(char*, k + !separate_argv0);
+                /* If seperate_argv0, we'll move first element to path variable */
+                n = new(char*, MAX(k + !separate_argv0, 1u));
                 if (!n)
                         return log_oom();
 
                 if (!n)
                         return log_oom();
 
index d6a7d439150be1e0c4168f0447c7bb7ac1367ae1..e517f571d6901c7799b91327801976279644bb08 100644 (file)
@@ -137,6 +137,20 @@ static void test_config_parse_exec(void) {
         c1 = c1->command_next;
         check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false);
 
         c1 = c1->command_next;
         check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false);
 
+        log_info("/* honour_argv0, no args */");
+        r = config_parse_exec(NULL, "fake", 3, "section", 1,
+                              "LValue", 0, "@/RValue",
+                              &c, NULL);
+        assert_se(r == 0);
+        assert_se(c1->command_next == NULL);
+
+        log_info("/* no command, check for bad memory access */");
+        r = config_parse_exec(NULL, "fake", 3, "section", 1,
+                              "LValue", 0, "    ",
+                              &c, NULL);
+        assert_se(r == 0);
+        assert_se(c1->command_next == NULL);
+
         log_info("/* ignore && honour_argv0 */");
         r = config_parse_exec(NULL, "fake", 4, "section", 1,
                               "LValue", 0, "-@/RValue///slashes3 argv0a r1",
         log_info("/* ignore && honour_argv0 */");
         r = config_parse_exec(NULL, "fake", 4, "section", 1,
                               "LValue", 0, "-@/RValue///slashes3 argv0a r1",