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

Re: URI::path() confused by $1

Thread Next
From:
Gisle Aas
Date:
April 7, 2000 12:48
Subject:
Re: URI::path() confused by $1
Message ID:
m3r9ch8zup.fsf@eik.g.aas.no
The URI module have problems because of the following behaviour
of $1 in subroutine argument lists (@_):

-----------------------------------------------------
#!/usr/bin/perl -w

"foobar" =~ /(o+)/;

foo($1);    # prints "a"
foo("$1");  # print "oo"

sub foo
{
    "foobar" =~ /(a+)/;
    print "@_\n";
}
__END__
------------------------------------------------------

I get the same result with perl5.004, perl5.005 and perl5.6.0.
I assume this is a perl bug.  Is it easily fixable?

Regards,
Gisle



Peter Hendrickson <pdh@best.com> writes:

> Hi,
> 
> I've noticed that URI::path() interacts badly with the $1 variable.
> If you call $uri->path($1) instead of setting the path to $1, you get
> strange results such as the new path being the scheme and domain name
> of the old URI.
> 
> I haven't figured out exactly why this is happening, but I have
> identified the code and that does it and fixed it.  A patch to
> URI-1.05 is enclosed below.  It passes all the tests in "make test"
> and seems to work when I try it.  Looking through other functions in
> URI-1.05/URI/_generic.pm makes me think this might be happening
> elsewhere, too, but I thought it better not to submit a patch to all
> of the other routines without really understanding what is going on.
> 
> Item 3 below is some test code which demonstrates the bug.  The two
> uri values it prints should be the same, but on a broken system you
> get something like this:
> > $uri1: http://www.amazon.html/http://www.amazon.html
> > $uri2: http://www.amazon.html/greatdeals
> 
> Tedious version discussion: I first noticed this using URI-1.04 under
> Perl 5.004_4 running under a NetBSD system.  I upgraded to URI-1.05
> and found the bug was still present.  I also tried it on a Linux
> system running Perl 5.005_3, which I believe is the current stable
> release.  Perl 5.005_3 appears to have URI-1.04 installed.  Item 1
> below is "perl -V" for the NetBSD system and item 2 is the same thing
> for the Linux system.
> 
> I'm not root on the Linux system, so the changes were made on the
> NetBSD system and tested there using URI-1.05.
> 
> Please let me know if I can be of any assistance - and thanks for
> writing this handy module!
> 
> Peter
> 
> --------------------------------------------------
> 1. Perl Version where change was made:
> Summary of my perl5 (5.0 patchlevel 4 subversion 4) configuration:
>   Platform:
>     osname=netbsd, osvers=1.4.1, archname=i386-netbsd
>     uname='netbsd natasha 1.4.1 netbsd 1.4.1 (generic) #0: thu dec 23 09:54:05 pst 1999 pdh@natasha:usrsrcsysarchi386compilegeneric i386 '
>     hint=recommended, useposix=true, d_sigaction=define
>     bincompat3=y useperlio=undef d_sfio=undef
>   Compiler:
>     cc='cc', optimize='-O2', gccversion=egcs-2.91.60 19981201 (egcs-1.1.1 release)
>     cppflags='-I/usr/pkg/include'
>     ccflags ='-I/usr/pkg/include'
>     stdchar='char', d_stdstdio=undef, usevfork=true
>     voidflags=15, castflags=0, d_casti32=define, d_castneg=define
>     intsize=4, alignbytes=4, usemymalloc=n, prototype=define
>   Linker and Libraries:
>     ld='ld', ldflags =' -L/usr/pkg/lib'
>     libpth=/usr/pkg/lib /usr/lib
>     libs=-lm -lcrypt
>     libc=/usr/lib/libc.so.12.40, so=so
>     useshrplib=false, libperl=libperl.a
>   Dynamic Linking:
>     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=define, ccdlflags='-Wl,-R/usr/pkg/lib  -Wl,-R/usr/pkg/lib/perl5/i386-netbsd/5.00404/CORE'
>     cccdlflags='-DPIC -fPIC ', lddlflags='-Bforcearchive -Bshareable  -L/usr/pkg/lib'
> 
> 
> Characteristics of this binary (from libperl): 
>   Built under netbsd
>   Compiled at Dec 25 1999 13:02:56
>   @INC:
>     /usr/pkg/lib/perl5/i386-netbsd/5.00404
>     /usr/pkg/lib/perl5
>     /usr/pkg/lib/perl5/site_perl/i386-netbsd
>     /usr/pkg/lib/perl5/site_perl
>     .
> 
> 2. More recent Perl version where bug is also expressed:
> Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
>   Platform:
>     osname=linux, osvers=2.2.5-22smp, archname=i386-linux
>     uname='linux porky.devel.redhat.com 2.2.5-22smp #1 smp wed jun 2 09:11:51 edt 1999 i686 unknown '
>     hint=recommended, useposix=true, d_sigaction=define
>     usethreads=undef useperlio=undef d_sfio=undef
>   Compiler:
>     cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
>     cppflags='-Dbool=char -DHAS_BOOL -I/usr/local/include'
>     ccflags ='-Dbool=char -DHAS_BOOL -I/usr/local/include'
>     stdchar='char', d_stdstdio=undef, usevfork=false
>     intsize=4, longsize=4, ptrsize=4, doublesize=8
>     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
>     alignbytes=4, usemymalloc=n, prototype=define
>   Linker and Libraries:
>     ld='cc', ldflags =' -L/usr/local/lib'
>     libpth=/usr/local/lib /lib /usr/lib
>     libs=-lnsl -ldl -lm -lc -lposix -lcrypt
>     libc=, so=so, useshrplib=false, libperl=libperl.a
>   Dynamic Linking:
>     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
>     cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'
> 
> 
> Characteristics of this binary (from libperl): 
>   Built under linux
>   Compiled at Aug 30 1999 23:09:51
>   @INC:
>     /usr/lib/perl5/5.00503/i386-linux
>     /usr/lib/perl5/5.00503
>     /usr/lib/perl5/site_perl/5.005/i386-linux
>     /usr/lib/perl5/site_perl/5.005
>     .
> 
> 3. Test Case:
> #!/usr/bin/perl -w
> 
> # Demonstrates bad URI interaction with $1.
> 
> require 5;
> use strict;
> 
> use URI;
> 
> &main();
> 
> sub main {
>     my($url_string) = "http://www.amazon.html/greatdeals.html";
>     my($uri1) = URI->new($url_string);
>     my($uri2) = URI->new($url_string);
> 
>     # Let's strip ".html" off the path for some reason.
>     if( $uri1->path =~ /^(.*)\.html$/ ) {
> 	$uri1->path($1);
>     }
> 
>     if( $uri2->path =~ /^(.*)\.html$/ ) {
> 	my($newpath) = $1;
> 	$uri2->path($newpath);
>     }
> 
>     # Not the same!
>     print "\$uri1: $uri1\n";
>     print "\$uri2: $uri2\n";
> }
> 
> 4. Diff on URI/_generic.pm from Version 1.05:
> *** URI/_generic.pm	Thu Jul 22 05:24:59 1999
> --- ../URI-1.05-modified/URI/_generic.pm	Wed Mar  8 08:29:23 2000
> ***************
> *** 34,45 ****
>   sub path
>   {
>       my $self = shift;
>       $$self =~ m,^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$,s or die;
>   
>       if (@_) {
>   	$$self = $1;
>   	my $rest = $3;
> - 	my $new_path = shift;
>   	$new_path = "" unless defined $new_path;
>   	$new_path =~ s/([^$PCHAR])/$URI::Escape::escapes{$1}/go;
>   	_check_path($new_path, $$self);
> --- 34,45 ----
>   sub path
>   {
>       my $self = shift;
> +     my $new_path = $_[0] if @_;
>       $$self =~ m,^((?:[^:/?\#]+:)?(?://[^/?\#]*)?)([^?\#]*)(.*)$,s or die;
>   
>       if (@_) {
>   	$$self = $1;
>   	my $rest = $3;
>   	$new_path = "" unless defined $new_path;
>   	$new_path =~ s/([^$PCHAR])/$URI::Escape::escapes{$1}/go;
>   	_check_path($new_path, $$self);
> 

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