threads / patch / 7841

patchgit-svn: Don't rely on $_ after making a function call

Subject: [PATCH] git-svn: Don't rely on $_ after making a function call

## tl;dr

5 messages between Apr 25, 2007 and Apr 26, 2007. Diffs are folded; open one to read it.

replies: 4people: 4as markdown or json

Adam Roben· Apr 25, 2007, 18:50 UTC · lore

Many functions and operators in perl set $_, so its value cannot be relied upon after calling arbitrary functions. The solution is simply to copy the value of $_ into a local variable that will not get overwritten.

Signed-off-by: Adam Roben <aroben@apple.com>
---
 git-svn.perl |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
Show changes to git-svn.perl +5 −5
diff --git a/git-svn.perl b/git-svn.perl
index 077d6b3..90f3bc1 100755
--- a/git-svn.perl
+++ b/git-svn.perl
@@ -771,19 +771,19 @@ sub cmt_metadata {
 sub working_head_info {
 	my ($head, $refs) = @_;
 	my ($fh, $ctx) = command_output_pipe('rev-list', $head);
-	while (<$fh>) {
-		chomp;
-		my ($url, $rev, $uuid) = cmt_metadata($_);
+	while (my $hash = <$fh>) {
+		chomp($hash);
+		my ($url, $rev, $uuid) = cmt_metadata($hash);
 		if (defined $url && defined $rev) {
 			if (my $gs = Git::SVN->find_by_url($url)) {
 				my $c = $gs->rev_db_get($rev);
-				if ($c && $c eq $_) {
+				if ($c && $c eq $hash) {
 					close $fh; # break the pipe
 					return ($url, $rev, $uuid, $gs);
 				}
 			}
 		}
-		unshift @$refs, $_ if $refs;
+		unshift @$refs, $hash if $refs;
 	}
 	command_close_pipe($fh, $ctx);
 	(undef, undef, undef, undef);
-- 
1.5.2.rc0.14.g520d-dirty
Eric Wong· Apr 25, 2007, 20:59 UTC · re: Adam Roben · lore

Re: [PATCH] git-svn: Don't rely on $_ after making a function call

Adam Roben <aroben@apple.com> wrote:
> Many functions and operators in perl set $_, so its value cannot be relied upon
> after calling arbitrary functions. The solution is simply to copy the value of
> $_ into a local variable that will not get overwritten.

Does this fix any particular bug? It looks right to me and makes the code easier to follow, so;

Acked-by: Eric Wong <normalperson@yhbt.net>
Show 37 quoted lines
> Signed-off-by: Adam Roben <aroben@apple.com>
> ---
>  git-svn.perl |   10 +++++-----
>  1 files changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/git-svn.perl b/git-svn.perl
> index 077d6b3..90f3bc1 100755
> --- a/git-svn.perl
> +++ b/git-svn.perl
> @@ -771,19 +771,19 @@ sub cmt_metadata {
>  sub working_head_info {
>  	my ($head, $refs) = @_;
>  	my ($fh, $ctx) = command_output_pipe('rev-list', $head);
> -	while (<$fh>) {
> -		chomp;
> -		my ($url, $rev, $uuid) = cmt_metadata($_);
> +	while (my $hash = <$fh>) {
> +		chomp($hash);
> +		my ($url, $rev, $uuid) = cmt_metadata($hash);
>  		if (defined $url && defined $rev) {
>  			if (my $gs = Git::SVN->find_by_url($url)) {
>  				my $c = $gs->rev_db_get($rev);
> -				if ($c && $c eq $_) {
> +				if ($c && $c eq $hash) {
>  					close $fh; # break the pipe
>  					return ($url, $rev, $uuid, $gs);
>  				}
>  			}
>  		}
> -		unshift @$refs, $_ if $refs;
> +		unshift @$refs, $hash if $refs;
>  	}
>  	command_close_pipe($fh, $ctx);
>  	(undef, undef, undef, undef);
> -- 
> 1.5.2.rc0.14.g520d-dirty
> 
-- 
Eric Wong
Adam Roben· Apr 25, 2007, 21:03 UTC · re: Eric Wong · lore

Re: [PATCH] git-svn: Don't rely on $_ after making a function call

On Apr 25, 2007, at 1:59 PM, Eric Wong wrote:
Show 9 quoted lines
> Adam Roben <aroben@apple.com> wrote:
>> Many functions and operators in perl set $_, so its value cannot  
>> be relied upon
>> after calling arbitrary functions. The solution is simply to copy  
>> the value of
>> $_ into a local variable that will not get overwritten.
>
> Does this fix any particular bug?  It looks right to me
> and makes the code easier to follow, so;
    Yes, I was getting warnings that $_ was uninitialized in the eq.  
Sorry for not mentioning that.
-Adam
Junio C Hamano· Apr 25, 2007, 21:13 UTC · re: Eric Wong · lore

Re: [PATCH] git-svn: Don't rely on $_ after making a function call

Eric Wong <normalperson@yhbt.net> writes:
Show 7 quoted lines
> Adam Roben <aroben@apple.com> wrote:
>> Many functions and operators in perl set $_, so its value cannot be relied upon
>> after calling arbitrary functions. The solution is simply to copy the value of
>> $_ into a local variable that will not get overwritten.
>
> Does this fix any particular bug?  It looks right to me
> and makes the code easier to follow, so;
I suspect Merlyn has better explanation, but...
     for (list) {
	...	
     }

loop implicitly localizes $_ and does not share this problem, which I really appreciate whenever I am writing Perl code, but often enough I was bitten by scripts that use "while (<$fh>)", which does not localizes $_, and made hard to spot bugs by clobbering $_.

I find the patch a good safety measure for any future breakages.
Randal L. Schwartz· Apr 26, 2007, 02:11 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-svn: Don't rely on $_ after making a function call

>>>>> "Junio" == Junio C Hamano <junkio@cox.net> writes:
Junio> I suspect Merlyn has better explanation, but...

Junio> for (list) { Junio> ... Junio> }

Junio> loop implicitly localizes $_ and does not share this problem, Junio> which I really appreciate whenever I am writing Perl code, but Junio> often enough I was bitten by scripts that use "while (<$fh>)", Junio> which does not localizes $_, and made hard to spot bugs by Junio> clobbering $_.

Junio> I find the patch a good safety measure for any future breakages.
I'm happy with the patch.
-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

← back to recent threads