21255d
From deb9e6ad3a1d7cfbc3b53d1e74cda6ae398a90fd Mon Sep 17 00:00:00 2001
21255d
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
21255d
Date: Tue, 10 Nov 2020 10:38:37 +0100
21255d
Subject: [PATCH] sd-event: update state at the end in event_source_enable
21255d
21255d
Coverity in CID#1435966 was complaining that s->enabled is not "restored" in
21255d
all cases. But the code was actually correct, since it should only be
21255d
"restored" in the error paths. But let's still make this prettier by not setting
21255d
the state before all operations that may fail are done.
21255d
21255d
We need to set .enabled for the prioq reshuffling operations, so move those down.
21255d
21255d
No functional change intended.
21255d
21255d
(cherry picked from commit d2eafe61ca07f8300dc741a0491a914213fa2b6b)
21255d
21255d
Related: #1819868
21255d
---
21255d
 src/libsystemd/sd-event/sd-event.c | 51 +++++++++++++++++-------------
21255d
 1 file changed, 29 insertions(+), 22 deletions(-)
21255d
21255d
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
21255d
index 34b42c298f..0cfba8fb39 100644
21255d
--- a/src/libsystemd/sd-event/sd-event.c
21255d
+++ b/src/libsystemd/sd-event/sd-event.c
21255d
@@ -2352,11 +2352,11 @@ static int event_source_disable(sd_event_source *s) {
21255d
         return 0;
21255d
 }
21255d
 
21255d
-static int event_source_enable(sd_event_source *s, int m) {
21255d
+static int event_source_enable(sd_event_source *s, int enable) {
21255d
         int r;
21255d
 
21255d
         assert(s);
21255d
-        assert(IN_SET(m, SD_EVENT_ON, SD_EVENT_ONESHOT));
21255d
+        assert(IN_SET(enable, SD_EVENT_ON, SD_EVENT_ONESHOT));
21255d
         assert(s->enabled == SD_EVENT_OFF);
21255d
 
21255d
         /* Unset the pending flag when this event source is enabled */
21255d
@@ -2366,31 +2366,16 @@ static int event_source_enable(sd_event_source *s, int m) {
21255d
                         return r;
21255d
         }
21255d
 
21255d
-        s->enabled = m;
21255d
-
21255d
         switch (s->type) {
21255d
-
21255d
         case SOURCE_IO:
21255d
-                r = source_io_register(s, m, s->io.events);
21255d
-                if (r < 0) {
21255d
-                        s->enabled = SD_EVENT_OFF;
21255d
+                r = source_io_register(s, enable, s->io.events);
21255d
+                if (r < 0)
21255d
                         return r;
21255d
-                }
21255d
-
21255d
-                break;
21255d
-
21255d
-        case SOURCE_TIME_REALTIME:
21255d
-        case SOURCE_TIME_BOOTTIME:
21255d
-        case SOURCE_TIME_MONOTONIC:
21255d
-        case SOURCE_TIME_REALTIME_ALARM:
21255d
-        case SOURCE_TIME_BOOTTIME_ALARM:
21255d
-                event_source_time_prioq_reshuffle(s);
21255d
                 break;
21255d
 
21255d
         case SOURCE_SIGNAL:
21255d
                 r = event_make_signal_data(s->event, s->signal.sig, NULL);
21255d
                 if (r < 0) {
21255d
-                        s->enabled = SD_EVENT_OFF;
21255d
                         event_gc_signal_data(s->event, &s->priority, s->signal.sig);
21255d
                         return r;
21255d
                 }
21255d
@@ -2411,10 +2396,12 @@ static int event_source_enable(sd_event_source *s, int m) {
21255d
 
21255d
                 break;
21255d
 
21255d
+        case SOURCE_TIME_REALTIME:
21255d
+        case SOURCE_TIME_BOOTTIME:
21255d
+        case SOURCE_TIME_MONOTONIC:
21255d
+        case SOURCE_TIME_REALTIME_ALARM:
21255d
+        case SOURCE_TIME_BOOTTIME_ALARM:
21255d
         case SOURCE_EXIT:
21255d
-                prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
21255d
-                break;
21255d
-
21255d
         case SOURCE_DEFER:
21255d
         case SOURCE_POST:
21255d
         case SOURCE_INOTIFY:
21255d
@@ -2424,6 +2411,26 @@ static int event_source_enable(sd_event_source *s, int m) {
21255d
                 assert_not_reached("Wut? I shouldn't exist.");
21255d
         }
21255d
 
21255d
+        s->enabled = enable;
21255d
+
21255d
+        /* Non-failing operations below */
21255d
+        switch (s->type) {
21255d
+        case SOURCE_TIME_REALTIME:
21255d
+        case SOURCE_TIME_BOOTTIME:
21255d
+        case SOURCE_TIME_MONOTONIC:
21255d
+        case SOURCE_TIME_REALTIME_ALARM:
21255d
+        case SOURCE_TIME_BOOTTIME_ALARM:
21255d
+                event_source_time_prioq_reshuffle(s);
21255d
+                break;
21255d
+
21255d
+        case SOURCE_EXIT:
21255d
+                prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
21255d
+                break;
21255d
+
21255d
+        default:
21255d
+                break;
21255d
+        }
21255d
+
21255d
         return 0;
21255d
 }
21255d