From: Eric Wong Date: Tue, 10 Oct 2023 10:07:56 +0000 (+0000) Subject: over*: avoid defined-or hash assignments with side-effects X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d75e148e550a93548d09f05cc98be6c5fd63fc0;p=thirdparty%2Fpublic-inbox.git over*: avoid defined-or hash assignments with side-effects These may've been causing strange errors[1] in t/imapd.t from the -watch daemon, such as: Cannot copy to HASH in scalar assignment ../PublicInbox/Over.pm in the Over->dbh() sub. I've only noticed this failure on FreeBSD 13.2 (Perl 5.32.1, DBD::SQLite 1.72 (bundled SQLite 3.39.4), DBI 1.643) so far, so it could also be something to do with the versions used and/or memory layout differences with libc or build toolchain. --- diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm index e3a8adb1b..4eed4f469 100644 --- a/lib/PublicInbox/Over.pm +++ b/lib/PublicInbox/Over.pm @@ -82,7 +82,13 @@ sub dbh_close { } } -sub dbh ($) { $_[0]->{dbh} //= $_[0]->dbh_new } # dbh_new may be subclassed +sub dbh ($) { + my ($self) = @_; + $self->{dbh} // do { + my $dbh = $self->dbh_new; # dbh_new may be subclassed + $self->{dbh} = $dbh; + } +} sub load_from_row ($;$) { my ($smsg, $cull) = @_; @@ -256,9 +262,12 @@ SELECT ts,ds,ddd FROM over WHERE $s sub get_art { my ($self, $num) = @_; # caching $sth ourselves is faster than prepare_cached - my $sth = $self->{-get_art} //= dbh($self)->prepare(<<''); + my $sth = $self->{-get_art} // do { + my $sth = dbh($self)->prepare(<<''); SELECT num,tid,ds,ts,ddd FROM over WHERE num = ? LIMIT 1 + $self->{-get_art} = $sth; + }; $sth->execute($num); my $smsg = $sth->fetchrow_hashref; $smsg ? load_from_row($smsg) : undef; diff --git a/lib/PublicInbox/OverIdx.pm b/lib/PublicInbox/OverIdx.pm index aead375a3..c9c25828a 100644 --- a/lib/PublicInbox/OverIdx.pm +++ b/lib/PublicInbox/OverIdx.pm @@ -520,7 +520,7 @@ EOM # used for cross-inbox search sub eidx_prep ($) { my ($self) = @_; - $self->{-eidx_prep} //= do { + $self->{-eidx_prep} // do { my $dbh = $self->dbh; $dbh->do(<<''); INSERT OR IGNORE INTO counter (key) VALUES ('eidx_docid') @@ -565,7 +565,7 @@ CREATE TABLE IF NOT EXISTS eidx_meta ( $dbh->do(<<''); CREATE TABLE IF NOT EXISTS eidxq (docid INTEGER PRIMARY KEY NOT NULL) - 1; + $self->{-eidx_prep} = 1; }; }