This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf8.c: Don't use Newx in decoding UTF-8
authorKarl Williamson <khw@cpan.org>
Mon, 6 Mar 2017 19:25:21 +0000 (12:25 -0700)
committerKarl Williamson <khw@cpan.org>
Tue, 7 Mar 2017 17:49:14 +0000 (10:49 -0700)
The bottom level UTF-8 decoding routine can be used during periods when
using Newx is prohibited, as diagnosed by Dave Mitchell for perl #130921
(see that ticket for his explanation).

This particular use of Newx was unnecessary, as it is just large enough
to hold a single character, and that can be done by an automatic
variable on the C stack.  The variable is used only upon rare error
conditions, but its only 14 bytes (15 on EBCDIC).

utf8.c

diff --git a/utf8.c b/utf8.c
index 89c8413..4949bf6 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -1079,6 +1079,8 @@ Perl_utf8n_to_uvchr_error(pTHX_ const U8 *s,
     U8 * adjusted_s0 = (U8 *) s0;
     U8 * adjusted_send = NULL;  /* (Initialized to silence compilers' wrong
                                    warning) */
+    U8 temp_char_buf[UTF8_MAXBYTES + 1]; /* Used to avoid a Newx in this
+                                            routine; see [perl #130921] */
     UV uv_so_far = 0;   /* (Initialized to silence compilers' wrong warning) */
 
     PERL_ARGS_ASSERT_UTF8N_TO_UVCHR_ERROR;
@@ -1245,10 +1247,7 @@ Perl_utf8n_to_uvchr_error(pTHX_ const U8 *s,
                                      I8_TO_NATIVE_UTF8(UTF_CONTINUATION_MARK));
             }
 
-            Newx(adjusted_s0, OFFUNISKIP(min_uv) + 1, U8);
-            SAVEFREEPV((U8 *) adjusted_s0);    /* Needed because we may not get
-                                                  to free it ourselves if
-                                                  warnings are made fatal */
+            adjusted_s0 = temp_char_buf;
             adjusted_send = uvoffuni_to_utf8_flags(adjusted_s0, min_uv, 0);
         }
     }