Addenum to "Constify obj_dat.[ch]."
authorAndy Polyakov <appro@openssl.org>
Tue, 18 Sep 2007 22:15:31 +0000 (22:15 +0000)
committerAndy Polyakov <appro@openssl.org>
Tue, 18 Sep 2007 22:15:31 +0000 (22:15 +0000)
crypto/asn1/a_object.c
crypto/asn1/asn1.h
crypto/asn1/tasn_enc.c
crypto/objects/obj_lib.c

index 6a421a6..6e4ae3b 100644 (file)
@@ -287,6 +287,7 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
        {
        ASN1_OBJECT *ret=NULL;
        const unsigned char *p;
+       unsigned char *data;
        int i;
 
        /* only the ASN1_OBJECTs from the 'table' will have values
@@ -299,15 +300,22 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
        else    ret=(*a);
 
        p= *pp;
-       if ((ret->data == NULL) || (ret->length < len))
+       /* detach data from object */
+       data = (unsigned char *)ret->data;
+       ret->data = NULL;
+       /* once detached we can change it */
+       if ((data == NULL) || (ret->length < len))
                {
-               if (ret->data != NULL) OPENSSL_free(ret->data);
-               ret->data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
-               ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
-               if (ret->data == NULL)
+               ret->length=0;
+               if (data != NULL) OPENSSL_free(data);
+               data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
+               if (data == NULL)
                        { i=ERR_R_MALLOC_FAILURE; goto err; }
+               ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
                }
-       memcpy(ret->data,p,(int)len);
+       memcpy(data,p,(int)len);
+       /* reattach data to object, after which it remains const */
+       ret->data  =data;
        ret->length=(int)len;
        ret->sn=NULL;
        ret->ln=NULL;
@@ -356,7 +364,7 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a)
                }
        if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
                {
-               if (a->data != NULL) OPENSSL_free(a->data);
+               if (a->data != NULL) OPENSSL_free((void *)a->data);
                a->data=NULL;
                a->length=0;
                }
index eeb884f..1bf227d 100644 (file)
@@ -208,7 +208,7 @@ typedef struct asn1_object_st
        const char *sn,*ln;
        int nid;
        int length;
-       unsigned char *data;
+       const unsigned char *data;      /* data remains const after init */
        int flags;      /* Should we free this one */
        } ASN1_OBJECT;
 
index 28f6e42..03e59d5 100644 (file)
@@ -569,7 +569,8 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
        ASN1_STRING *strtmp;
        ASN1_OBJECT *otmp;
        int utype;
-       unsigned char *cont, c;
+       const unsigned char *cont;
+       unsigned char c;
        int len;
        const ASN1_PRIMITIVE_FUNCS *pf;
        pf = it->funcs;
index 706fa0b..23e9d48 100644 (file)
@@ -66,7 +66,8 @@ ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
        {
        ASN1_OBJECT *r;
        int i;
-       char *ln=NULL;
+       char *ln=NULL,*sn=NULL;
+       unsigned char *data=NULL;
 
        if (o == NULL) return(NULL);
        if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
@@ -79,42 +80,42 @@ ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
                OBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);
                return(NULL);
                }
-       r->data=OPENSSL_malloc(o->length);
-       if (r->data == NULL)
+       data=OPENSSL_malloc(o->length);
+       if (data == NULL)
                goto err;
        if (o->data != NULL)
-               memcpy(r->data,o->data,o->length);
+               memcpy(data,o->data,o->length);
+       /* once data attached to object it remains const */
+       r->data = data;
        r->length=o->length;
        r->nid=o->nid;
        r->ln=r->sn=NULL;
        if (o->ln != NULL)
                {
                i=strlen(o->ln)+1;
-               r->ln=ln=OPENSSL_malloc(i);
-               if (r->ln == NULL) goto err;
+               ln=OPENSSL_malloc(i);
+               if (ln == NULL) goto err;
                memcpy(ln,o->ln,i);
+               r->ln=ln;
                }
 
        if (o->sn != NULL)
                {
-               char *s;
-
                i=strlen(o->sn)+1;
-               r->sn=s=OPENSSL_malloc(i);
-               if (r->sn == NULL) goto err;
-               memcpy(s,o->sn,i);
+               sn=OPENSSL_malloc(i);
+               if (sn == NULL) goto err;
+               memcpy(sn,o->sn,i);
+               r->sn=sn;
                }
        r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|
                ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA);
        return(r);
 err:
        OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
-       if (r != NULL)
-               {
-               if (ln != NULL) OPENSSL_free(ln);
-               if (r->data != NULL) OPENSSL_free(r->data);
-               OPENSSL_free(r);
-               }
+       if (ln != NULL)         OPENSSL_free(ln);
+       if (sn != NULL)         OPENSSL_free(sn);
+       if (data != NULL)       OPENSSL_free(data);
+       if (r != NULL)          OPENSSL_free(r);
        return(NULL);
        }