From: pjb@... (Pascal J. Bourguignon) Date: 2009-02-07T20:14:16+09:00 Subject: Re: eval statement Christopher Dicely writes: > On 2/6/09, Pascal J. Bourguignon wrote: >> badboy writes: >> >>> Jonathan Wills schrieb: >>> why do you want to use eval? >>> eval is evil and in most cases not needed in Ruby >> >> Why are you saying that eval is evil? >> >> In know that in Common Lisp, EVAL has properties that make its use >> dubious in most cases. Mostly it's because it works in the global >> environment. >> >> But this is not the case of eval in Ruby. Since there's no compiler, >> they can execute eval in the local lexical environment. So is there >> remaining any evilness I don't know? > > Without taking additional special care, restricting eval to the to a > particular binding doesn't contain it very much. eval can, unless > something is done to stop it, get access to every Object in the top > level environment. > > For example try running this in irb: > > hash = Hash.new > object = Object.new > def object.get_binding > binding > end > b = object.get_binding > eval('ObjectSpace.each_object {|o| next if o.frozen?; begin def > o.inspect; "pwned"; end; rescue TypeError; end}',a) > hash #=>pwned > > If you run untrusted code in eval without being extra careful, it can > reach out of the binding its in and stomp over other objects, even > outside of that binding. Ok. In CL we say EVAL is evil because it cannot do some things, and in Ruby you say it's evil because it can. How interesting... Of couse, when you execute dynamic code, you must trust the source of that code. This doesn't "evilize" the mechanism to execute it. -- __Pascal Bourguignon__