Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'find2perl' bug: -exec causes chdir error #7181

Closed
p5pRT opened this issue Mar 18, 2004 · 4 comments
Closed

'find2perl' bug: -exec causes chdir error #7181

p5pRT opened this issue Mar 18, 2004 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 18, 2004

Migrated from rt.perl.org#27748 (status was 'resolved')

Searchable as RT27748$

@p5pRT
Copy link
Author

p5pRT commented Mar 18, 2004

From @jdhedden

The code generated by 'find2perl' when the '-exec' directive is used
is faulty. A global variable for the current working directory ($cwd)
is not being set because its code occurs after the exit() call for the
generated script.

Here is an example of the generated code​:

<<< EXAMPLE STARTS >>>

  > find2perl.buggy . -name '*.pl' -exec grep -H BUGUS '{}' ';'
  #! /usr/bin/perl -w
  eval 'exec /usr/bin/perl -S $0 ${1+"$@​"}'
  if 0; #$running_under_some_shell

  use strict;
  use File​::Find ();

  # Set the variable $File​::Find​::dont_use_nlink if you're using
AFS,
  # since AFS cheats.

  # for the convenience of &wanted calls, including -eval
statements​:
  use vars qw/*name *dir *prune/;
  *name = *File​::Find​::name;
  *dir = *File​::Find​::dir;
  *prune = *File​::Find​::prune;

  sub wanted;
  sub doexec ($@​);

  # Traverse desired filesystems
  File​::Find​::find({wanted => \&wanted}, '.');
  exit;

  sub wanted {
  /^.*\.pl\z/s &&
  doexec(0, 'grep','-H','BUGUS','{}');
  }

  use Cwd ();
  my $cwd = Cwd​::cwd();

  sub doexec ($@​) {
  my $ok = shift;
  my @​command = @​_; # copy so we don't try to s/// aliases to
constants
  for my $word (@​command)
  { $word =~ s#{}#$name#g }
  if ($ok) {
  my $old = select(STDOUT);
  $| = 1;
  print "@​command";
  select($old);
  return 0 unless <STDIN> =~ /^y/;
  }
  chdir $cwd; #sigh
  system @​command;
  chdir $File​::Find​::dir;
  return !$?;
  }

<<< EXAMPLE ENDS >>>

Note in the above that the lines​:

  use Cwd ();
  my $cwd = Cwd​::cwd();

occur after​:

  File​::Find​::find({wanted => \&wanted}, '.');
  exit;

As a result $cwd never gets set, and causes errors when the doexec()
function is executed, as in the following​:

<<< EXAMPLE STARTS >>>

  > find2perl.buggy . -name '*.pl' -exec grep -H BUGUS '{}' ';' |
perl
  Use of uninitialized value in chdir at - line 48.
  Use of chdir('') or chdir(undef) as chdir() is deprecated at -
line 48.
  Use of uninitialized value in chdir at - line 48.
  Use of chdir('') or chdir(undef) as chdir() is deprecated at -
line 48.
  Use of uninitialized value in chdir at - line 48.
  ...

<<< EXAMPLE ENDS >>>

I have verified that this bug is present in the following Perl
distributions​:
  5.6.2
  5.8.3
  5.9.1

I have a fix for this bug. The following is a diff of 'find2perl' as
installed from the Perl 5.9.1 distribution​:

<<< DIFF STARTS >>>

  259a260,267
  > if (exists $init{doexec}) {
  > print <<'END';
  > use Cwd ();
  > my $cwd = Cwd​::cwd();
  >
  > END
  > }
  >
  327,330d334
  <
  < use Cwd ();
  < my $cwd = Cwd​::cwd();
  <

<<< DIFF ENDS >>>

This fix will work with all the versions of 'find2perl' in the
distributions that I examined. As the 'diff' lines will differ
between version, the following shows the code changes with bracketing
lines from the 'find2perl' script​:

<<< FIRST CODE SECTION STARTS >>>

  $declaresubs

  END

  ### The following block needs to be added ###
  if (exists $init{doexec}) {
  print <<'END';
  use Cwd ();
  my $cwd = Cwd​::cwd();

  END
  }
  ### (end block) ###

  if (exists $init{ls}) {
  print <<'END';

<<< FIRST CODE SECTION ENDS >>>

<<< SECOND CODE SECTION STARTS >>>

  if (exists $init{doexec}) {
  print <<'END';

  ### The following two lines need to be removed ###
  use Cwd ();
  my $cwd = Cwd​::cwd();
  ### (end remove) ###

  sub doexec ($@​) {

<<< SECOND CODE SECTION ENDS >>>

Thank you,
Jerry D. Hedden

@p5pRT
Copy link
Author

p5pRT commented Mar 24, 2004

From @iabyn

On Thu, Mar 18, 2004 at 08​:25​:50PM -0000, jdhedden@​1979.usna.com (via RT) wrote​:

The code generated by 'find2perl' when the '-exec' directive is used
is faulty. A global variable for the current working directory ($cwd)
is not being set because its code occurs after the exit() call for the
generated script.

Thanks, your patch has been applied to the development veersion of Perl as
change #22586.

NB​: in case you submit any future patches, it is easier for us if you
generate it using diff -u, or if the -u option isn't supported, diff -c.

Dave.

--
The perl5 internals are a complete mess. It's like Jenga - to get the
perl5 tower taller and do something new you select a block somewhere in
the middle, with trepidation pull it out slowly, and then carefully
balance it somewhere new, hoping the whole edifice won't collapse as a
result.
  - Nicholas Clark, based on an original by Simon Cozens.

@p5pRT
Copy link
Author

p5pRT commented Mar 24, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Mar 24, 2004

@iabyn - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant