This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / pp.c
diff --git a/pp.c b/pp.c
index 7127aa1..82baf1e 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -1,6 +1,7 @@
 /*    pp.c
  *
- *    Copyright (c) 1991-2002, Larry Wall
+ *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+ *    2000, 2001, 2002, 2003, by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -106,6 +107,9 @@ PP(pp_padhv)
     }
     else if (gimme == G_SCALAR) {
        SV* sv = sv_newmortal();
+        if (SvRMAGICAL(TARG) && mg_find(TARG, PERL_MAGIC_tied))
+            Perl_croak(aTHX_ "Can't provide tied hash usage; "
+                       "use keys(%%hash) to test if empty");
        if (HvFILL((HV*)TARG))
            Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
                      (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
@@ -858,6 +862,7 @@ PP(pp_postinc)
     else
        sv_inc(TOPs);
     SvSETMAGIC(TOPs);
+    /* special case for undef: see thread at 2003-03/msg00536.html in archive */
     if (!SvOK(TARG))
        sv_setiv(TARG, 0);
     SETs(TARG);
@@ -1237,7 +1242,7 @@ PP(pp_divide)
                     }
                     RETURN;
                 } /* tried integer divide but it was not an integer result */
-            } /* else (abs(result) < 1.0) or (both UVs in range for NV) */
+            } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
         } /* left wasn't SvIOK */
     } /* right wasn't SvIOK */
 #endif /* PERL_TRY_UV_DIVIDE */
@@ -2458,16 +2463,76 @@ PP(pp_i_divide)
     }
 }
 
+STATIC
+PP(pp_i_modulo_0)
+{
+     /* This is the vanilla old i_modulo. */
+     dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
+     {
+         dPOPTOPiirl;
+         if (!right)
+              DIE(aTHX_ "Illegal modulus zero");
+         SETi( left % right );
+         RETURN;
+     }
+}
+
+#if defined(__GLIBC__) && IVSIZE == 8
+STATIC
+PP(pp_i_modulo_1)
+{
+     /* This is the i_modulo with the workaround for the _moddi3 bug
+      * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
+      * See below for pp_i_modulo. */
+     dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
+     {
+         dPOPTOPiirl;
+         if (!right)
+              DIE(aTHX_ "Illegal modulus zero");
+         SETi( left % PERL_ABS(right) );
+         RETURN;
+     }
+}
+#endif
+
 PP(pp_i_modulo)
 {
-    dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
-    {
-      dPOPTOPiirl;
-      if (!right)
-       DIE(aTHX_ "Illegal modulus zero");
-      SETi( left % right );
-      RETURN;
-    }
+     dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
+     {
+         dPOPTOPiirl;
+         if (!right)
+              DIE(aTHX_ "Illegal modulus zero");
+         /* The assumption is to use hereafter the old vanilla version... */
+         PL_op->op_ppaddr =
+              PL_ppaddr[OP_I_MODULO] =
+                  &Perl_pp_i_modulo_0;
+         /* .. but if we have glibc, we might have a buggy _moddi3
+          * (at least glicb 2.2.5 is known to have this bug), in other
+          * words our integer modulus with negative quad as the second
+          * argument might be broken.  Test for this and re-patch the
+          * opcode dispatch table if that is the case, remembering to
+          * also apply the workaround so that this first round works
+          * right, too.  See [perl #9402] for more information. */
+#if defined(__GLIBC__) && IVSIZE == 8
+         {
+              IV l =   3;
+              IV r = -10;
+              /* Cannot do this check with inlined IV constants since
+               * that seems to work correctly even with the buggy glibc. */
+              if (l % r == -3) {
+                   /* Yikes, we have the bug.
+                    * Patch in the workaround version. */
+                   PL_op->op_ppaddr =
+                        PL_ppaddr[OP_I_MODULO] =
+                            &Perl_pp_i_modulo_1;
+                   /* Make certain we work right this time, too. */
+                   right = PERL_ABS(right);
+              }
+         }
+#endif
+         SETi( left % right );
+         RETURN;
+     }
 }
 
 PP(pp_i_add)
