Forum

> > CS2D > Scripts > Limit loops
Forums overviewCS2D overview Scripts overviewLog in to reply

English Limit loops

4 replies
To the start Previous 1 Next To the start

old Limit loops

Avo
User Off Offline

Quote
Let's load a string and make a function from it. If the string is user-made it can contain dangerous code like infinite loops or loops that will use much CPU and take much time to finish.

I'd like to avoid situations like that so I thought about editing the string before loading it from:
1
2
3
4
5
6
7
for i = 1, 100000000 do
	for i = 1, 100000000 do
		for i = 1, 100000000 do
			print "foo"
		end
	end
end
To something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local _time = os.time() 
for i = 1, 100000000 do
	if os.time() - _time >= 5 then
		break
	end 
	for i = 1, 100000000 do
		if os.time() - _time >= 5 then
			break
		end
		for i = 1, 100000000 do
			if os.time() - _time >= 5 then
				break
			end
			print "foo"
		end
	end
end

So, the main part - request. Provide me a function that will return an edited string that won't contain any code like in example no.1. It has to be like in the example no. 2. Code of a returned string have to abort all loops if the script takes more than 5 seconds to execute. Do not forget about other loops (while).

Or maybe users like Starkkz have some other ideas how to deal with stuff like that?

old Re: Limit loops

Starkkz
Moderator Off Offline

Quote
1
debug.sethook(function () error("tick quota exceeded") end, "", 5000)
5000 is the quota adjustment. (It doesn't mean 5000 milliseconds)
edited 1×, last 20.03.14 12:29:46 am

old Re: Limit loops

VADemon
User Off Offline

Quote
Separate threads (should have no impact on the mainthread)
Keyword: Sandboxing

old Re: Limit loops

Starkkz
Moderator Off Offline

Quote
@user VADemon: or either that, but you would have to run the code in a separate function environment (without CS2D functions). He could use my Lua lanes modification to have the CS2D functions but it crashes too much, you can't load that module twice in a program.

old Re: Limit loops

VADemon
User Off Offline

Quote
@user Starkkz: Then you either use lindas directly from the Lanes thread (which isn't that complicated, but a pain in the ass though) or you write a wrapper to manage cs2d functions in the child thread (using Lindas again under the hood).
The only thing I don't like here is that people could run scripts in separate threads editing one same value to the mainthread (eg Player1 health)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview