Bug #17323 Can't use undefined value as HASH reference at ..../mysql.pm line 113
Submitted: 11 Feb 2006 11:26 Modified: 29 Dec 2006 17:17
Reporter: Phil Randal Email Updates:
Status: Closed Impact on me:
None 
Category:Connectors: DBD::mysql ( Perl ) Severity:S3 (Non-critical)
Version:2.903 and later OS:Perl
Assigned to: CPU Architecture:Any

[11 Feb 2006 11:26] Phil Randal
Description:
In mysql.pm the Connect method has three optional parameters:

$username, $password, and $attrhash

Only the first two are given default "null" values if they are undefined.

How to repeat:
I found this error after upgrading a Redhat Linux 9 box to CentOS 4.2.

A previously flawless Nagmin installation (http://nagmin.sf.net) then crashed with the error:

"Error - Perl execution failed

Can't use an undefined value as a HASH reference at /path/to/mysql.pm line 113".

Suggested fix:
At around line 109 of mysql.pm:

------------------------------------
    # Avoid warnings for undefined values
    $username ||= '';
    $password ||= '';

    # create a 'blank' dbh
    my($this, $privateAttrHash) = (undef, $attrhash);
    $privateAttrHash = { %$privateAttrHash,
	'Name' => $dsn,
	'user' => $username,
	'password' => $password
    };
-------------------------------------

Needs to be changed to

-------------------------------------
   # Avoid warnings for undefined values
    $username ||= '';
    $password ||= '';
    $attrhash ||= {};

    # create a 'blank' dbh
    my($this, $privateAttrHash) = (undef, $attrhash);
    $privateAttrHash = { %$privateAttrHash,
	'Name' => $dsn,
	'user' => $username,
	'password' => $password
    };
---------------------------------------

Verified fixed with that extra line applied, which is obviously correct and good defensive programming.

The bug (and the fix) has also been reported by Chris Radcliff on the mysql and perl list on November 17, 2003:

  http://lists.mysql.com/perl/2570
[12 Feb 2006 9:47] Valeriy Kravchuk
Thank you for a problem report. Please, check newer versions (3.0002, namely). I see no problem like you described in them.
[12 Feb 2006 12:12] Phil Randal
That line is missing from mysql.pm 3.0002.  It is NOT fixed in 3.0002.  If it had already been fixed, I would not have reported the bug.

From the synopsis in the documentation:

"   use DBI;

    $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";

    $dbh = DBI->connect($dsn, $user, $password);"

and later down

"  use strict;
  use DBI();

  # Connect to the database.
  my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
                         "joe", "joe's password",
                         {'RaiseError' => 1});"

Like I've said and the documentation says, the 4th parameter is optional, and my suggested fix is both obvious and good defensive programming.

And it fixes the reported problem.

Cheers,

Phil
[14 Feb 2006 10:31] Valeriy Kravchuk
Verified just as described with 3.0002_4. File lib/DBD/mysql.pm should be patched.
[29 Dec 2006 17:17] Jim Winstead
I haven't been able to reproduce the warning, but I have applied the patch. As far as I can tell, the DBI module always passes a hash along to the driver's connect method, regardless of whether it gets an undefined attrhash passed to DBI->connect or not.