From d96457e13269330fabb477a9bf130dfd2e30b36a Mon Sep 17 00:00:00 2001 From: kenping Date: Thu, 21 Apr 2022 15:59:28 +0800 Subject: [PATCH] listener: Preserve last error in evconnlistener_new_bind() before close In function evconnlistener_new_bind() after go to "err:", The evutil_closesocket() would clear the error code( I found this under Windows ). User can not use EVUTIL_SOCKET_ERROR() to get the evconnlistener_new_bind()'s failing error. I add a err_code variable to store and restore the last error code. v2: rebased by azat to make the patch simpler --- listener.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/listener.c b/listener.c index 125c7286..fc7c2c58 100644 --- a/listener.c +++ b/listener.c @@ -275,8 +275,13 @@ evconnlistener_new_bind(struct event_base *base, evconnlistener_cb cb, return listener; err: - evutil_closesocket(fd); - return NULL; + { + int saved_errno = EVUTIL_SOCKET_ERROR(); + evutil_closesocket(fd); + if (saved_errno) + EVUTIL_SET_SOCKET_ERROR(saved_errno); + return NULL; + } } void