Weird bug(?) regarding function calling

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Weird bug(?) regarding function calling

Post by Roujin »

Hey guys, I just managed to crash OpenTTD with my AI script. Of course, I did something wrong to cause this, but in any case afaik OpenTTD shouldn't crash but rather let the AI crash.

I removed part after part of my AI to narrow it down to this:

Code: Select all

import("queue.fibonacci_heap", "FibonacciHeap", 2);

class BugAI extends AIController
{
}

function BugAI::Start()
{
	local someitem = "hello";
	::TestStuff(someitem);
	
	while(true) {}
}

function TestStuff(item, prio, someother = 0)
{
	local queue = FibonacciHeap();
	
	queue.Insert(item, null);
//	queue.Insert(item, prio);
}
Yes, I'm using FibonacciHeap in there, but I think it's a bug in the framework itself, not in the FibonacciHeap library.
What happens here? Well you see, I'm calling the function ::TestStuff here with one parameter (someitem), whereas I didn't define such a function. I only defined one with three values, of which the last has a default value (important! Without this third parameter with default value, the API gives a proper error (wrong number of parameters) instead of crashing OpenTTD).

Now, the intresting stuff starts. Inside the called function, I assume that prio has now a (null) value. This would still be fine, but somehow it isn't: If I call the FibonacciHeap's Insert function with an explicit null as parameter for the priority, it doesn't complain (don't know what exactly it does then, though). However, feeding it with prio instead will crash OpenTTD. This leads me to the assumption that prio doesn't really contain (null), but something undefined or other.


I have not been able to produce a crash of OpenTTD without using the FibonacciHeap here, but I think it's a problem of the framework.
Or isn't it? Am I missing something? Please tell me, if so...
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage
User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: Weird bug(?) regarding function calling

Post by Dustin »

Roujin wrote: I have not been able to produce a crash of OpenTTD without using the FibonacciHeap here, but I think it's a problem of the framework.
Or isn't it? Am I missing something? Please tell me, if so...

You should certianly file a bug on http://bugs.openttd.org/. Pretty interesting crash bug. I am jealous.
Edit: If you wanted to narrow it down more, you could look into the fibonacciHeap lib function and probably get an even cleaner repro scenario.
Blustuff
Engineer
Engineer
Posts: 112
Joined: 21 Aug 2008 09:37
Location: France

Re: Weird bug(?) regarding function calling

Post by Blustuff »

It may be a bug in Squirrel rather than in OTTD. It is not a problem to call a function which have not been defined previously since function name lookup are done at run time. But it should be forbiden to call your function with only one argument. If you remove the third one you'll get :

Code: Select all

Your script made an error: wrong number of parameters
The third parameter is hence important as you stated before since it allows to call your function. The second parmeter may be null (this is what you get when you print it) but depending on squirrel implementation some initialization may have not be done.

However, Squirrel manual is very incomplete and there is not much information about function calls.
User avatar
Dustin
Transport Coordinator
Transport Coordinator
Posts: 272
Joined: 07 Dec 2005 19:22

Re: Weird bug(?) regarding function calling

Post by Dustin »

Blustuff wrote:It may be a bug in Squirrel rather than in OTTD. It is not a problem to call a function which have not been defined previously since function name lookup are done at run time. But it should be forbiden to call your function with only one argument. If you remove the third one you'll get :

Code: Select all

Your script made an error: wrong number of parameters
The third parameter is hence important as you stated before since it allows to call your function. The second parmeter may be null (this is what you get when you print it) but depending on squirrel implementation some initialization may have not be done.

However, Squirrel manual is very incomplete and there is not much information about function calls.
Squirrel may need the first arguement to be "this".
Blustuff
Engineer
Engineer
Posts: 112
Joined: 21 Aug 2008 09:37
Location: France

Re: Weird bug(?) regarding function calling

Post by Blustuff »

Dustin wrote:Squirrel may need the first arguement to be "this".
This is not a method and Squirrel have a particular way to bind an environnement to a method. (closure.bindenv(env_obj))
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Weird bug(?) regarding function calling

Post by Yexo »

Roujin wrote:Hey guys, I just managed to crash OpenTTD with my AI script. Of course, I did something wrong to cause this, but in any case afaik OpenTTD shouldn't crash but rather let the AI crash.
That's very true. Thanks for the clear report and simple test case. It was a bug in squirrel code which I fixed in r17273 and reported back to the squirrel developer.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 48 guests