[ previous ] [ next ] [ threads ]
 
 From:  "=?ISO-8859-1?Q?R=F6nnblom_Jan=E5ke_?= /Teknous" <jan dash ake dot ronnblom at skeria dot skelleftea dot se>
 To:  m0n0wall at lists dot m0n0 dot ch
 Subject:  Re: [m0n0wall] problem with captiveportal - bug in for loop?
 Date:  Wed, 12 Dec 2007 11:35:39 +0100
Hi!

In the function captiveportal_prune_old() at somewhere near line 442 you find
this:

    for ($i = 0; $i < count($cpdb); $i++) {

and later on in the function if an account is logged off this code is run:

        if ($timedout) {
            captiveportal_disconnect($cpdb[$i],
$radiusservers,$term_cause,$stop_time);
            captiveportal_logportalauth($cpdb[$i][4], $cpdb[$i][3],
$cpdb[$i][2], "TIMEOUT");
            unset($cpdb[$i]);
        }

Here the unset() removes one row from the array and during the next iteration
where the for
loop again evalutes count() it is now less than the real size of the array
which causes the
for loop to prematurely exit and some of our accounts never gets logged off.

A fix would then be to write:

    $cnt = count($cpdb);
    for ($i = 0; $i < $cnt; $i++) {

I used this code as an proof of concept with regards to unset and count:

#!/usr/local/bin/php -f
<?php

$cpdb = array();

$cpdb[] = explode(",", "janron,192,a");
$cpdb[] = explode(",", "tesusr,228,b");

print "0 is {$cpdb[0][0]}\n";
print "1 is {$cpdb[1][0]}\n";

print "number of records in cpdb is " . count($cpdb) . "\n";

for ($i = 0; $i < count($cpdb); $i++)
{
	print "printing $i in {$cpdb[$i][0]}\n";
}

for($i = 0; $i < count($cpdb);$i++)
{
	print "unsetting with i = $i in {$cpdb[$i][0]}\n";
	unset($cpdb[$i]);
}

print "number of records in cpdb is " . count($cpdb) . "\n";

print "0 is {$cpdb[0][0]}\n";
print "1 is {$cpdb[1][0]}\n";


?>

Am I totally off track? What haven't I understood yet?

=====================================================


Assistentgatan 23
931 77 Skelleftea (Sweden)
-----------------------------------------------------
Phone  : +46-910-58 54 24
Mobile : 070-397 07 43
Fax    : +46-910-58 54 99
URL    : http://skeria.skelleftea.se
-----------------------------------------------------
"Those who do not understand Unix are condemned to reinvent it, poorly." --
Henry Spencer