[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[COMMITTED 2/2] poked: Fix `chan_send` to correctly send data over outpu
From: |
Mohammad-Reza Nabipoor |
Subject: |
[COMMITTED 2/2] poked: Fix `chan_send` to correctly send data over output chans |
Date: |
Fri, 11 Mar 2022 02:14:55 +0330 |
2022-03-11 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* poked/poked.pk (chan_send): Remove length prefix. Adding length
prefix is the responsibility of usock library.
* poked/poked.c (poked_buf_send): Fix to not expect length prefix
in Poke array.
* poked/usock.c (usock_out): Fix the prefix and data length.
---
ChangeLog | 8 ++++++++
poked/poked.c | 26 +++++++++-----------------
poked/poked.pk | 7 ++-----
poked/usock.c | 4 ++--
4 files changed, 21 insertions(+), 24 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 273eb503..db2ba25e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-03-11 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * poked/poked.pk (chan_send): Remove length prefix. Adding length
+ prefix is the responsibility of usock library.
+ * poked/poked.c (poked_buf_send): Fix to not expect length prefix
+ in Poke array.
+ * poked/usock.c (usock_out): Fix the prefix and data length.
+
2022-03-11 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* poked/poked.pk (__Disasm): Change type of `adr` to offset.
diff --git a/poked/poked.c b/poked/poked.c
index fcf08e1e..cd6463da 100644
--- a/poked/poked.c
+++ b/poked/poked.c
@@ -83,24 +83,21 @@ static void
poked_buf_send (void)
{
pk_val arr = pk_decl_val (pkc, "__chan_send_buf");
- pk_val nelem = pk_uint_value (pk_array_nelem (arr));
+ uint16_t nelem = (uint16_t)pk_uint_value (pk_array_nelem (arr));
uint8_t chan = pk_uint_value (pk_decl_val (pkc, "__chan_send_chan")) & 0x7f;
uint8_t lbuf[2];
- size_t memlen;
uint8_t *mem;
pk_val exc;
- assert (nelem > 2);
+ lbuf[0] = nelem;
+ lbuf[1] = nelem >> 8;
- lbuf[0] = pk_uint_value (pk_array_elem_value (arr, 0));
- lbuf[1] = pk_uint_value (pk_array_elem_value (arr, 1));
-
- memlen = (size_t)lbuf[1] << 8 | lbuf[0];
- mem = malloc (memlen);
- assert (mem);
- for (size_t i = 2; i < nelem; ++i)
- mem[i - 2] = pk_uint_value (pk_array_elem_value (arr, i));
- usock_out (srv, /*no kind*/ 0, chan, mem, memlen);
+ mem = malloc (nelem);
+ if (mem == NULL)
+ err (1, "malloc() failed");
+ for (uint16_t i = 0; i < nelem; ++i)
+ mem[i] = pk_uint_value (pk_array_elem_value (arr, i));
+ usock_out (srv, /*no kind*/ 0, chan, mem, nelem);
free (mem);
(void)pk_call (pkc, pk_decl_val (pkc, "__chan_send_reset"), NULL, &exc, 0);
@@ -227,11 +224,6 @@ poked_restart:
goto eol;
}
-#if 0
- // ?!
- if (!ok)
- continue;
-#endif
if (pk_int_value (pk_decl_val (pkc, "__poked_restart_p")))
{
diff --git a/poked/poked.pk b/poked/poked.pk
index 89459af0..0fe6daf3 100644
--- a/poked/poked.pk
+++ b/poked/poked.pk
@@ -61,13 +61,10 @@ fun __chan_send_reset = void:
fun chan_send = (uint<7> chan, byte[] data) void:
{
- if (data'length > 0xffff)
- return;
-
- var len = data'length as uint16;
+ assert (0 < data'length && data'length <= 0xffff);
__chan_send_chan = chan;
- __chan_send_buf = [len as byte, (len .>> 8) as byte] + data;
+ __chan_send_buf = data;
__chan_send_p = 1;
}
diff --git a/poked/usock.c b/poked/usock.c
index 4ba24dc6..1b70f6ea 100644
--- a/poked/usock.c
+++ b/poked/usock.c
@@ -725,10 +725,10 @@ usock_out (struct usock *u, uint32_t kind, uint8_t chan,
const char *data,
assert (kind <= 0x7f); // TODO implement ULEB128
- uint16_t len16 = (data[len - 1] == '\0' ? len : len + 1) + (kind != 0);
+ uint16_t len16 = (len & 0xffff) + (data[len - 1] != '\0') + (kind != 0);
uint8_t prefix[/*len*/ 2 + /*kind*/ 5] = { len16, len16 >> 8, kind & 0x7f };
struct usock_buf *buf = usock_buf_new_prefix (prefix, 2 + (kind != 0), data,
- len16 - (kind != 0));
+ len & 0xffff);
buf->len = 0; // cursor for how much data has been written
buf->tag = chan;
--
2.35.1