develooper Front page | perl.perl5.porters | Postings from December 2000

[PATCH] Warn on use of reference as array elem

Thread Next
From:
Simon Cozens
Date:
December 23, 2000 07:12
Subject:
[PATCH] Warn on use of reference as array elem
Message ID:
20001223151157.A7936@deep-dark-truthful-mirror.perlhacker.org
This'll warn on things like
    $a = \1; $array[$a];

which can easily cause bizarre out-of-memory conditions and aren't usually
what the programmer meant.


--- pp_hot.c~	Sat Dec 23 15:03:27 2000
+++ pp_hot.c	Sat Dec 23 15:04:59 2000
@@ -2815,12 +2815,15 @@
 {
     djSP;
     SV** svp;
-    IV elem = POPi;
+    SV* elemsv = POPs;
+    IV elem = SvIV(elemsv);
     AV* av = (AV*)POPs;
     U32 lval = PL_op->op_flags & OPf_MOD;
     U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
     SV *sv;
 
+    if (SvROK(elemsv))
+	Perl_warner(aTHX_ WARN_MISC, "Use of reference \"%s\" as array element", SvPV_nolen(elemsv));
     if (elem > 0)
 	elem -= PL_curcop->cop_arybase;
     if (SvTYPE(av) != SVt_PVAV)
--- pod/perldiag.pod~	Sat Dec 23 15:07:00 2000
+++ pod/perldiag.pod	Sat Dec 23 15:09:59 2000
@@ -3700,6 +3700,15 @@
 (D deprecated) This was an ill-advised attempt to emulate a poorly
 defined B<awk> feature.  Use an explicit printf() or sprintf() instead.
 
+=item Use of reference "%s" in array element
+
+(W) You tried to use a reference as an array element; this probably
+isn't what you mean, because references tend to be huge numbers which
+take you out of memory, and so usually indicates programmer error.
+
+If you really do mean it, explicitly stringify or numify your reference,
+like so: C<$array["$ref"]> or C<$array[0+$ref]>
+
 =item Use of reserved word "%s" is deprecated
 
 (D deprecated) The indicated bareword is a reserved word.  Future

-- 
We use Linux for all our mission-critical applications. Having the source code
means that we are not held hostage by anyone's support department.
(Russell Nelson, President of Crynwr Software)

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About