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