X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/b0f06652c4d78a8d14a47a5cacbc9268b0fe80d9..96f4e2269315e1961a4e9db45c14fef0dbfeee02:/perlio.c diff --git a/perlio.c b/perlio.c index 288159c..30e3e6c 100644 --- a/perlio.c +++ b/perlio.c @@ -478,13 +478,9 @@ PerlIO_debug(const char *fmt, ...) const char * const s = CopFILE(PL_curcop); /* Use fixed buffer as sv_catpvf etc. needs SVs */ char buffer[1024]; - const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop)); -# ifdef USE_VSNPRINTF - const STRLEN len2 = vsnprintf(buffer+len, sizeof(buffer) - len, fmt, ap); -# else - const STRLEN len2 = vsprintf(buffer+len, fmt, ap); -# endif /* USE_VSNPRINTF */ - PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2); + const STRLEN len1 = my_snprintf(buffer, sizeof(buffer), "%.40s:%" IVdf " ", s ? s : "(none)", (IV) CopLINE(PL_curcop)); + const STRLEN len2 = my_vsnprintf(buffer + len1, sizeof(buffer) - len1, fmt, ap); + PerlLIO_write(PL_perlio_debug_fd, buffer, len1 + len2); #else const char *s = CopFILE(PL_curcop); STRLEN len; @@ -1412,32 +1408,6 @@ Perl_PerlIO_fileno(pTHX_ PerlIO *f) Perl_PerlIO_or_Base(f, Fileno, fileno, -1, (aTHX_ f)); } -static const char * -PerlIO_context_layers(pTHX_ const char *mode) -{ - dVAR; - const char *type = NULL; - /* - * Need to supply default layer info from open.pm - */ - if (PL_curcop) { - SV * const layers = PL_curcop->cop_io; - if (layers) { - STRLEN len; - type = SvPV_const(layers, len); - if (type && mode[0] != 'r') { - /* - * Skip to write part - */ - const char * const s = strchr(type, 0); - if (s && (STRLEN)(s - type) < len) { - type = s + 1; - } - } - } - } - return type; -} static PerlIO_funcs * PerlIO_layer_from_ref(pTHX_ SV *sv) @@ -1461,8 +1431,9 @@ PerlIO_layer_from_ref(pTHX_ SV *sv) return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Code"), 0); case SVt_PVGV: return PerlIO_find_layer(aTHX_ STR_WITH_LEN("Glob"), 0); + default: + return NULL; } - return NULL; } PerlIO_list_t * @@ -1495,7 +1466,7 @@ PerlIO_resolve_layers(pTHX_ const char *layers, } } if (!layers || !*layers) - layers = PerlIO_context_layers(aTHX_ mode); + layers = Perl_PerlIO_context_layers(aTHX_ mode); if (layers && *layers) { PerlIO_list_t *av; if (incdef) { @@ -1532,7 +1503,7 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd, if (!f && narg == 1 && *args == &PL_sv_undef) { if ((f = PerlIO_tmpfile())) { if (!layers || !*layers) - layers = PerlIO_context_layers(aTHX_ mode); + layers = Perl_PerlIO_context_layers(aTHX_ mode); if (layers && *layers) PerlIO_apply_layers(aTHX_ f, mode, layers); } @@ -5056,6 +5027,36 @@ PerlIO_tmpfile(void) * Now some functions in terms of above which may be needed even if we are * not in true PerlIO mode */ +const char * +Perl_PerlIO_context_layers(pTHX_ const char *mode) +{ + dVAR; + const char *type = NULL; + /* + * Need to supply default layer info from open.pm + */ + if (PL_curcop && PL_curcop->cop_hints & HINT_LEXICAL_IO) { + SV * const layers + = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 0, + "open", 4, 0, 0); + assert(layers); + if (SvOK(layers)) { + STRLEN len; + type = SvPV_const(layers, len); + if (type && mode && mode[0] != 'r') { + /* + * Skip to write part, which is separated by a '\0' + */ + STRLEN read_len = strlen(type); + if (read_len < len) { + type += read_len + 1; + } + } + } + } + return type; +} + #ifndef HAS_FSETPOS #undef PerlIO_setpos @@ -5146,20 +5147,15 @@ vfprintf(FILE *fd, char *pat, char *args) int PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap) { - dVAR; -#ifdef USE_VSNPRINTF - const int val = vsnprintf(s, n > 0 ? n : 0, fmt, ap); -#else - const int val = vsprintf(s, fmt, ap); -#endif /* #ifdef USE_VSNPRINTF */ - if (n >= 0) { - if (strlen(s) >= (STRLEN) n) { - dTHX; - (void) PerlIO_puts(Perl_error_log, - "panic: sprintf overflow - memory corrupted!\n"); - my_exit(1); - } + dTHX; + const int val = my_vsnprintf(s, n > 0 ? n : 0, fmt, ap); + PERL_UNUSED_CONTEXT; + +#ifndef PERL_MY_VSNPRINTF_GUARDED + if (val < 0 || (n > 0 ? val >= n : 0)) { + Perl_croak(aTHX_ "panic: my_vsnprintf overflow in PerlIO_vsprintf\n"); } +#endif return val; } #endif