From: Shuanglei Tao Date: Sun, 27 Dec 2020 05:16:29 +0000 (+0800) Subject: protocol: fix incorrect uv_close usage X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=5650606674bb98d5f65aebf84f834a9c0913287c;p=ttyd.git protocol: fix incorrect uv_close usage --- diff --git a/src/protocol.c b/src/protocol.c index 1be0a4e..038b4aa 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -87,9 +87,14 @@ static bool check_host_origin(struct lws *wsi) { return len > 0 && strcasecmp(buf, host_buf) == 0; } +static void close_cb(uv_handle_t *handle) { + struct pty_proc *proc = container_of((uv_pipe_t *)handle, struct pty_proc, pipe); + free(proc); +} + static void pty_proc_free(struct pty_proc *proc) { uv_read_stop((uv_stream_t *)&proc->pipe); - uv_close((uv_handle_t *)&proc->pipe, NULL); + uv_close((uv_handle_t *)&proc->pipe, close_cb); close(proc->pty); @@ -101,8 +106,6 @@ static void pty_proc_free(struct pty_proc *proc) { for (int i = 0; i < proc->argc; i++) { free(proc->args[i]); } - - free(proc); } static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { diff --git a/src/utils.h b/src/utils.h index 935bec4..2060ac9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,6 +1,12 @@ #ifndef TTYD_UTIL_H #define TTYD_UTIL_H +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type, member)); \ + }) + // malloc with NULL check void *xmalloc(size_t size);