Sebastian Kuschel reports that pfd_closing can be called for a socket
[u/mdw/putty] / unix / uxsel.c
index 7142acb..e2979c9 100644 (file)
@@ -62,22 +62,18 @@ void uxsel_init(void)
 
 void uxsel_set(int fd, int rwx, uxsel_callback_fn callback)
 {
-    struct fd *newfd = snew(struct fd);
-    struct fd *oldfd;
+    struct fd *newfd;
 
-    newfd->fd = fd;
-    newfd->rwx = rwx;
-    newfd->callback = callback;
+    uxsel_del(fd);
 
-    oldfd = find234(fds, newfd, NULL);
-    if (oldfd) {
-       uxsel_input_remove(oldfd->id);
-       del234(fds, oldfd);
-       sfree(oldfd);
+    if (rwx) {
+       newfd = snew(struct fd);
+       newfd->fd = fd;
+       newfd->rwx = rwx;
+       newfd->callback = callback;
+       newfd->id = uxsel_input_add(fd, rwx);
+       add234(fds, newfd);
     }
-
-    add234(fds, newfd);
-    newfd->id = uxsel_input_add(fd, rwx);
 }
 
 void uxsel_del(int fd)
@@ -115,6 +111,13 @@ int first_fd(int *state, int *rwx)
 int select_result(int fd, int event)
 {
     struct fd *fdstruct = find234(fds, &fd, uxsel_fd_findcmp);
-    assert(fdstruct != NULL);
-    return fdstruct->callback(fd, event);
+    /*
+     * Apparently this can sometimes be NULL. Can't see how, but I
+     * assume it means I need to ignore the event since it's on an
+     * fd I've stopped being interested in. Sigh.
+     */
+    if (fdstruct)
+        return fdstruct->callback(fd, event);
+    else
+        return 1;
 }