From: Shuanglei Tao <tsl0922@gmail.com>
Date: Sun, 14 Mar 2021 03:57:57 +0000 (+0800)
Subject: protocol: fix -Wstringop-overflow compile warning
X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=2480810eb393bb935616ad4a4f422c5d0504ebb1;p=ttyd.git

protocol: fix -Wstringop-overflow compile warning
---

diff --git a/src/protocol.c b/src/protocol.c
index d34acf3..e63994d 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -127,14 +127,16 @@ static bool spawn_process(struct pss_tty *pss, uint16_t columns, uint16_t rows)
 
 static void wsi_output(struct lws *wsi, pty_buf_t *buf) {
   if (buf == NULL) return;
-  char *wbuf = xmalloc(LWS_PRE + 1 + buf->len);
-  memcpy(wbuf + LWS_PRE + 1, buf->base, buf->len);
-  wbuf[LWS_PRE] = OUTPUT;
+  char message[LWS_PRE + 1 + buf->len];
+  char *ptr= &message[LWS_PRE];
+
+  *ptr = OUTPUT;
+  memcpy(ptr + 1, buf->base, buf->len);
   size_t n = buf->len + 1;
-  if (lws_write(wsi, (unsigned char *) wbuf + LWS_PRE, n, LWS_WRITE_BINARY) < n) {
+
+  if (lws_write(wsi, (unsigned char *) ptr, n, LWS_WRITE_BINARY) < n) {
     lwsl_err("write OUTPUT to WS\n");
   }
-  free(wbuf);
 }
 
 int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in,
diff --git a/src/pty.c b/src/pty.c
index 03f6557..e09ec30 100644
--- a/src/pty.c
+++ b/src/pty.c
@@ -36,7 +36,7 @@ static void close_cb(uv_handle_t *handle) {
   free(handle);
 }
 
-pty_buf_t *pty_buf_init(char *base, ssize_t len) {
+pty_buf_t *pty_buf_init(char *base, size_t len) {
   pty_buf_t *buf = xmalloc(sizeof(pty_buf_t));
   buf->base = xmalloc(len);
   memcpy(buf->base, base, len);
@@ -58,7 +58,7 @@ static void read_cb(uv_stream_t *stream, ssize_t n, const uv_buf_t *buf) {
     io->read_cb(io->ctx, NULL, true);
     goto done;
   }
-  io->read_cb(io->ctx, pty_buf_init(buf->base, n), false);
+  io->read_cb(io->ctx, pty_buf_init(buf->base, (size_t) n), false);
 
 done:
   free(buf->base);
diff --git a/src/pty.h b/src/pty.h
index 6b2d738..07f9fc1 100644
--- a/src/pty.h
+++ b/src/pty.h
@@ -18,7 +18,7 @@ bool conpty_init();
 
 typedef struct {
   char *base;
-  ssize_t len;
+  size_t len;
 } pty_buf_t;
 
 typedef void (*pty_read_cb)(void *, pty_buf_t *, bool);
@@ -58,7 +58,7 @@ struct pty_process_ {
   void *ctx;
 };
 
-pty_buf_t *pty_buf_init(char *base, ssize_t len);
+pty_buf_t *pty_buf_init(char *base, size_t len);
 void pty_buf_free(pty_buf_t *buf);
 pty_process *process_init(void *ctx, uv_loop_t *loop, char **argv);
 bool process_running(pty_process *process);