Subject: | length($string) > 32; length(substr($string, 0, 32)) == 31 |
Strings are being represented with an incorrect length when pulled from a database.
DBD::Pg version 3.5.3; DBI version 1.634.0
Code
=================================
my $dbpass = '';
use DBI;
use Devel::Peek;
use strict;
use warnings;
my $db;
$db = DBI->connect('dbi:Pg:dbname=db;host=localhost','user',$dbpass,{AutoCommit => 1});
my ($md5size);
my $q = $db->prepare('SELECT md5size FROM hashes WHERE md5size IS NOT NULL LIMIT 5');
$q->execute();
$q->bind_columns(\$md5size);
while ($q->fetch()) {
print "\n";
Dump $md5size;
print $md5size , "\n";
printf "%vx\n", $md5size;
print '.' x 32, '-' x 32 . "\n";
print substr($md5size, 0, 32), " (" . length($md5size) . ' -- ' . length(substr($md5size, 0, 32)) . ")\n";
}
=================================
Output for the last good fetch and the first bad fetch
=================================
SV = PVMG(0xc8f590) at 0x9a1800
REFCNT = 2
FLAGS = (SMG,POK,pPOK,UTF8)
IV = 0
NV = 0
PV = 0xd3fcf0 "xxxxxxxxxxxxxxxxxxxxxxxx2c76efde-83654"\0 [UTF8 "xxxxxxxxxxxxxxxxxxxxxxxx2c76efde-83654"]
CUR = 38
LEN = 40
MAGIC = 0xd40030
MG_VIRTUAL = &PL_vtbl_utf8
MG_TYPE = PERL_MAGIC_utf8(w)
MG_LEN = 38
xxxxxxxxxxxxxxxxxxxxxxxx2c76efde-83654
xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.32.63.37.36.65.66.64.65.2d.38.33.34.35.36
................................--------------------------------
xxxxxxxxxxxxxxxxxxxxxxxx2c76efde (38 -- 32)
SV = PVMG(0xc8f590) at 0x9a1800
REFCNT = 2
FLAGS = (SMG,POK,pPOK,UTF8)
IV = 0
NV = 0
PV = 0xd3fcf0 "xxxxxxxxxxxxxxxxxxxxxxxxa7360334-5682"\0 [UTF8 "xxxxxxxxxxxxxxxxxxxxxxxxa7360334-5682"]
CUR = 37
LEN = 40
MAGIC = 0xd40030
MG_VIRTUAL = &PL_vtbl_utf8
MG_TYPE = PERL_MAGIC_utf8(w)
MG_LEN = 38
xxxxxxxxxxxxxxxxxxxxxxxxa7360334-5682
xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx.61.37.33.36.30.33.33.34.2d.35.36.38.32
................................--------------------------------
xxxxxxxxxxxxxxxxxxxxxxxxa736033 (38 -- 31)
=================================
Workarounds:
1. At the start of the loop, $md5size =~ /(.*)/; $md5size = $1;
2. At the end of the loop, $md5size = undef;
3. fetchrow_arrayref() doesn't exhibit the problem