Bug in Constants.pm causes 500 error when running agent

Bug #647097 reported by Saint Aardvark
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OCS Inventory: Server
New
Undecided
Unassigned

Bug Description

I'm running OCS Inventory version 1.3.2 on CentOS Linux, 5.4; the
package comes from the Fedora EPEL project. The client is version
1.0.1 (yes, that's old, but I'm having unrelated problems with version
1.1.2 on CentOS, and from what I can see the problem isn't related to
the client).

When I run the OCS Inventory client and point it at the server, I get
a 500 error from Apache and this message logged in Apache's error_log:

----
DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '|CHECKSUM|1),
        NAME='bogus',
        WORKGROUP='example.com',
        USERDOMAIN=NULL,' at line 4 at /usr/lib/perl5/vendor_perl/5.8.8/Apache/Ocsinventory/Server/Inventory/Update/Hardware.pm line 35.
----

(Actual hostnames deleted.) I added some debugging statements to
Hardware.pm, and here's a bit more context from the query
being run:

----
UPDATE hardware SET USERAGENT='OCS-NG_linux_client_v15',
        LASTDATE=NOW(),
        LASTCOME=NOW(),
        CHECKSUM=(|CHECKSUM|1),
        NAME='bogus',
        WORKGROUP='example.com',
        USERDOMAIN=NULL,
----

The relevant bit of Hardware.pm is:

----
  $dbh->do("UPDATE hardware SET USERAGENT=".$dbh->quote($ua).",
        LASTDATE=NOW(),
        LASTCOME=NOW(),
        CHECKSUM=(".(defined($base->{CHECKSUM})?$base->{CHECKSUM}:CHECKSUM_MAX_VALUE)."|CHECKSUM|1),
        NAME=".$dbh->quote($base->{NAME}).",
        WORKGROUP=".$dbh->quote($base->{WORKGROUP}).",
----

In turn, CHECKSUM_MAX_VALUE is defined in
Ocsinventory/Server/Constants.pm:

----
my $checksum_max_value = &get_checksum();
use constant CHECKSUM_MAX_VALUE => $checksum_max_value;

[snip]

sub get_checksum {
  my $checksum;

  for my $section (keys %DATA_MAP){
    $checksum|=$DATA_MAP{$section}->{mask};
  }
  return $checksum;
}
----

By adding more debugging statements, I figured out that the subroutine
get_checksum() *was* returning a sane value (262143), but the "use
constant" statement was *not* using it. This problem can be reduced
to the test case:

----
$ perl -e'$bar = &get_bar(); print "\$bar == |$bar|\n"; use constant FOO => $bar; print "FOO == |" . FOO . "|\n"; sub get_bar { return 1;}'
$bar == |1|
FOO == ||
----

The problem appears to be that a "use constant" statement is
evaluated/set at *compile* time, and the variable assignment is done
at *run* time. This PerlMonks thread has more details:

 http://www.perlmonks.org/bare/?node_id=843110

I'm not sure what the correct solution would be, but a stupid
workaround for me is just to insert this into Hardware.pm:

 use constant CHECKSUM_MAX_VALUE => 262143;

Thanks very much, and please let me know if you need any further information.

mortheres (mortheres)
affects: ocsinventory-contact → ocsinventory-server
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.