beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 138 forks source link

Crash when called with particular search string (?) #520

Closed mafm closed 4 years ago

mafm commented 9 years ago

The 2.14 single-file version of ack crashes out here, when called like this: $ ack new_msg_warning --type=python Can't use an undefined value as a symbol reference at /c/mafm/bin/ack line 3127.

It also crashes in the same way with these search strings: $ ack msg_warning --type=python $ ack new_msg --type=python

But this works: $ ack warning --type=python

The same behaviour was also in the single-file version of 1.12, (died at line 997): $ old-ack new_msg_warning --type=python Can't use an undefined value as a symbol reference at /c/mafm/bin/old-ack line 997.

All on windows 7 - in msysgit's cygwin shell....

mafm commented 9 years ago

More info - I just noticed the error happens for me only when called from my home directory.

When in a working directory full of python code, ack appears to run as expected.

ack does work when called from the home directory for other search strings. (The relevant python code is in a git working copy, a couple of layers beneath the home directory.)

petdance commented 9 years ago

What if you don't have --type=python?

(also, FYI, you can just say --python, it's the same thing)

mafm commented 9 years ago

Without "--type=python" it works from my home directory.

(With --python, it fails in my home directory - as with "--type=python".)

Is there something simple I could do to get a stack trace for the error? (You've probably guessed I'm not a perl programmer).

petdance commented 9 years ago

Can you please verify that the line 3127 it's dying on is in sub firstliney and is:

    my $rc     = sysread( $fh, $buffer, 250 );

If so, it looks like the ->open above is failing and we're not checking for it.

mafm commented 9 years ago

Yes, that's what I have at line 3127 in my copy.

(And also in line 997 of the previous version.)

jadero commented 9 years ago

Installed App::Ack in Perl CPAN in Windows and had the same issue. I fixed it locally with some code I found in the standalone version of Ack. In sub "firstliney" in file Basic.pm, around line 157:

my $fh = $self->open();
if ( !$fh ) {
    if ( $App::Ack::report_bad_filenames ) {
      App::Ack::warn( $self->name . ': ' . $! );
    }
    return '';
}

if ( !exists $self->{firstliney} ) {

The error is caused by a file that cannot be opened for reading. Hope this helps.

jadero commented 6 years ago

Two and a half years later and I stumbled over this again :(

This time I had to add this to file "Resource.pm" after line 213.

petdance commented 6 years ago

I'm sorry about that. Can you say more about the file that couldn't be opened? Why couldn't it be opened?

petdance commented 6 years ago

Note to self: These need to get ported to ack3.

jadero commented 6 years ago

In file "Ack/Resource.pm". Now it looks like this:

sub firstliney {
    my ( $self ) = @_;

    my $fh = $self->open();

    if ( !$fh ) {
        if ( $App::Ack::report_bad_filenames ) {
          App::Ack::warn( $self->name . ': ' . $! );
        }
        return '';
    }

    if ( !exists $self->{firstliney} ) {
        my $buffer = '';
        my $rc     = sysread( $fh, $buffer, 250 );
        unless($rc) { # XXX handle this better?
            $buffer = '';
        }
        $buffer =~ s/[\r\n].*//s;
        $self->{firstliney} = $buffer;
        $self->reset;
    }

    $self->close;

    return $self->{firstliney};
}

I am not a Perl expert but this just works for me :^)

petdance commented 6 years ago

Fixed in e5de215. Will be released in Ack 2.22. Thanks.