diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 29c50338..ce2bcce0 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -25,11 +25,12 @@ type template_args_short_read struct { } func template_short_read(b *buffer, args template_args_short_read) error { - b.printf("short_read_%s:\nif (a_%s.limit.ptr_to_len) {\nstatus = %sSUSPENSION_LIMITED_READ;\n} else if (a_%s.buf->closed) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n} else {\nstatus = %sSUSPENSION_SHORT_READ;\n}\ngoto suspend;\n", + b.printf("short_read_%s:\nif (a_%s.limit.ptr_to_len) {\nstatus = %sSUSPENSION_LIMITED_READ;\n} else if (a_%s.buf && a_%s.buf->closed) {\nstatus = %sERROR_UNEXPECTED_EOF;\ngoto exit;\n} else {\nstatus = %sSUSPENSION_SHORT_READ;\n}\ngoto suspend;\n", args.name, args.name, args.PKGPREFIX, args.name, + args.name, args.PKGPREFIX, args.PKGPREFIX, ) diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 64c07f1a..9bfd46a4 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -159,14 +159,14 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil } if isInDst(g.tm, n, t.KeySlice, 2) { - b.printf("puffs_base_make_slice_u8(%sdst.buf->ptr,", aPrefix) + b.printf("puffs_base_make_slice_u8(%sdst.buf ? %sdst.buf->ptr : NULL,", aPrefix, aPrefix) for _, o := range n.Args() { if err := g.writeExpr(b, o.Arg().Value(), rp, parenthesesOptional, depth); err != nil { return err } b.writes(".ptr,") } - b.printf("%swptr_dst)", bPrefix) + b.printf("%sdst.buf ? %swptr_dst : NULL)", aPrefix, bPrefix) return nil } if isThatMethod(g.tm, n, t.KeyLength, 0) { diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index fca59e8c..03fd8147 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -356,7 +356,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { // TODO: is ptr_to_len the right check? b.printf("if (%ssrc.limit.ptr_to_len) {", aPrefix) b.printf("status = %sSUSPENSION_LIMITED_READ;", g.PKGPREFIX) - b.printf("} else if (%ssrc.buf->closed) {", aPrefix) + b.printf("} else if (%ssrc.buf && %ssrc.buf->closed) {", aPrefix, aPrefix) b.printf("status = %sERROR_UNEXPECTED_EOF; goto exit;", g.PKGPREFIX) b.printf("} else { status = %sSUSPENSION_SHORT_READ; } goto suspend;\n", g.PKGPREFIX) @@ -365,7 +365,8 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { } else if isInDst(g.tm, n, t.KeyWrite, 1) { // TODO: don't assume that the argument is "this.stack[s:]". - b.printf("if (%sdst.buf->closed) { status = %sERROR_CLOSED_FOR_WRITES;", aPrefix, g.PKGPREFIX) + b.printf("if (%sdst.buf && %sdst.buf->closed) { status = %sERROR_CLOSED_FOR_WRITES;", + aPrefix, aPrefix, g.PKGPREFIX) b.writes("goto exit;") b.writes("}\n") b.printf("if ((%swend_dst - %swptr_dst) < (sizeof(self->private_impl.f_stack) - v_s)) {", @@ -472,6 +473,10 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { const wName = "dst" b.printf("size_t %s%d = (size_t)(%s >> 32);", tPrefix, temp0, scratchName) // TODO: it's not a BAD_ARGUMENT if we can copy from the sliding window. + // + // TODO: %s%s.buf->ptr might be a NULL pointer dereference. In any + // case, we need to compare the (resumed) distance against a mark, not + // against the beginning of the dst buffer. b.printf("if (PUFFS_UNLIKELY((%s%d == 0) || (%s%d > (%swptr_%s - %s%s.buf->ptr)))) { "+ "status = %sERROR_BAD_ARGUMENT; goto exit; }\n", tPrefix, temp0, tPrefix, temp0, bPrefix, wName, aPrefix, wName, g.PKGPREFIX) diff --git a/cmd/puffs-c/internal/cgen/templates.h b/cmd/puffs-c/internal/cgen/templates.h index 983bd896..3d50b5bf 100644 --- a/cmd/puffs-c/internal/cgen/templates.h +++ b/cmd/puffs-c/internal/cgen/templates.h @@ -12,7 +12,7 @@ short_read_qnameq: // TODO: is ptr_to_len the right check? if (a_qnameq.limit.ptr_to_len) { status = qPKGPREFIXqSUSPENSION_LIMITED_READ; - } else if (a_qnameq.buf->closed) { + } else if (a_qnameq.buf && a_qnameq.buf->closed) { status = qPKGPREFIXqERROR_UNEXPECTED_EOF; goto exit; } else { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 2d59bc1a..fab67dca 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -671,8 +671,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, v_z = t_0; if (v_z > 0) { v_m1 = ((puffs_base_buf1_mark){.ptr = b_wptr_dst}); - v_written = puffs_base_make_slice_u8(a_dst.buf->ptr, v_m0.ptr, v_m1.ptr, - b_wptr_dst); + v_written = + puffs_base_make_slice_u8(a_dst.buf ? a_dst.buf->ptr : NULL, v_m0.ptr, + v_m1.ptr, a_dst.buf ? b_wptr_dst : NULL); if (((uint64_t)(v_written.len)) >= 32768) { v_written = puffs_base_slice_u8_suffix(v_written, 32768); puffs_base_slice_u8_copy_from( @@ -877,7 +878,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_FLATE_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_FLATE_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -1025,7 +1026,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_FLATE_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_FLATE_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -1360,7 +1361,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_FLATE_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_FLATE_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -1671,7 +1672,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_FLATE_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_FLATE_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -2122,7 +2123,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_FLATE_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_FLATE_ERROR_UNEXPECTED_EOF; goto exit; } else { diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 365e0767..1b8952de 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -694,7 +694,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -776,7 +776,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -885,7 +885,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -959,7 +959,7 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, b_rptr_src = b_rend_src; if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -997,7 +997,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -1153,7 +1153,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else { @@ -1311,7 +1311,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, self->private_impl.f_stack[4095] = ((uint8_t)(v_c)); } PUFFS_COROUTINE_SUSPENSION_POINT(3); - if (a_dst.buf->closed) { + if (a_dst.buf && a_dst.buf->closed) { status = PUFFS_GIF_ERROR_CLOSED_FOR_WRITES; goto exit; } @@ -1387,7 +1387,7 @@ exit: short_read_src: if (a_src.limit.ptr_to_len) { status = PUFFS_GIF_SUSPENSION_LIMITED_READ; - } else if (a_src.buf->closed) { + } else if (a_src.buf && a_src.buf->closed) { status = PUFFS_GIF_ERROR_UNEXPECTED_EOF; goto exit; } else {