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