DEP8 error on armhf (32bits): long2ip() requires int

Bug #1865067 reported by Andreas Hasenack
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cacti
Fix Released
Unknown
cacti (Ubuntu)
Fix Released
Undecided
Andreas Hasenack

Bug Description

Starting with php 7.1 (or so I'm told), long2ip() requires an integer. Turns out that on 32bit a number bigger than MAXINT is a "float" (?):

$ cat long2ip.php
<?php
echo "PHP_INT_MAX = " . PHP_INT_MAX . "\n";
echo "long2ip(PHP_INT_MAX) = " . long2ip(PHP_INT_MAX) . "\n";
echo "long2ip(PHP_INT_MAX + 1) = " . long2ip(PHP_INT_MAX + 1) . "\n";
echo "long2ip(-1) = " . long2ip(-1) . "\n";
?>

$ php ./long2ip.php
PHP_INT_MAX = 2147483647
long2ip(PHP_INT_MAX) = 127.255.255.255
PHP Warning: long2ip() expects parameter 1 to be int, float given in /home/ubuntu/long2ip.php on line 4
long2ip(PHP_INT_MAX + 1) =
long2ip(-1) = 255.255.255.255

$ uname -a
Linux f1-armhf 5.4.0-14-generic #17-Ubuntu SMP Thu Feb 6 22:49:16 UTC 2020 armv8l armv8l armv8l GNU/Linux

This seems to be happening in the cacti dep8 tests:

02/27/2020 12:55:07 - ERROR PHP WARNING: long2ip() expects parameter 1 to be int, float given in file: /usr/share/cacti/site/lib/api_automation.php on line: 3006
02/27/2020 12:55:07 - CMDPHP PHP ERROR WARNING Backtrace: (/poller_automation.php[272]:automation_primeIPAddressTable(), /lib/api_automation.php[3179]:automation_calculate_total_ips(), /lib/api_automation.php[3128]:automation_get_network_info(), /lib/api_automation.php[3034]:automation_get_valid_mask(), /lib/api_automation.php[3006]:long2ip(), CactiErrorHandler())
02/27/2020 12:55:07 - ERROR PHP WARNING: long2ip() expects parameter 1 to be int, float given in file: /usr/share/cacti/site/lib/api_automation.php on line: 3006
02/27/2020 12:55:07 - CMDPHP PHP ERROR WARNING Backtrace: (/poller_automation.php[272]:automation_primeIPAddressTable(), /lib/api_automation.php[3182]:automation_calculate_start(), /lib/api_automation.php[3116]:automation_get_network_info(), /lib/api_automation.php[3034]:automation_get_valid_mask(), /lib/api_automation.php[3006]:long2ip(), CactiErrorHandler())

I'm not sure what value long2ip() is getting. I'm assuming it's a random network, and sometimes (most of the time?) it's larger than MAXINT. Or some other garbage, and long2ip() barfs.

This test hasn't always failed, hence my assumption above. I'll try to add some debugging to see what is being passed to that function.

Related branches

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I believe the bug is in lib/api_automation.php's automation_get_valid_mask($range) function.

It uses bindec() to convert binary to integer, but the result of bindec() is unsigned integer, or float if it's too big for an integer[1]. On 32bit turns out it's too big for an integer, a float is returned, and things break.

On 64bit:
 php ./test.php 24

bindec(11111111111111111111111100000000):
4294967040
Array
(
    [cidr] => 24
    [subnet] => 255.255.255.0
    [count] => 255
)

Same code on 32bits:
ubuntu@f1-armhf:~$ php ./test.php 24

bindec(11111111111111111111111100000000):
4294967040PHP Warning: long2ip() expects parameter 1 to be int, float given in /home/ubuntu/test.php on line 13

Array
(
    [cidr] => 24
    [subnet] =>
    [count] => 255
)

1. https://www.php.net/manual/en/function.bindec.php

Revision history for this message
Andreas Hasenack (ahasenack) wrote :

https://www.php.net/manual/en/function.long2ip.php has a note even:
"""
On 32-bit architectures, casting integer representations of IP addresses from string to integer will not give correct results for numbers which exceed PHP_INT_MAX.
"""

On armhf, PHP_INT_MAX is 2147483647

Changed in cacti (Ubuntu):
assignee: nobody → Andreas Hasenack (ahasenack)
status: New → In Progress
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package cacti - 1.2.9+ds1-1ubuntu2

---------------
cacti (1.2.9+ds1-1ubuntu2) focal; urgency=medium

  * d/p/fix-32bit-ip-conversion.patch: fix netmask generation on 32bit
    architectures (LP: #1865067)

 -- Andreas Hasenack <email address hidden> Mon, 02 Mar 2020 14:52:15 -0300

Changed in cacti (Ubuntu):
status: In Progress → Fix Released
Changed in cacti:
status: Unknown → Fix Released
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.