1. angri
  2. libevent-python2

Commits

angri  committed 31bffa1

Do not allow to call addToLoop() if event object is already in the loop and removeFromLoop() if it is not there. Now it should be harder for python code to provoke segfault.

  • Participants
  • Parent commits dca8f37
  • Branches default

Comments (0)

Files changed (1)

File libevent/eventmodule.c

View file
  • Ignore whitespace
     struct event ev;
     EventBaseObject *eventBase;
     PyObject *callback;
+    unsigned short int isInLoop;
 } EventObject;
 
 /* Forward declaration of CPython type object */
     Py_INCREF(callback);
     self->callback = callback;
     self->eventBase = NULL;
+    self->isInLoop = 0;
     PyObject_GC_Track(self);
     return 0;
 }
     static char    *kwlist[] = {"timeout", NULL};
     int             rv;
 
+    if (self->isInLoop != 0) {
+	PyErr_SetString(EventErrorObject,
+			"event is already added to the loop");
+	return NULL;
+    }
+    self->isInLoop = 1;
+
     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d:addToLoop", kwlist,
 				     &timeout))
         return NULL;
 static PyObject *Event_RemoveFromLoop(EventObject *self, PyObject *args,
 				      PyObject *kwargs) {
 
+    if (self->isInLoop != 1) {
+	PyErr_SetString(EventErrorObject, "event is not in the loop");
+	return NULL;
+    }
+    self->isInLoop = 0;
+
     if (event_del(&self->ev) < 0) {
 	PyErr_SetFromErrno(EventErrorObject);
 	return NULL;
      RO, "Events registered for this event object"},
     {"numCalls",  T_SHORT,  OFF(ev.ev_ncalls),
      RO, "Number of times this event has been called"},
+    {"isInLoop",  T_BOOL,   OFF(isInLoop),
+     RO, "Was the event added to the loop or not"},
     {"priority",  T_INT,    OFF(ev.ev_pri),
      RO, "Event priority"},
     {"flags",     T_INT,    OFF(ev.ev_flags),