Forum: Ruby Safe Mode Lowering

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-04-26 00:30
(Received via mailing list)
I am aware that the safe mode in Ruby can only be set higher, not
lower. This makes sense, as unsafe code should not be able to lower
the safe mode. However, is there a way to lower it at all, as I can
think of many times this may be useful. Would it not make a lot more
sense to be able to do something like this?

safe_mode(x) do # x must be greater than the current safe mode
   # do risky code here
end
# back to original safe mode

Maybe the above is possible and I am just missing how, but I would
think that it would be much more useful to have in a form similar to
this all the time.

- Jake McArthur
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-04-26 00:39
(Received via mailing list)
Jake McArthur wrote:
>
> Maybe the above is possible and I am just missing how, but I would think
> that it would be much more useful to have in a form similar to this all
> the time.
>
> - Jake McArthur

If you change $SAFE in a thread, it affects only that thread.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-26 00:39
(Received via mailing list)
On Wed, 26 Apr 2006, Jake McArthur wrote:

>
> Maybe the above is possible and I am just missing how, but I would think that
> it would be much more useful to have in a form similar to this all the time.
>
> - Jake McArthur

you have

   fork {
     $SAFE = 4
     ...
   }
   Process.wait

and also

   t = Thread.new{
     $SAFE = 4
     ...
   }
   t.value

fyi.

-a
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-04-26 00:45
(Received via mailing list)
On Apr 26, 2006, at 12:27 AM, Jake McArthur wrote:

> Maybe the above is possible and I am just missing how, but I would
> think that it would be much more useful to have in a form similar
> to this all the time.

module Kernel
   def with_safe(mode)
     Thread.new {
       $SAFE = mode
       yield
     }.join
   end
end

with_safe(3) do
   eval("1+2")
end

$SAFE, along with some other global variables, is thread local.

-- Daniel
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-04-26 00:48
(Received via mailing list)
I have even used that technique before. I'm dumb. So, something like
this would probably work for the type of abstraction I'm thinking of
(too lazy for an irb test).

def safe_block(mode)
   t = Thread.new do
     @SAFE = mode
     yield
   end
   t.value
end

- Jake McArthur
8d16869783573d7ca80a676b65cf98e7?d=identicon&s=25 David Pollak (Guest)
on 2006-04-26 01:04
(Received via mailing list)
Jake,

Here's code that will do what you want:

module SafeDo
  require 'monitor'

  def safe_do(safe_level)
    ret = nil
    waiter = ''
    waiter.extend(MonitorMixin)
    wait_cond = waiter.new_cond
    is_exception = false
    is_done = false

    Thread.start do
      begin
        $SAFE = safe_level

        ret = yield
        is_done = true
        waiter.synchronize do
          wait_cond.signal
        end
      rescue Exception => e
        ret = e
        is_exception = true
        is_done = true
        waiter.synchronize do
          wait_cond.signal
        end

      end
    end

    waiter.synchronize do
      wait_cond.wait_while { !is_done }
    end

    raise ret if is_exception

    return ret
  end
end

Thanks,

David
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-04-26 02:45
(Received via mailing list)
On Apr 25, 2006, at 4:02 PM, David Pollak wrote:

> Jake,
>
> Here's code that will do what you want:

You implemented Thread#value.

$ ruby
t = Thread.new { raise }
t.value
p 'can\'t do it'
-:1: unhandled exception
         from -:2:in `value'
         from -:2

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
8d16869783573d7ca80a676b65cf98e7?d=identicon&s=25 David Pollak (Guest)
on 2006-04-26 03:59
(Received via mailing list)
Eric,

Thanks for the correction.  I'll update the code to:

module SafeDo
  def safe_do(safe_level)
    (Thread.new {$SAFE = safe_level ; yield } ).value
  end
end

David
This topic is locked and can not be replied to.