s3:messages: check tevent_fd_get_flags() == 0 before using stale event context pointer
[samba.git] / source3 / lib / messages_dgm.c
index b9cddc274c223b94a0792ce968e3460218c6343b..b8878b68b9967378f72741631dd7cd7d841c8e79 100644 (file)
@@ -1679,8 +1679,18 @@ struct messaging_dgm_fde *messaging_dgm_register_tevent_context(
        }
 
        for (fde_ev = ctx->fde_evs; fde_ev != NULL; fde_ev = fde_ev->next) {
-               if ((fde_ev->ev == ev) &&
-                   (tevent_fd_get_flags(fde_ev->fde) != 0)) {
+               if (tevent_fd_get_flags(fde_ev->fde) == 0) {
+                       /*
+                        * If the event context got deleted,
+                        * tevent_fd_get_flags() will return 0
+                        * for the stale fde.
+                        *
+                        * In that case we should not
+                        * use fde_ev->ev anymore.
+                        */
+                       continue;
+               }
+               if (fde_ev->ev == ev) {
                        break;
                }
        }