ruby-changes:18766
From: nobu <ko1@a...>
Date: Sat, 5 Feb 2011 11:56:59 +0900 (JST)
Subject: [ruby-changes:18766] Ruby:r30793 (trunk): * ext/openssl/ossl_cipher.c (ossl_cipher_alloc): leave data ptr
nobu 2011-02-05 11:48:55 +0900 (Sat, 05 Feb 2011) New Revision: 30793 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30793 Log: * ext/openssl/ossl_cipher.c (ossl_cipher_alloc): leave data ptr NULL. * ext/openssl/ossl_cipher.c (ossl_cipher_new, ossl_cipher_initialize): allocate internal structure. [ruby-core:35094] * ext/openssl/ossl_cipher.c (ossl_cipher_copy): ditto. Modified files: trunk/ChangeLog trunk/ext/openssl/ossl_cipher.c trunk/test/openssl/test_cipher.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 30792) +++ ChangeLog (revision 30793) @@ -1,3 +1,13 @@ +Sat Feb 5 11:48:47 2011 Nobuyoshi Nakada <nobu@r...> + + * ext/openssl/ossl_cipher.c (ossl_cipher_alloc): leave data ptr + NULL. + + * ext/openssl/ossl_cipher.c (ossl_cipher_new, ossl_cipher_initialize): + allocate internal structure. [ruby-core:35094] + + * ext/openssl/ossl_cipher.c (ossl_cipher_copy): ditto. + Sat Feb 5 11:29:10 2011 Nobuyoshi Nakada <nobu@r...> * ext/json/parser/parser.h (GET_PARSER): raise TypeError. Index: ext/openssl/ossl_cipher.c =================================================================== --- ext/openssl/ossl_cipher.c (revision 30792) +++ ext/openssl/ossl_cipher.c (revision 30793) @@ -10,10 +10,17 @@ */ #include "ossl.h" +#define WrapCipher(obj, klass, ctx) \ + obj = Data_Wrap_Struct(klass, 0, ossl_cipher_free, ctx) #define MakeCipher(obj, klass, ctx) \ obj = Data_Make_Struct(klass, EVP_CIPHER_CTX, 0, ossl_cipher_free, ctx) +#define AllocCipher(obj, ctx) \ + memset(DATA_PTR(obj) = (ctx) = ALLOC(EVP_CIPHER_CTX), 0, sizeof(EVP_CIPHER_CTX)) +#define GetCipherInit(obj, ctx) do { \ + Data_Get_Struct(obj, EVP_CIPHER_CTX, ctx); \ +} while (0) #define GetCipher(obj, ctx) do { \ - Data_Get_Struct(obj, EVP_CIPHER_CTX, ctx); \ + GetCipherInit(obj, ctx); \ if (!ctx) { \ ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \ } \ @@ -51,7 +58,7 @@ EVP_CIPHER_CTX *ctx; ret = ossl_cipher_alloc(cCipher); - GetCipher(ret, ctx); + AllocCipher(ret, ctx); EVP_CIPHER_CTX_init(ctx); if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) ossl_raise(eCipherError, NULL); @@ -74,11 +81,9 @@ static VALUE ossl_cipher_alloc(VALUE klass) { - EVP_CIPHER_CTX *ctx; VALUE obj; - MakeCipher(obj, klass, ctx); - EVP_CIPHER_CTX_init(ctx); + WrapCipher(obj, klass, 0); return obj; } @@ -99,7 +104,12 @@ char *name; name = StringValuePtr(str); - GetCipher(self, ctx); + GetCipherInit(self, ctx); + if (ctx) { + ossl_raise(rb_eRuntimeError, "Cipher already inititalized!"); + } + AllocCipher(self, ctx); + EVP_CIPHER_CTX_init(ctx); if (!(cipher = EVP_get_cipherbyname(name))) { ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name); } @@ -116,7 +126,10 @@ rb_check_frozen(self); if (self == other) return self; - GetCipher(self, ctx1); + GetCipherInit(self, ctx1); + if (!ctx1) { + AllocCipher(self, ctx1); + } SafeGetCipher(other, ctx2); if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1) ossl_raise(eCipherError, NULL); Index: test/openssl/test_cipher.rb =================================================================== --- test/openssl/test_cipher.rb (revision 30792) +++ test/openssl/test_cipher.rb (revision 30793) @@ -64,6 +64,11 @@ assert_raise(ArgumentError){ @c1.update("") } end + def test_initialize + assert_raise(RuntimeError) {@c1.__send__(:initialize, "DES-EDE3-CBC")} + assert_raise(RuntimeError) {OpenSSL::Cipher.allocate.final} + end + if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00907000 def test_ciphers OpenSSL::Cipher.ciphers.each{|name| -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/