diff --git a/doio.c b/doio.c index 24bc91d937dd..b849f0626ec6 100644 --- a/doio.c +++ b/doio.c @@ -2215,14 +2215,13 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp) STRLEN len; /* Do this first to trigger any overloading. */ const char *tmps = SvPV_const(sv, len); - U8 *tmpbuf = NULL; bool happy = TRUE; if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */ if (!SvUTF8(sv)) { /* Convert to utf8 if necessary */ /* We don't modify the original scalar. */ - tmpbuf = bytes_to_utf8((const U8*) tmps, &len); - tmps = (char *) tmpbuf; + tmps = (char *) bytes_to_utf8((const U8*) tmps, &len); + SAVEFREEPV(tmps); } else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) { (void) check_utf8_print((const U8*) tmps, len); @@ -2230,20 +2229,9 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp) } /* else stream isn't utf8 */ else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to convert to bytes */ - STRLEN tmplen = len; - bool utf8 = TRUE; - U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8); - if (!utf8) { - - /* Here, succeeded in downgrading from utf8. Set up to below - * output the converted value */ - tmpbuf = result; - tmps = (char *) tmpbuf; - len = tmplen; - } - else { /* Non-utf8 output stream, but string only representable in - utf8 */ - assert((char *)result == tmps); + if (! utf8_to_bytes_temp_pv((const U8**) &tmps, &len)) { + /* Non-utf8 output stream, but string only representable in + utf8 */ Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8), "Wide character in %s", PL_op ? OP_DESC(PL_op) : "print" @@ -2261,7 +2249,6 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp) * io the write failure can be delayed until the flush/close. --jhi */ if (len && (PerlIO_write(fp,tmps,len) == 0)) happy = FALSE; - Safefree(tmpbuf); return happy ? !PerlIO_error(fp) : FALSE; } }