@@ -2830,24 +2895,14 @@ PP(pp_int)
                  SETu(U_V(value));
              } else {
 #if defined(SPARC64_MODF_WORKAROUND)
-               (void)sparc64_workaround_modf(value, &value);
-#else
-#   if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
-#       ifdef HAS_MODFL_POW32_BUG
+                  (void)sparc64_workaround_modf(value, &value);
+#elif defined(HAS_MODFL_POW32_BUG)
 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
-                {
-                    NV offset = Perl_modf(value, &value);
-                    (void)Perl_modf(offset, &offset);
-                    value += offset;
-                }
-#       else
-                 (void)Perl_modf(value, &value);
-#       endif
-#   else
-                 double tmp = (double)value;
-                 (void)Perl_modf(tmp, &tmp);
-                 value = (NV)tmp;
-#   endif
+                  NV offset = Perl_modf(value, &value);
+                  (void)Perl_modf(offset, &offset);
+                  value += offset;
+#else
+                  (void)Perl_modf(value, &value);
 #endif
                  SETn(value);
              }
@@ -2856,24 +2911,17 @@ PP(pp_int)
              if (value > (NV)IV_MIN - 0.5) {
                  SETi(I_V(value));
              } else {
-#if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
-#   ifdef HAS_MODFL_POW32_BUG
+#if defined(SPARC64_MODF_WORKAROUND)
+                  (void)sparc64_workaround_modf(-value, &value);
+#elif defined(HAS_MODFL_POW32_BUG)
 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
-                 {
-                     NV offset = Perl_modf(-value, &value);
-                     (void)Perl_modf(offset, &offset);
-                     value += offset;
-                 }
-#   else
-                 (void)Perl_modf(-value, &value);
-#   endif
-                 value = -value;
+                  NV offset = Perl_modf(-value, &value);
+                  (void)Perl_modf(offset, &offset);
+                  value += offset;
 #else
-                 double tmp = (double)value;
-                 (void)Perl_modf(-tmp, &tmp);
-                 value = -(NV)tmp;
+                 (void)Perl_modf(-value, &value);
 #endif
-                 SETn(value);
+                 SETn(-value);
              }
          }
       }
@@ -3327,7 +3375,8 @@ PP(pp_chr)
        tmps = SvPVX(TARG);
        if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
            memEQ(tmps, "\xef\xbf\xbd\0", 4)) {
-           SvGROW(TARG,3);
+           SvGROW(TARG, 3);
+           tmps = SvPVX(TARG);
            SvCUR_set(TARG, 2);
            *tmps++ = (U8)UTF8_EIGHT_BIT_HI(value);
            *tmps++ = (U8)UTF8_EIGHT_BIT_LO(value);
@@ -3358,6 +3407,24 @@ PP(pp_crypt)
         sv_utf8_downgrade(tsv, FALSE);
         tmps = SvPVX(tsv);
     }
+#   ifdef USE_ITHREADS
+#     ifdef HAS_CRYPT_R
+    if (!PL_reentrant_buffer->_crypt_struct_buffer) {
+      /* This should be threadsafe because in ithreads there is only
+       * one thread per interpreter.  If this would not be true,
+       * we would need a mutex to protect this malloc. */
+        PL_reentrant_buffer->_crypt_struct_buffer =
+         (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
+#if defined(__GLIBC__) || defined(__EMX__)
+       if (PL_reentrant_buffer->_crypt_struct_buffer) {
+           PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
+           /* work around glibc-2.2.5 bug */
+           PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
+       }
+#endif
+    }
+#     endif /* HAS_CRYPT_R */
+#   endif /* USE_ITHREADS */
 #   ifdef FCRYPT
     sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
 #   else
@@ -4657,6 +4724,7 @@ PP(pp_split)
                }
            }
            s = rx->endp[0] + orig;
+           PUTBACK;
        }
     }
 
@@ -4723,12 +4791,10 @@ PP(pp_split)
        if (gimme == G_ARRAY)
            RETURN;
     }
-    if (iters || !pm->op_pmreplroot) {
-       GETTARGET;
-       PUSHi(iters);
-       RETURN;
-    }
-    RETPUSHUNDEF;
+
+    GETTARGET;
+    PUSHi(iters);
+    RETURN;
 }
 
 #ifdef USE_5005THREADS