Skip to contentSkip to conversation

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

PATCH In review Adam Roben 4 participants 5 messages Jun 15, 2016

lore
Share & tools
lore

Adam Roben

author
#0 lore raw

PATCH 1/1 git-svn: Don't rely on $_ after making a function call

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.

---
 git-svn.perl |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
git-svn.perl +5 -5
index 077d6b3..90f3bc1 100755
@@ -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);
Signature · 1 line
1.5.2.rc0.14.g520d-dirty
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Adam Roben

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;

Quoted 37 lines
---
 git-svn.perl |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
git-svn.perl +5 -5
index 077d6b3..90f3bc1 100755
@@ -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);
Signature · 1 line
1.5.2.rc0.14.g520d-dirty
Signature · 1 line
Eric Wong
author
#2 lore raw
Quoted 7 lines · Eric Wong
Quoted 3 lines · Adam Roben

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

#3 lore raw
In reply to Eric Wong
Quoted 7 lines · Eric Wong
Quoted 3 lines · Adam Roben

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.

#4 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Junio" == Junio C Hamano <[email protected]> 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.

Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <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!