From 6defc05f0a7226a260d7f80e6708dad1ca5b66f5 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Wed, 22 Jun 2011 12:20:05 +0200 Subject: [PATCH] core: fix freeze when hook_fd is called with a bad file/socket (bug #33619) --- ChangeLog | 1 + src/core/wee-hook.c | 39 +++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9e5b91118..6d559436b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ v0.3.6-dev, 2011-06-22 Version 0.3.6 (under dev!) -------------------------- +* core: fix freeze when hook_fd is called with a bad file/socket (bug #33619) * core: fix bug with option weechat.look.hotlist_count_max (value+1 was used) * core: add local variable "highlight_regex" in buffers * core: add "hdata" (direct access to WeeChat/plugin data) diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index e20c0aeb9..ffea7a7ac 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include "weechat.h" #include "wee-hook.h" @@ -1221,23 +1223,28 @@ hook_fd_set (fd_set *read_fds, fd_set *write_fds, fd_set *exception_fds) { if (!ptr_hook->deleted) { - if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_READ) + /* skip invalid file descriptors */ + if ((fcntl (HOOK_FD(ptr_hook,fd), F_GETFD) != -1) + || (errno != EBADF)) { - FD_SET (HOOK_FD(ptr_hook, fd), read_fds); - if (HOOK_FD(ptr_hook, fd) > max_fd) - max_fd = HOOK_FD(ptr_hook, fd); - } - if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_WRITE) - { - FD_SET (HOOK_FD(ptr_hook, fd), write_fds); - if (HOOK_FD(ptr_hook, fd) > max_fd) - max_fd = HOOK_FD(ptr_hook, fd); - } - if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_EXCEPTION) - { - FD_SET (HOOK_FD(ptr_hook, fd), exception_fds); - if (HOOK_FD(ptr_hook, fd) > max_fd) - max_fd = HOOK_FD(ptr_hook, fd); + if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_READ) + { + FD_SET (HOOK_FD(ptr_hook, fd), read_fds); + if (HOOK_FD(ptr_hook, fd) > max_fd) + max_fd = HOOK_FD(ptr_hook, fd); + } + if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_WRITE) + { + FD_SET (HOOK_FD(ptr_hook, fd), write_fds); + if (HOOK_FD(ptr_hook, fd) > max_fd) + max_fd = HOOK_FD(ptr_hook, fd); + } + if (HOOK_FD(ptr_hook, flags) & HOOK_FD_FLAG_EXCEPTION) + { + FD_SET (HOOK_FD(ptr_hook, fd), exception_fds); + if (HOOK_FD(ptr_hook, fd) > max_fd) + max_fd = HOOK_FD(ptr_hook, fd); + } } } }