Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do_print: Use new utf8_to_bytes_temp_pv() #22812

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions doio.c
Original file line number Diff line number Diff line change
@@ -2214,36 +2214,30 @@ Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
else {
STRLEN len;
/* Do this first to trigger any overloading. */
const char *tmps = SvPV_const(sv, len);
U8 *tmpbuf = NULL;
tonycoz marked this conversation as resolved.
Show resolved Hide resolved
const U8 *tmps = (const U8 *) SvPV_const(sv, len);

/* If 'tmps' doesn't need converting, this will remain NULL and
* Safefree(free_me) will do nothing; Otherwise it points to the newly
* allocated memory that tmps will also be changed to point to, so
* Safefree(free_me) will free it. This saves having to have extra
* logic. */
void *free_me = 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;
/* This doesn't modify the original scalar. */
tmps = bytes_to_utf8_free_me(tmps, &len, &free_me);
}
else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
(void) check_utf8_print((const U8*) tmps, len);
(void) check_utf8_print(tmps, len);
}
} /* 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_new_pv(&tmps, &len, &free_me)) {
/* 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 +2255,7 @@ 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);
Safefree(free_me);
return happy ? !PerlIO_error(fp) : FALSE;
}
}