From 0f30713c63053586b39af5d7e26773bf6faf7462 Mon Sep 17 00:00:00 2001
From: Hongli Lai (Phusion) <hongli@phusion.nl>
Date: Tue, 5 Jan 2010 10:50:15 +0100
Subject: [PATCH 2/3] Fix some various OpenSSL compilation and runtime warnings.

---
 ext/openssl/ossl_asn1.c     |   41 +++++++++++++++++++++--------------------
 ext/openssl/ossl_cipher.c   |    4 ++--
 ext/openssl/ossl_ssl.c      |    4 ++--
 ext/openssl/ossl_x509attr.c |    7 ++++---
 ext/openssl/ossl_x509ext.c  |    9 +++++++--
 5 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/ext/openssl/ossl_asn1.c b/ext/openssl/ossl_asn1.c
index 8ceea95..bac8366 100644
--- a/ext/openssl/ossl_asn1.c
+++ b/ext/openssl/ossl_asn1.c
@@ -304,10 +304,10 @@ obj_to_asn1derstr(VALUE obj)
  * DER to Ruby converters
  */
 static VALUE
-decode_bool(unsigned char* der, int length)
+decode_bool(OSSL_MORE_CONST unsigned char* der, int length)
 {
     int bool;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
 
     p = der;
     if((bool = d2i_ASN1_BOOLEAN(NULL, &p, length)) < 0)
@@ -317,10 +317,10 @@ decode_bool(unsigned char* der, int length)
 }
 
 static VALUE
-decode_int(unsigned char* der, int length)
+decode_int(OSSL_MORE_CONST unsigned char* der, int length)
 {
     ASN1_INTEGER *ai;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     VALUE ret; 
     int status = 0;
 
@@ -336,10 +336,11 @@ decode_int(unsigned char* der, int length)
 }
 
 static VALUE
-decode_bstr(unsigned char* der, int length, long *unused_bits)
+decode_bstr(OSSL_MORE_CONST unsigned char* der, int length, long *unused_bits)
 {
     ASN1_BIT_STRING *bstr;
-    unsigned char *p, *buf;
+    OSSL_MORE_CONST unsigned char *p;
+    unsigned char *buf;
     long len;
     VALUE ret;
 
@@ -362,10 +363,10 @@ decode_bstr(unsigned char* der, int length, long *unused_bits)
 }
 
 static VALUE
-decode_enum(unsigned char* der, int length)
+decode_enum(OSSL_MORE_CONST unsigned char* der, int length)
 {
     ASN1_ENUMERATED *ai;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     VALUE ret; 
     int status = 0;
 
@@ -381,10 +382,10 @@ decode_enum(unsigned char* der, int length)
 }
 
 static VALUE
-decode_null(unsigned char* der, int length)
+decode_null(OSSL_MORE_CONST unsigned char* der, int length)
 {
     ASN1_NULL *null;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
 
     p = der;
     if(!(null = d2i_ASN1_NULL(NULL, &p, length)))
@@ -395,10 +396,10 @@ decode_null(unsigned char* der, int length)
 }
 
 static VALUE
-decode_obj(unsigned char* der, int length)
+decode_obj(OSSL_MORE_CONST unsigned char* der, int length)
 {
     ASN1_OBJECT *obj;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     VALUE ret;
     int nid;
     BIO *bio;
@@ -424,10 +425,10 @@ decode_obj(unsigned char* der, int length)
 }
 
 static VALUE
-decode_time(unsigned char* der, int length)
+decode_time(OSSL_MORE_CONST unsigned char* der, int length)
 {
     ASN1_TIME *time;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     VALUE ret;
     int status = 0;
 
@@ -712,10 +713,10 @@ ossl_asn1data_to_der(VALUE self)
 }
 
 static VALUE
