This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_sv_vcatpvfn_flags: eliminate utf8buf[] var
authorDavid Mitchell <davem@iabyn.com>
Thu, 18 May 2017 09:45:56 +0000 (10:45 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 7 Jun 2017 08:11:02 +0000 (09:11 +0100)
%c for a >255 char generates its utf8 byte representation and stores it in
thiis temporarly buffer:

    U8 utf8buf[UTF8_MAXBYTES+1]

But we already have another temporary buffer, ebuf, for creating floating
point strings, which is big enough. So use that instead.

sv.c

diff --git a/sv.c b/sv.c
index 3401854..c1ec649 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -11927,8 +11927,6 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
        const char *fmtstart;         /* start of current format (the '%') */
        char c           = 0;         /* current character read from format */
 
-       U8 utf8buf[UTF8_MAXBYTES+1];  /* temp buf for %c */
-
 
        /* echo everything up to the next format specification */
        for (q = p; q < patend && *q != '%'; ++q) ;
@@ -12324,9 +12322,11 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
            uv = (args) ? va_arg(*args, int) : SvIV_nomg(argsv);
            if ((uv > 255 ||
                 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
-               && !IN_BYTES) {
-               eptr = (char*)utf8buf;
-               elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
+               && !IN_BYTES)
+            {
+                assert(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
+               eptr = ebuf;
+               elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
                is_utf8 = TRUE;
            }
            else {