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 6a421a66a639c428da810779b6b95d977ca37fc4..6e4ae3b0730a89790482ca1744e3f0fcc8102fd1 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;
        {
        ASN1_OBJECT *ret=NULL;
        const unsigned char *p;
+       unsigned char *data;
        int i;
 
        /* only the ASN1_OBJECTs from the 'table' will have values
        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;
        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; }
                        { 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;
        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->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;
                }
                a->data=NULL;
                a->length=0;
                }
index eeb884fc3d47053c6bb5df92893d215cb8fb364a..1bf227d27f2b459dae70c7005f214eebcd0396f8 100644 (file)
@@ -208,7 +208,7 @@ typedef struct asn1_object_st
        const char *sn,*ln;
        int nid;
        int length;
        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;
 
        int flags;      /* Should we free this one */
        } ASN1_OBJECT;
 
index 28f6e42521361f16029c9e6c8563c64e1c9d5f60..03e59d59c786b87b060e24192b4baab6a83f2c2d 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;
        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;
        int len;
        const ASN1_PRIMITIVE_FUNCS *pf;
        pf = it->funcs;
index 706fa0b0e78cec1da503471e4800a11eba6316fc..23e9d48cdf3dc3514339d20e7a3d54cb036c25c1 100644 (file)
@@ -66,7 +66,8 @@ ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
        {
        ASN1_OBJECT *r;
        int i;
        {
        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))
 
        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);
                }
                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)
                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->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);
                memcpy(ln,o->ln,i);
+               r->ln=ln;
                }
 
        if (o->sn != NULL)
                {
                }
 
        if (o->sn != NULL)
                {
-               char *s;
-
                i=strlen(o->sn)+1;
                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);
                }
        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);
        }
 
        return(NULL);
        }