git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Narebski <jnareb@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH 3/5] gitweb: Always use three argument form of open
Date: Sun, 10 May 2009 02:38:34 +0200	[thread overview]
Message-ID: <200905100238.34838.jnareb@gmail.com> (raw)
In-Reply-To: <200905100203.51744.jnareb@gmail.com>

In most cases (except insert_file() subroutine) we used old two argument
form of 'open' to open files for reading.  This can cause subtle bugs when
$projectroot or $projects_list file starts with mode characters ('>', '<',
'+<', '|', etc.) or with leading whitespace; and also when $projects_list
file or $mimetypes_file or ctags files end with trailing whitespace or '|'.

Additionally it is also more clear to explicitly state that we open those
files for reading.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen

  Write open $fh, q{<}, $filename; instead of open $fh, "<$filename";.

  The three-argument form of open (introduced in Perl 5.6) prevents subtle
  bugs that occur when the filename starts with funny characters like
  '>' or '<'.  It's also more explicitly clear to define the input mode of
  the file, and not to e.g. use open( $fh, 'foo.txt' );

See also Damian Conway's book "Perl Best Practices",
chapter "10.4. Opening Cleanly" (Use either the 'IO::File' module
or the three-argument form of 'open'.)


This patch _textually_ depends on the previous patch (no bareword
filehandles), even if _conceptually_ they are quite independent.

 gitweb/gitweb.perl |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index a9daa1d..852beb6 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -2050,7 +2050,7 @@ sub git_get_project_description {
 	my $path = shift;
 
 	$git_dir = "$projectroot/$path";
-	open my $fd, "$git_dir/description"
+	open my $fd, '<', "$git_dir/description"
 		or return git_get_project_config('description');
 	my $descr = <$fd>;
 	close $fd;
@@ -2069,7 +2069,7 @@ sub git_get_project_ctags {
 		return $ctags;
 	}
 	foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
-		open my $ct, $_ or next;
+		open my $ct, '<', $_ or next;
 		my $val = <$ct>;
 		chomp $val;
 		close $ct;
@@ -2129,7 +2129,7 @@ sub git_get_project_url_list {
 	my $path = shift;
 
 	$git_dir = "$projectroot/$path";
-	open my $fd, "$git_dir/cloneurl"
+	open my $fd, '<', "$git_dir/cloneurl"
 		or return wantarray ?
 		@{ config_to_multi(git_get_project_config('url')) } :
 		   config_to_multi(git_get_project_config('url'));
@@ -2187,7 +2187,7 @@ sub git_get_projects_list {
 		# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 		# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 		my %paths;
-		open my ($fd), $projects_list or return;
+		open my $fd, '<', $projects_list or return;
 	PROJECT:
 		while (my $line = <$fd>) {
 			chomp $line;
@@ -2250,7 +2250,7 @@ sub git_get_project_list_from_file {
 	# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 	# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 	if (-f $projects_list) {
-		open (my $fd , $projects_list);
+		open(my $fd, '<', $projects_list);
 		while (my $line = <$fd>) {
 			chomp $line;
 			my ($pr, $ow) = split ' ', $line;
@@ -2804,7 +2804,7 @@ sub mimetype_guess_file {
 	-r $mimemap or return undef;
 
 	my %mimemap;
-	open(my $mh, $mimemap) or return undef;
+	open(my $mh, '<', $mimemap) or return undef;
 	while (<$mh>) {
 		next if m/^#/; # skip comments
 		my ($mimetype, $exts) = split(/\t+/);
-- 
1.6.3

  parent reply	other threads:[~2009-05-10  0:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-10  0:03 [PATCH 0/5] gitweb: Some code cleanups (up to perlcritic --stern) Jakub Narebski
2009-05-10  0:05 ` [PATCHv2 1/5] gitweb: Remove function prototypes Jakub Narebski
2009-05-10  9:05   ` Jakub Narebski
2009-05-10  0:36 ` [PATCH 2/5] gitweb: Do not use bareword filehandles Jakub Narebski
2009-05-10  7:50   ` Petr Baudis
2009-05-10  9:27     ` Jakub Narebski
2009-05-11  1:21   ` [PATCH v2 " Jakub Narebski
2009-05-10  0:38 ` Jakub Narebski [this message]
2009-05-11  1:29   ` [PATCH v2 3/5] gitweb: Always use three argument form of open Jakub Narebski
2009-05-10  0:39 ` [PATCH 4/5] gitweb: Localize magic variable $/ Jakub Narebski
2009-05-10  0:40 ` [PATCH 5/5] gitweb: Use block form of map/grep in a few cases more Jakub Narebski
2009-05-11  0:47 ` [PATCH 0/5] gitweb: Some code cleanups (up to perlcritic --stern) Junio C Hamano
2009-05-11  1:33   ` Jakub Narebski
2009-05-11  4:21     ` Junio C Hamano
2009-05-11  5:13       ` Daniel Pittman
2009-05-11  7:19       ` Jakub Narebski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200905100238.34838.jnareb@gmail.com \
    --to=jnareb@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).