[collectd] Modbus segfault

Stefan Nickl stefan.nickl at googlemail.com
Thu Jan 19 15:36:45 CET 2012


Hi all,

I also stumbled across the modbus segfault (collectd 5.0.1 / libmodbus
3.0.1 / gentoo, both i386 & x86_64).

The problem is that host->connection is simply never initialized in
mb_read_data() before being passed to libmodbus.
The only place mb_init_connection() is called is from within the
reconnection loop below.

Adding this before the call to modbus_set_slave() makes the crash go away:

  if (host->connection == NULL)
  {
    status = mb_init_connection (host);
    if (status != 0)
    {
      ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
          host->host, host->node);
      return (-1);
    }
  }

As I'm reading float values I also had to modify mb_register_to_float() to
get what I was expecting.
But not sure whether this is due to running on a 64bit machine or just my
PLC being backwards.
What I'm doing is

static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
{
  union
  {
    uint8_t b[4];
    uint16_t s[2];
    float f;
  } conv;

#if BYTE_ORDER == LITTLE_ENDIAN
  conv.s[0] = hi;
  conv.s[1] = lo;
#else
...

-- 
Cheers, Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.verplant.org/pipermail/collectd/attachments/20120119/8568d9a0/attachment.html>


More information about the collectd mailing list