develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #109146] 3-arg open of undef for read

From:
Nicholas Clark
Date:
January 27, 2012 03:02
Subject:
Re: [perl #109146] 3-arg open of undef for read
Message ID:
20120127110238.GM9069@plum.flirble.org
On Thu, Jan 26, 2012 at 05:25:56PM -0800, Yitzchak Scott-Thoennes wrote:

> open(FH, "<", undef) triggers special behavior to open a temporary file
> (at least with perlio) that is documented as only applying to +> and +<.
> 
> To keep open(FH, "<", $hash{typoedkey}) from quietly succeeding, this
> should at least trigger a warning (and the doc be updated to not be mode
> specific).

I think that a comprehensive fix should be possible. Note that XS can tell
the difference:

$ ./perl -Ilib -MDevel::Peek -e 'Dump undef'
SV = NULL(0x0) at 0x1003aa990
  REFCNT = 2147483621
  FLAGS = (READONLY)
$ ./perl -Ilib -MDevel::Peek -e 'Dump $a'
SV = NULL(0x0) at 0x100812b90
  REFCNT = 1
  FLAGS = ()
$ ./perl -Ilib -MDevel::Peek -e 'Dump $hash{typoedkey}'
SV = PVLV(0x100818a00) at 0x100802ed0
  REFCNT = 1
  FLAGS = (TEMP,GMG,SMG)
  IV = 0
  NV = 0
  PV = 0
  MAGIC = 0x100509040
    MG_VIRTUAL = &PL_vtbl_defelem
    MG_TYPE = PERL_MAGIC_defelem(y)
    MG_FLAGS = 0x02
      REFCOUNTED
    MG_OBJ = 0x100803110
    SV = PV(0x100801070) at 0x100803110
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x100501b50 "typoedkey"\0
      CUR = 9
      LEN = 16
  TYPE = y
  TARGOFF = 0
  TARGLEN = 1
  TARG = 0x100812ae8
  FLAGS = 0
  SV = PVHV(0x100807ea0) at 0x100812ae8
    REFCNT = 2
    FLAGS = (SHAREKEYS)
    ARRAY = 0x0
    KEYS = 0
    FILL = 0
    MAX = 7
    RITER = -1
    EITER = 0x0

Quite a big difference.

And that the case of a simple undefined variable is already spotted:

$ ./perl -e 'open $fh, "<", $a or die'
Died at -e line 1.
$ ./perl -e 'open $fh, "<", $hash{typoedkey} or die'


So I think that there's something more subtly amis here, probably due to
how and when the magic is executed for hash keys passed to subroutines.

Nicholas Clark



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