-ossl_asn1_decode0(unsigned char **pp, long length, long *offset, long depth,
-		  int once, int yield)
+ossl_asn1_decode0(OSSL_MORE_CONST unsigned char **pp, long length,
+		  long *offset, long depth, int once, int yield)
 {
-    unsigned char *start, *p;
+    OSSL_MORE_CONST unsigned char *start, *p;
     long len, off = *offset;
     int hlen, tag, tc, j;
     VALUE ary, asn1data, value, tag_class;
@@ -818,7 +819,7 @@ ossl_asn1_decode0(unsigned char **pp, long length, long *offset, long depth,
 static VALUE
 ossl_asn1_traverse(VALUE self, VALUE obj)
 {
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     long offset = 0;
     volatile VALUE tmp;
 
@@ -834,7 +835,7 @@ static VALUE
 ossl_asn1_decode(VALUE self, VALUE obj)
 {
     VALUE ret, ary;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     long offset = 0;
     volatile VALUE tmp;
 
@@ -851,7 +852,7 @@ static VALUE
 ossl_asn1_decode_all(VALUE self, VALUE obj)
 {
     VALUE ret;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     long offset = 0;
     volatile VALUE tmp;
 
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index b680dc6..fdb1cc8 100644
--- a/ext/openssl/ossl_cipher.c
+++ b/ext/openssl/ossl_cipher.c
@@ -186,7 +186,7 @@ ossl_cipher_init(int argc, VALUE *argv, VALUE self, int mode)
 	 * We deprecated the arguments for this method, but we decided
 	 * keeping this behaviour for backward compatibility.
 	 */
-	char *cname  = rb_class2name(rb_obj_class(self));
+	const char *cname  = rb_class2name(rb_obj_class(self));
 	rb_warn("argumtents for %s#encrypt and %s#decrypt were deprecated; "
                 "use %s#pkcs5_keyivgen to derive key and IV",
                 cname, cname, cname);
@@ -307,7 +307,7 @@ ossl_cipher_pkcs5_keyivgen(int argc, VALUE *argv, VALUE self)
 static VALUE
 ossl_cipher_update_deprecated(VALUE self, VALUE data)
 {
-    char *cname;
+    const char *cname;
 
     cname = rb_class2name(rb_obj_class(self));
     rb_warning("%s#<< is deprecated; use %s#update instead", cname, cname);
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
index a13add5..26f9963 100644
--- a/ext/openssl/ossl_ssl.c
+++ b/ext/openssl/ossl_ssl.c
@@ -1196,10 +1196,10 @@ ossl_ssl_get_peer_cert_chain(VALUE self)
     }
     chain = SSL_get_peer_cert_chain(ssl);
     if(!chain) return Qnil;
-    num = sk_num(chain);
+    num = sk_x509_num(chain);
     ary = rb_ary_new2(num);
     for (i = 0; i < num; i++){
-	cert = (X509*)sk_value(chain, i);
+	cert = (X509*)sk_x509_value(chain, i);
 	rb_ary_push(ary, ossl_x509_new(cert));
     }
 
diff --git a/ext/openssl/ossl_x509attr.c b/ext/openssl/ossl_x509attr.c
index ca1c59a..b177242 100644
--- a/ext/openssl/ossl_x509attr.c
+++ b/ext/openssl/ossl_x509attr.c
@@ -93,7 +93,7 @@ ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
 {
     VALUE oid, value;
     X509_ATTRIBUTE *attr;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
 
     GetX509Attr(self, attr);
     if(rb_scan_args(argc, argv, "11", &oid, &value) == 1){
@@ -217,8 +217,9 @@ ossl_x509attr_get_value(VALUE self)
 	ossl_str_adjust(str, p);
     }
     else{
-	length = i2d_ASN1_SET_OF_ASN1_TYPE(attr->value.set, NULL,
-			i2d_ASN1_TYPE, V_ASN1_SET, V_ASN1_UNIVERSAL, 0);
+	length = i2d_ASN1_SET_OF_ASN1_TYPE(attr->value.set,
+			(unsigned char **) NULL, i2d_ASN1_TYPE,
+			V_ASN1_SET, V_ASN1_UNIVERSAL, 0);
 	str = rb_str_new(0, length);
 	p = RSTRING_PTR(str);
 	i2d_ASN1_SET_OF_ASN1_TYPE(attr->value.set, &p,
diff --git a/ext/openssl/ossl_x509ext.c b/ext/openssl/ossl_x509ext.c
index aa9366f..b7b2df3 100644
--- a/ext/openssl/ossl_x509ext.c
+++ b/ext/openssl/ossl_x509ext.c
@@ -220,6 +220,7 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
 #ifdef HAVE_X509V3_EXT_NCONF_NID
     VALUE rconf;
     CONF *conf;
+    ID i_config;
 #else
     static LHASH *empty_lhash;
 #endif
@@ -236,7 +237,11 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
     rb_str_append(valstr, value);
     GetX509ExtFactory(self, ctx);
 #ifdef HAVE_X509V3_EXT_NCONF_NID
-    rconf = rb_iv_get(self, "@config");
+    i_config = rb_intern("@config");
+    if (rb_ivar_defined(self, i_config))
+	rconf = rb_ivar_get(self, i_config);
+    else
+	rconf = Qnil;
     conf = NIL_P(rconf) ? NULL : GetConfigPtr(rconf);
     ext = X509V3_EXT_nconf_nid(conf, ctx, nid, RSTRING_PTR(valstr));
 #else
@@ -273,7 +278,7 @@ static VALUE
 ossl_x509ext_initialize(int argc, VALUE *argv, VALUE self)
 {
     VALUE oid, value, critical;
-    unsigned char *p;
+    OSSL_MORE_CONST unsigned char *p;
     X509_EXTENSION *ext;
 
     GetX509Ext(self, ext);
-- 
1.6.6

