anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
572a44
From 9835435400e945b294872a525aacf99a5ad5bcf5 Mon Sep 17 00:00:00 2001
572a44
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
572a44
Date: Tue, 31 Dec 2013 11:23:58 -0500
572a44
Subject: [PATCH] sleep-config: fix double free
572a44
572a44
Before 34a3baa4d 'sleep-config: Dereference pointer before check for NULL'
572a44
oom conditions would not be detected properly. After that commit, a double
572a44
free was performed.
572a44
572a44
Rework the whole function to be easier to understand, and also replace
572a44
strv_split_nulstr with strv_new, since we know the strings anyway.
572a44
---
572a44
 src/shared/sleep-config.c | 38 ++++++++++++++++++++------------------
572a44
 1 file changed, 20 insertions(+), 18 deletions(-)
572a44
572a44
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
572a44
index b2a0787..70a0896 100644
572a44
--- a/src/shared/sleep-config.c
572a44
+++ b/src/shared/sleep-config.c
572a44
@@ -28,11 +28,14 @@
572a44
 #include "strv.h"
572a44
 #include "util.h"
572a44
 
572a44
-int parse_sleep_config(const char *verb, char ***modes, char ***states) {
572a44
+#define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0)
572a44
+
572a44
+int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
572a44
         _cleanup_strv_free_ char
572a44
                 **suspend_mode = NULL, **suspend_state = NULL,
572a44
                 **hibernate_mode = NULL, **hibernate_state = NULL,
572a44
                 **hybrid_mode = NULL, **hybrid_state = NULL;
572a44
+        char **modes, **states;
572a44
 
572a44
         const ConfigTableItem items[] = {
572a44
                 { "Sleep",   "SuspendMode",      config_parse_strv,  0, &suspend_mode  },
572a44
@@ -59,47 +62,46 @@ int parse_sleep_config(const char *verb, char ***modes, char ***states) {
572a44
 
572a44
         if (streq(verb, "suspend")) {
572a44
                 /* empty by default */
572a44
-                *modes = suspend_mode;
572a44
+                USE(modes, suspend_mode);
572a44
 
572a44
                 if (suspend_state)
572a44
-                        *states = suspend_state;
572a44
+                        USE(states, suspend_state);
572a44
                 else
572a44
-                        *states = strv_split_nulstr("mem\0standby\0freeze\0");
572a44
+                        states = strv_new("mem", "standby", "freeze", NULL);
572a44
 
572a44
-                suspend_mode = suspend_state = NULL;
572a44
         } else if (streq(verb, "hibernate")) {
572a44
                 if (hibernate_mode)
572a44
-                        *modes = hibernate_mode;
572a44
+                        USE(modes, hibernate_mode);
572a44
                 else
572a44
-                        *modes = strv_split_nulstr("platform\0shutdown\0");
572a44
+                        modes = strv_new("platform", "shutdown", NULL);
572a44
 
572a44
                 if (hibernate_state)
572a44
-                        *states = hibernate_state;
572a44
+                        USE(states, hibernate_state);
572a44
                 else
572a44
-                        *states = strv_split_nulstr("disk\0");
572a44
+                        states = strv_new("disk", NULL);
572a44
 
572a44
-                hibernate_mode = hibernate_state = NULL;
572a44
         } else if (streq(verb, "hybrid-sleep")) {
572a44
                 if (hybrid_mode)
572a44
-                        *modes = hybrid_mode;
572a44
+                        USE(modes, hybrid_mode);
572a44
                 else
572a44
-                        *modes = strv_split_nulstr("suspend\0platform\0shutdown\0");
572a44
+                        modes = strv_new("suspend", "platform", "shutdown", NULL);
572a44
 
572a44
                 if (hybrid_state)
572a44
-                        *states = hybrid_state;
572a44
+                        USE(states, hybrid_state);
572a44
                 else
572a44
-                        *states = strv_split_nulstr("disk\0");
572a44
+                        states = strv_new("disk", NULL);
572a44
 
572a44
-                hybrid_mode = hybrid_state = NULL;
572a44
         } else
572a44
                 assert_not_reached("what verb");
572a44
 
572a44
-        if (!*modes || !*states) {
572a44
-                strv_free(*modes);
572a44
-                strv_free(*states);
572a44
+        if ((!modes && !streq(verb, "suspend")) || !states) {
572a44
+                strv_free(modes);
572a44
+                strv_free(states);
572a44
                 return log_oom();
572a44
         }
572a44
 
572a44
+        *_modes = modes;
572a44
+        *_states = states;
572a44
         return 0;
572a44
 }
572a44