diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 521e03a4..7d28521a 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -877,7 +877,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { temp := g.perFunc.tempW g.perFunc.tempW++ - // TODO: delete this hack that only matches "in.src.read_u8?()". + // TODO: delete these hacks that only matches "in.src.read_u8?()" etc. if isInSrcReadU8(g.tm, n) { // TODO: suspend coroutine state. // @@ -896,8 +896,22 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { } g.printf(" %s%d = %ssrc->ptr[%ssrc->ri++];\n", tPrefix, temp, aPrefix, aPrefix) - // TODO: delete this hack that only matches "in.dst.write_u8?()". - } else if isInDstWriteU8(g.tm, n) { + } else if isInDst(g.tm, n, t.KeyWrite) { + // TODO: suspend coroutine state. + // + // TODO: don't assume that the argument is "this.stack[s:]". + g.printf("if ((%sdst->len - %sdst->wi) < (sizeof(self->f_stack) - v_s)) {", aPrefix, aPrefix) + g.printf("status = puffs_%s_status_short_write;", g.pkgName) + if g.perFunc.public && g.perFunc.suspendible { + g.writes("goto cleanup0;") + } else { + g.writes("return status;") + } + g.writes("}\n") + g.printf("memmove(a_dst->ptr + a_dst->wi, self->f_stack + v_s, sizeof(self->f_stack) - v_s);\n") + g.printf("a_dst->wi += sizeof(self->f_stack) - v_s;\n") + + } else if isInDst(g.tm, n, t.KeyWriteU8) { // TODO: suspend coroutine state. g.printf("if (%sdst->wi >= %sdst->len) { status = puffs_%s_status_short_write;", aPrefix, aPrefix, g.pkgName) @@ -913,7 +927,6 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { return err } g.writes(";\n") - return nil } else { // TODO: fix this. @@ -1057,13 +1070,13 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { return n.ID0() == 0 && n.ID1().Key() == t.KeyIn } -func isInDstWriteU8(tm *t.Map, n *a.Expr) bool { +func isInDst(tm *t.Map, n *a.Expr, methodName t.Key) bool { // TODO: check that n.Args() is "(x:bar)". if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 { return false } n = n.LHS().Expr() - if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 { + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName { return false } n = n.LHS().Expr() diff --git a/gen/c/gif.c b/gen/c/gif.c index d3910f8c..dd9d95f7 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -290,6 +290,13 @@ label_0_continue:; if ((v_code == v_save_code) && v_use_save_code) { self->f_stack[4095] = ((uint8_t)(v_c)); } + if ((a_dst->len - a_dst->wi) < (sizeof(self->f_stack) - v_s)) { + status = puffs_gif_status_short_write; + goto cleanup0; + } + memmove(a_dst->ptr + a_dst->wi, self->f_stack + v_s, + sizeof(self->f_stack) - v_s); + a_dst->wi += sizeof(self->f_stack) - v_s; if (v_use_save_code) { self->f_suffixes[v_save_code] = self->f_stack[4095]; self->f_prefixes[v_save_code] = ((uint16_t)(v_prev_code)); diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 04d97a21..65ae1606 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -598,9 +598,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, // No-op. case t.KeyOpenParen: - // TODO: delete this hack that only matches "in.src.read_u8?()" and - // "in.dst.write_u8?(x:bar)". - if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) { + // TODO: delete this hack that only matches "in.src.read_u8?()" etc. + if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) { for _, o := range n.Args() { // TODO: check that the arg range at the caller and the // signature are compatible. diff --git a/lang/check/type.go b/lang/check/type.go index 7c77fbfa..4688bc44 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -326,9 +326,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyOpenParen: // n is a function call. - // TODO: delete this hack that only matches "in.src.read_u8?()" and - // "in.dst.write_u8?(x:bar)". - if isInSrcReadU8(q.tm, n) || isInDstWriteU8(q.tm, n) { + // TODO: delete this hack that only matches "in.src.read_u8?()" etc. + if isInSrcReadU8(q.tm, n) || isInDst(q.tm, n, t.KeyWrite) || isInDst(q.tm, n, t.KeyWriteU8) { if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { return err } @@ -406,13 +405,13 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool { return n.ID0() == 0 && n.ID1().Key() == t.KeyIn } -func isInDstWriteU8(tm *t.Map, n *a.Expr) bool { +func isInDst(tm *t.Map, n *a.Expr, methodName t.Key) bool { // TODO: check that n.Args() is "(x:bar)". if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 1 { return false } n = n.LHS().Expr() - if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyWriteU8 { + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName { return false } n = n.LHS().Expr() diff --git a/lang/token/list.go b/lang/token/list.go index 15aa2f39..be643a56 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -241,6 +241,7 @@ const ( KeyReadU32LE = Key(IDReadU32LE >> KeyShift) KeyReadU64BE = Key(IDReadU64BE >> KeyShift) KeyReadU64LE = Key(IDReadU64LE >> KeyShift) + KeyWrite = Key(IDWrite >> KeyShift) KeyWriteU8 = Key(IDWriteU8 >> KeyShift) KeyWriteU16BE = Key(IDWriteU16BE >> KeyShift) KeyWriteU16LE = Key(IDWriteU16LE >> KeyShift) @@ -392,6 +393,7 @@ const ( IDReadU32LE = ID(0xB5<wi == 0) { + return msg; + } + size_t base = i - (i & 15); + for (int j = -3 * 16; j <= +3 * 16; j += 16) { + if ((j < 0) && (base < (size_t)(-j))) { + continue; + } + size_t b = base + j; + if (b >= buf->wi) { + break; + } + size_t n = buf->wi - b; + INCR_FAIL(msg, " %06zx:", b); + for (size_t k = 0; k < 16; k++) { + if (k % 2 == 0) { + INCR_FAIL(msg, " "); + } + if (k < n) { + INCR_FAIL(msg, "%02x", buf->ptr[b + k]); + } else { + INCR_FAIL(msg, " "); + } + } + INCR_FAIL(msg, " "); + for (size_t k = 0; k < 16; k++) { + char c = ' '; + if (k < n) { + c = buf->ptr[b + k]; + if ((c < 0x20) || (0x7F < c)) { + c = '.'; + } + } + INCR_FAIL(msg, "%c", c); + } + INCR_FAIL(msg, "\n"); + if (n < 16) { + break; + } + } + return msg; +} + +bool buf1s_equal(puffs_base_buf1* got, puffs_base_buf1* want) { + if (!got || !want) { + FAIL("bufs1_equal: NULL argument"); + return false; + } + char* msg = fail_msg; + size_t i = 0; + for (; (i < got->wi) && (i < want->wi); i++) { + if (got->ptr[i] != want->ptr[i]) { + break; + } + } + if (got->wi != want->wi) { + INCR_FAIL(msg, "bufs1_equal: wi: got %zu, want %zu.\n", got->wi, want->wi); + } else if (i < got->wi) { + INCR_FAIL(msg, "bufs1_equal:\n"); + } else { + return true; + } + INCR_FAIL(msg, "contents differ at byte %zu (in hex: 0x%06zx):\n", i, i); + msg = hex_dump(msg, got, i); + INCR_FAIL(msg, "excerpts of got (above) versus want (below):\n"); + msg = hex_dump(msg, want, i); + return false; +} + #endif // PUFFS_BASE_HEADER_H