diff --git a/universal.c b/universal.c index 119117e818a4..14d11d66f908 100644 --- a/universal.c +++ b/universal.c @@ -593,11 +593,21 @@ XS(XS_utf8_upgrade) croak_xs_usage(cv, "sv"); else { SV * const sv = ST(0); - STRLEN RETVAL; + STRLEN RETVAL = 0; dXSTARG; - RETVAL = sv_utf8_upgrade(sv); - XSprePUSH; PUSHi((IV)RETVAL); + XSprePUSH; + if (LIKELY(sv)) { + SvGETMAGIC(sv); + if (LIKELY(SvOK(sv))) { + RETVAL = sv_utf8_upgrade_nomg(sv); + PUSHi( (IV) RETVAL); + } else { + PUSHs(newSV(0)); + } + } else { + PUSHs(newSV(0)); + } } XSRETURN(1); }