From 9c0bf42077d3922d6e004b57385695ae67088828 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 1 Apr 2012 18:56:22 +0900 Subject: [PATCH] Fixing truncation handling in SEXP_WRITE_STRING_OP. --- vm.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/vm.c b/vm.c index 026d0393..e142f0d1 100644 --- a/vm.c +++ b/vm.c @@ -1807,22 +1807,28 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) { if (sexp_port_stream(_ARG3)) { i = fwrite(sexp_bytes_data(tmp1), 1, sexp_unbox_fixnum(_ARG2), sexp_port_stream(_ARG3)); #if SEXP_USE_GREEN_THREADS - if ((i < sexp_unbox_fixnum(_ARG2)) && ferror(sexp_port_stream(_ARG3)) - && (errno == EAGAIN) - && sexp_applicablep(sexp_global(ctx, SEXP_G_THREADS_BLOCKER))) { + if ((i < sexp_unbox_fixnum(_ARG2)) + && ferror(sexp_port_stream(_ARG3)) + && (errno == EAGAIN)) { + clearerr(sexp_port_stream(_ARG3)); + /* modify stack in-place so we continue where we left off next time */ if (i > 0) { - /* modify stack in-place so we continue where we left off next time */ - _ARG1 = sexp_substring(ctx, _ARG1, SEXP_ZERO, sexp_make_fixnum(i)); + if (sexp_stringp(_ARG1)) + _ARG1 = sexp_substring(ctx, _ARG1, sexp_make_fixnum(i), SEXP_FALSE); + else + _ARG1 = sexp_subbytes(ctx, _ARG1, sexp_make_fixnum(i), SEXP_FALSE); _ARG2 = sexp_make_fixnum(sexp_unbox_fixnum(_ARG2) - i); } - clearerr(sexp_port_stream(_ARG3)); - sexp_apply1(ctx, sexp_global(ctx, SEXP_G_THREADS_BLOCKER), _ARG3); - fuel = 0; + /* yield if threads are enabled */ + if (sexp_applicablep(sexp_global(ctx, SEXP_G_THREADS_BLOCKER))) { + sexp_apply1(ctx, sexp_global(ctx, SEXP_G_THREADS_BLOCKER), _ARG3); + fuel = 0; + } ip--; /* try again */ goto loop; } #endif - } else { /* not a stream-backed string */ + } else { /* not a stream-backed string, won't block */ if (sexp_bytes_length(tmp1) != sexp_unbox_fixnum(_ARG2)) tmp1 = sexp_subbytes(ctx, tmp1, SEXP_ZERO, _ARG2); sexp_write_string(ctx, sexp_bytes_data(tmp1), _ARG3);