[Libwebsockets] [PATCH 2/3] libev: clean cast abuse

Alejandro Mery amery at geeks.cl
Sun Dec 7 03:36:12 CET 2014


assuming that you can cast to the first field of an struct is very
risky

Signed-off-by: Alejandro Mery <amery at geeks.cl>
---
 lib/libev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/libev.c b/lib/libev.c
index 04a4b5b..eff7197 100644
--- a/lib/libev.c
+++ b/lib/libev.c
@@ -33,7 +33,7 @@ static void
 libwebsocket_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
 {
 	struct libwebsocket_pollfd eventfd;
-	struct lws_io_watcher *lws_io = (struct lws_io_watcher *)watcher;
+	struct lws_io_watcher *lws_io = container_of(watcher, struct lws_io_watcher, watcher);
 	struct libwebsocket_context *context = lws_io->context;
 
 	if (revents & EV_ERROR)
@@ -65,8 +65,8 @@ libwebsocket_initloop(
 	int status = 0;
 	int backend;
 	const char * backend_name;
-	struct ev_io *w_accept = (ev_io *)&context->w_accept;
-	struct ev_signal *w_sigint = (ev_signal *)&context->w_sigint;
+	struct ev_io *w_accept = &context->w_accept.watcher;
+	struct ev_signal *w_sigint = &context->w_sigint.watcher;
 
 	if (!loop)
 		loop = ev_default_loop(0);
-- 
2.2.0




More information about the Libwebsockets mailing list