This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_sv_vcatpvfn_flags: calc (width - elen) once
authorDavid Mitchell <davem@iabyn.com>
Wed, 10 May 2017 15:58:58 +0000 (16:58 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 7 Jun 2017 08:11:00 +0000 (09:11 +0100)
There's a couple of blocks of code which repeat the expression
(width - elen). Calculate this once at the top. This makes it slightly
easier to audit the code for signed/unsigned wrap etc.

Should be no functional change.

sv.c

diff --git a/sv.c b/sv.c
index f12ad5f..07fa7fe 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -12807,16 +12807,17 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                                     exponent);
 
                 if (elen < width) {
+                    STRLEN gap = (STRLEN)(width - elen);
                     if (left) {
                         /* Pad the back with spaces. */
-                        memset(PL_efloatbuf + elen, ' ', width - elen);
+                        memset(PL_efloatbuf + elen, ' ', gap);
                     }
                     else if (fill == '0') {
                         /* Insert the zeros after the "0x" and the
                          * the potential sign, but before the digits,
                          * otherwise we end up with "0000xH.HHH...",
                          * when we want "0x000H.HHH..."  */
-                        STRLEN nzero = width - elen;
+                        STRLEN nzero = gap;
                         char* zerox = PL_efloatbuf + 2;
                         STRLEN nmove = elen - 2;
                         if (negative || plus) {
@@ -12828,10 +12829,10 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                     }
                     else {
                         /* Move it to the right. */
-                        Move(PL_efloatbuf, PL_efloatbuf + width - elen,
+                        Move(PL_efloatbuf, PL_efloatbuf + gap,
                              elen, char);
                         /* Pad the front with spaces. */
-                        memset(PL_efloatbuf, ' ', width - elen);
+                        memset(PL_efloatbuf, ' ', gap);
                     }
                     elen = width;
                 }
@@ -12841,15 +12842,16 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                 if (elen) {
                     /* Not affecting infnan output: precision, alt, fill. */
                     if (elen < width) {
+                        STRLEN gap = (STRLEN)(width - elen);
                         if (left) {
                             /* Pack the back with spaces. */
-                            memset(PL_efloatbuf + elen, ' ', width - elen);
+                            memset(PL_efloatbuf + elen, ' ', gap);
                         } else {
                             /* Move it to the right. */
-                            Move(PL_efloatbuf, PL_efloatbuf + width - elen,
+                            Move(PL_efloatbuf, PL_efloatbuf + gap,
                                  elen, char);
                             /* Pad the front with spaces. */
-                            memset(PL_efloatbuf, ' ', width - elen);
+                            memset(PL_efloatbuf, ' ', gap);
                         }
                         elen = width;
                     }