Rescue'ing OpenSSL errors

Hi! I'm having some issues with the OpenSSL library:

I'm trying to rescue whatever errors the library wants to throw, however, it's not working:

def encrypt
     require 'openssl'
     begin
       aes = OpenSSL::Cipher::Cipher.new('AES-128-CBC')
       aes.encrypt('SOME_KEY')
       cipher = aes.update(password)
       cipher << aes.final
       return cipher
     rescue
       return false
     end
end

Now I go into irb:

require 'encrypt'

encrypt('test') # -> gives encrypted output
encrypt('') # -> gives error "evp_enc.c(332): OpenSSL internal error, assertion failed: inl > 0 \n Abort trap"

Seems like I can't rescue that because the error comes from a C library. Is that right?

Are there any solutions to this, except, for example, checking that "not password.empty?" ?

Thanks, Rob

By default, rescue only catches exceptions that are derived from
StandardError (not Exception). It's possible that OpenSSL is raising
an exception derived directly from Exception.

Try using "rescue Exception" instead of just "rescue".

regards,
Ed

···

On Wed, Oct 19, 2005 at 07:11:12AM +0900, Robert wrote:

Seems like I can't rescue that because the error comes from a C
library. Is that right?

Hello Edward,

thanks about pointing that out. Another thing learned :slight_smile:

However, it does not work. It still throws that same error (and also quits irb).

Anyway, for me that's not a problem anymore, because I just added:
       raise ArgumentError if password.empty?
to my script. But I'm still curious why a "rescue Exception" doesn't work here -- if anyone knows, that'd be cool :slight_smile:

roob

Your clue is the last part of the error. It appears as if the OpenSSL
library has generated a SIGABRT. Try this:

require 'encrypt'
trap( "ABRT") { puts "abort trapped... dumping out" }
encrypt( 'test')
encrypt( '')

See if that works.

···

On Wed, Oct 19, 2005 at 07:58:50AM +0900, Robert wrote:

Hello Edward,

thanks about pointing that out. Another thing learned :slight_smile:

However, it does not work. It still throws that same error (and also
quits irb).

Anyway, for me that's not a problem anymore, because I just added:
      raise ArgumentError if password.empty?
to my script. But I'm still curious why a "rescue Exception" doesn't
work here -- if anyone knows, that'd be cool :slight_smile:

--
Toby DiPasquale