gnustavo / Git-Hooks

Framework for implementing Git (and Gerrit) hooks
http://search.cpan.org/dist/Git-Hooks/
41 stars 17 forks source link

Use of uninitialized value $CFG at Git/Hooks.pm line 80 #57

Closed oktal3700 closed 5 years ago

oktal3700 commented 5 years ago

Steps to reproduce

Place this script in .git/hooks/pre-receive:

#!/usr/bin/perl
package Git::Hooks::MyHook;
use Git::Hooks;
PRE_RECEIVE { return 0; };
run_hook($0, @ARGV);

All it does is refuse all pushed changes. Execute it with empty stdin to simulate no changes:

$ .git/hooks/pre-receive </dev/null

Output

Use of uninitialized value $CFG in substitution (s///) at /usr/local/share/perl/5.20.2/Git/Hooks.pm line 80.
Use of uninitialized value $CFG in lc at /usr/local/share/perl/5.20.2/Git/Hooks.pm line 81.

Impact

This affects all kinds of hooks, whenever they return false to indicate failure.

Cause

This regressed in a971c01b7497515ca3e87e08a6f5e45a263082a9. At line 80, $CFG gets the value $hook->{package}, which is set at line 37:

*{"Git::Hooks::$installer"} = sub (&) {
    push @{$Hooks{$hook}}, {
        package => scalar(caller(1)), # <-- problem here
        sub     => shift(@_),
    };
}

The problem is that scalar(caller(1)) is undef. The documentation for caller is not great. It actually goes back by one additional call frame.

Solution

Just use scalar(caller)

gnustavo commented 5 years ago

Thanks, @oktal3700 . I'm releasing v2.10.1 which includes your proposed fix.