Either I'm going mad or I found a PHP bug

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Either I'm going mad or I found a PHP bug
« on July 8th, 2011, 12:58 AM »
I don't want to get into the details, suffice to say I had need to write a SimpleDesk plugin today, and one of the things it does is add a new area to the admin panel, through $config_vars being passed to prepareDBSettingsContext. Nothing wrong there.

Then, after I finish setting up the array and passing it through to that function, I get a Warning: Invalid argument supplied for foreach() in C:\Dev\public_html\intranet\smf_2-0_install\Sources\ManageServer.php on line 1824.

This is a bit strange, but sure enough, line 1824:
Code: [Select]
foreach ($config_vars as $config_var)

Now, there's nothing happening between the start of prepareDBSettingsContext and that line that touches $config_var. And if I do a var_dump, I find there's a random NULL at the end of the var-dump that appears to be breaking it. Now, I'm confused because I have no idea where that NULL is coming from.

Here's the kicker: if I var_dump BEFORE the call, I get a normal var_dump and the array is normally traversable, so something about the call itself causes it to go nuts. It's not even the fact it's a reference, I reverted it to a normal variable by value and the same thing happens.

It's not like there's anything else going on - this variable is passed directly to the destination function and nothing hokey is going on in the middle.

So I'm really, really confused, and wondering if it's a bug. Maybe I should try upgrading (currently on 5.2.13 on local PC)
Posted: July 8th, 2011, 12:46 AM

In other news, I am actually going mad, ignore me. Stupid bug, having spent an hour of frustrated debugging, because I forgot something quite important...
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

snoopy-virtual

  • Posts: 152
Re: Either I'm going mad or I found a PHP bug
« Reply #1, on July 8th, 2011, 01:09 AM »
Funny enough I saw a similar problem a year ago. I had in one of my mods an array of members suspicious of being spammers and I had to do some process with them to be sure if they were spammers or not.

The array of members to process was called $members_data and I had in the code:

Code: [Select]
foreach ($members_data as $row)
{
(do some process)
}

After the foreach had processed all the members that were supposed to be there, it was also processing another one with NULL values.

I was ages trying to find out where this last NULL member had come from and couldn't find it, so at the end I assumed I had made a mistake somewhere else loading the members into the $members_data array and just added this to sort it:

Code: [Select]
foreach ($members_data as $row)
{
if (!empty($row['id_member']))
{
(do some process)
}
}

That of course sorted the problem because it was not precessing the NULL member, but I have been always going back to that problem trying to sort it properly (finding where the NULL member had coming from in the first place).

I never thought it could be a bug in PHP.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Either I'm going mad or I found a PHP bug
« Reply #2, on July 8th, 2011, 01:13 AM »
In my case it was even more stupid. Since this was extending SimpleDesk's admin area, it was going through the existing handler - which already calls prepareDBSettingsContext. Because my new function wasn't returning $config_vars back up the chain like it was supposed to, the second time we hit the function, it was null.

It was the var_dump that threw me, I thought I was seeing NULL added to the end, not a *second* call to the function.

Nao

  • Dadman with a boy
  • Posts: 16,078

Eros

  • I has a thought. Be afraid.
  • Posts: 56
Re: Either I'm going mad or I found a PHP bug
« Reply #4, on July 8th, 2011, 03:06 AM »
I think that happens to all of us from time to time tho. I know I've missed stupid things that were blindingly obvious after I came back the next day. And I was like 'Oh shit, I've got to re-do that section because I missed X'. :/

Its probably the only reason pair programming can ever make sense, if you are on a tight time constraint. Those sort of mistakes tend to get caught by the other person.
18+ Roleplay Forum <- hopefully running Wedge when it is ready.

Yaoi RP Forum <- hopefully running Wedge when it is ready.