How to check if eval() changes values of any local variables?
Show older comments
Dear all,
I have a GUI that lets users to evaluate custom scripts, mostly problem oriented functions. The scripts for running need to create some temporary variables and I would like to make sure that they don’t mess up the local workspace. Is there any clean and neat way to do?
Thank you for your help, Kirill Andreev
Answers (3)
Walter Roberson
on 16 Jan 2012
1 vote
No, it is always possible to escape from eval() and do arbitrary things. What-ever mechanisms MathWorks uses for the Contents are not available to users.
If you want security, do not use eval() on user input without having proven the input to be harmless (which is generally a tough task.)
Sean de Wolski
on 16 Jan 2012
0 votes
Package their script into a function (using fopen/fwrite/fprintf so that it uses its own local workspace. Call the function.
Waallaa! No eval and no poofing
3 Comments
Walter Roberson
on 16 Jan 2012
Until the user writes
assignin('caller', 'OneOfYourVariables', SomeRandomValue)
Also, unless you use a different function name each time, be sure to "clear" the function name after you generate the new .m file or Strange Things Will Happen.
Kirill Andreev
on 16 Jan 2012
Sean de Wolski
on 16 Jan 2012
Rather than saving them to disk you could set them to appdata, which will be MUCH faster:
doc setappdata/doc getappdata
Jan
on 16 Jan 2012
0 votes
You can shadow assignin by creating an own function with the same name. Then you can catch the 'base' and 'caller' argument and collect all changes separately from the actual workspace.
As long as the users can call eval, strange this will happen - promissed! Somebody will create a variable called 'load' and you will not be able to load the variable dump any longer.
2 Comments
Walter Roberson
on 16 Jan 2012
And then the user will create a variable named "builtin"...
Kirill Andreev
on 16 Jan 2012
Categories
Find more on Controller Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!