Wiremod style tick quota.

I recently posted about a way to stop infinite loops but despite help from others it seemed like the best option was to re purpose E2 to my needs, I have one last idea that might save me.

So I’m looking to limit the amount of times something can loop within a function, I have a system setup where a player can type in code that is run (clientside and sandboxed), they have functions such as Screen:WriteLine; which provides them with the ability to write onto a 2D3D screen, this is all well and good but due to the fact that it is actually a lua environment, users can also use loops such as for loops or while loops; my plan is to limit the amount of loops that can be run so they are not infinite or resource hogging.

Wiremod has a system setup where if a script contains a loop that is running too much it will stop and provide the error of tick quota reached.

Does anyone know of a way I can LIMIT the amount of times a loop can be run in a function, or at least search in the function to see if a loop is going over a certain amount

Thanks a lot.

So how many times have you seen this “while true do end” loop being abused on a regular basis? Provide some insight for a real reason, yknow, some evidence.

The system hasn’t been used by other players yet, I’m just pre-planning for what I know could happen: if there is a chance that a player can crash all the clients with a line of code; chances are at least one will.

As everyone says: never trust the client / player, always expect they are up to no good.

It’s the most basic of infinite loops, you really don’t need to provide evidence that some cockhole is going to try and run it if you allow arbitrary execution of code as it’s a given some cockhole will try and run the simplest ~l33t hax~ they can find online.

It’s better to plan in advance and anticipate malicious behaviour than implement solutions reactively. Otherwise the damage will already be done.

Exactly, thanks Hexpunk. I think I’ve actually found a solution:

I have to thank kikito for this one, I don’t think he probably knows what gmod is, but the sandbox code he provided seems to work really well. I’m going to get to rewriting it now, thanks everyone for the help.