Cactiusers.org


It is currently Thu May 17, 2012 7:49 am




Post new topic Reply to topic  [ 5 posts ] 
 Variable corrupted when set to a function of itself 
Author Message
New User

Joined: Thu Jun 14, 2007 9:33 am
Posts: 10
Post Variable corrupted when set to a function of itself
Hi Jimmy,

I had a problem with the last two versions of the thold plugin where trying to add thold to hosts by selecting them in /cacti/host.php and then choosing the "Apply Thresholds" dropdown option wasn't working. I debugged the script and realized that the $save hash (is it called that in php?) was becoming corrupted before making it to the function thold_device_action_prepare().

A bit more fiddling and it would seem that my implementation here (I am wondering if this is a bug in php rather than your code) cannot deal with assigning a variable to a function of itself. This line from the cacti/include/plugins.php (do_hook_function) seemed to have caused the problem.

Code:
                $ret = $function($ret);


I changed it to...

Code:
                $retr = $function($ret);


and also changed the return to match at the bottom of this function.

So is this a bug in the plugin arch or should I be taking this up with the php devs? If it is a bug in php can this be added to the next version of the plugin arch in any case to workaround the problem?

Thanks

iain


Thu Jan 10, 2008 11:28 am
New User

Joined: Thu Jun 14, 2007 9:33 am
Posts: 10
Post 
ok seems my fix broke the drop down in other pages (eg. graphs.php) so I had to add a...

Code:
$retr = $ret;


just underneath the function definition for times when we don't go through the foreach..

function look like this atm

Code:
function do_hook_function($name,$parm=NULL) {
    global $plugin_hooks;
    $ret = $parm;
    $retr = $ret;

    if (isset($plugin_hooks[$name])
          && is_array($plugin_hooks[$name])) {
        foreach ($plugin_hooks[$name] as $function) {
            if (function_exists($function)) {
                $retr = $function($ret);
            }
        }
    }

    /* Variable-length argument lists have a slight problem when */
    /* passing values by reference. Pity. This is a workaround.  */
    return $retr;
}


Hope this helps.

Thanks

iain


Thu Jan 10, 2008 1:04 pm
Site Admin

Joined: Thu Nov 11, 2004 11:12 am
Posts: 2007
Post 
That would break how other things work and would only truly return the last plugins results. Instead there is more than likely a plugin that is using the hook incorrectly, narrow it down to that plugin and I can fix it for you. Breaking the api's only temporarily solve the issue.


Mon Feb 04, 2008 12:42 am
New User

Joined: Thu Jun 14, 2007 9:33 am
Posts: 10
Post 
Hi Jimmy,

Ok the original problem was that the "Apply Thresholds" dropdown option in hosts.php. Before fiddling with this if I select a device by its check box then select "Apply Thresholds" in the dropdown at the bottom of the page and click "GO" the next page that comes up did not show a list of the devices for which tholds would be enabled. Clicking ok on this next page did not create any new tholds (I assume since the list of what to do with the tholds was blank)

I added some print statements into thold_device_action_prepare() to see what was going on and checked the function itself.

The contents of $save did not appear to be what it should have been. This test was not matching the entry 'thold' which it should if doing an "Apply Tholds" in host.php.

Code:
if ($save['drp_action'] != 'thold')
return $save;


as the contents of $save['drp_action'] was coming out as just 't' rather than 'thold'.

Furthermore $save['host_list'] also had a value of 't'

I traced this back into the cacti code and the variables where being filled out and passed into do_hook_function correctly but when they popped out the other side of this function the variables seemed to have become corrupted.

Adding an extra variable into do_hook_function so that $ret is not set based on a function of itself stopped the corruption of the variable.

So why does this break the arch by adding in an extra holder variable to this function?

Thanks

iain


Wed Feb 13, 2008 11:58 am
Site Admin

Joined: Thu Nov 11, 2004 11:12 am
Posts: 2007
Post 
savagemindz wrote:
So why does this break the arch by adding in an extra holder variable to this function?


It may work on this particular hooks, but other hooks require the changes to be passed from one plugin to another. That change would break this as it would send the original value to each plugin that uses the hook.

Did you disable all other plugins while doing your testing?


Sat Mar 22, 2008 9:11 pm
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: