Addenum to "Constify obj_dat.[ch]."

This commit is contained in:
Andy Polyakov 2007-09-18 22:15:31 +00:00
parent b5e5760d01
commit a005fb019f
4 changed files with 36 additions and 26 deletions

View File

@ -287,6 +287,7 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
{ {
ASN1_OBJECT *ret=NULL; ASN1_OBJECT *ret=NULL;
const unsigned char *p; const unsigned char *p;
unsigned char *data;
int i; int i;
/* only the ASN1_OBJECTs from the 'table' will have values /* 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); else ret=(*a);
p= *pp; 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->length=0;
ret->data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1); if (data != NULL) OPENSSL_free(data);
ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
if (ret->data == NULL) 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->length=(int)len;
ret->sn=NULL; ret->sn=NULL;
ret->ln=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->data=NULL;
a->length=0; a->length=0;
} }

View File

@ -208,7 +208,7 @@ typedef struct asn1_object_st
const char *sn,*ln; const char *sn,*ln;
int nid; int nid;
int length; int length;
unsigned char *data; const unsigned char *data; /* data remains const after init */
int flags; /* Should we free this one */ int flags; /* Should we free this one */
} ASN1_OBJECT; } ASN1_OBJECT;

View File

@ -569,7 +569,8 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
ASN1_STRING *strtmp; ASN1_STRING *strtmp;
ASN1_OBJECT *otmp; ASN1_OBJECT *otmp;
int utype; int utype;
unsigned char *cont, c; const unsigned char *cont;
unsigned char c;
int len; int len;
const ASN1_PRIMITIVE_FUNCS *pf; const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs; pf = it->funcs;

View File

@ -66,7 +66,8 @@ ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o)
{ {
ASN1_OBJECT *r; ASN1_OBJECT *r;
int i; int i;
char *ln=NULL; char *ln=NULL,*sn=NULL;
unsigned char *data=NULL;
if (o == NULL) return(NULL); if (o == NULL) return(NULL);
if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) 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); OBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);
return(NULL); return(NULL);
} }
r->data=OPENSSL_malloc(o->length); data=OPENSSL_malloc(o->length);
if (r->data == NULL) if (data == NULL)
goto err; goto err;
if (o->data != NULL) 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->length=o->length;
r->nid=o->nid; r->nid=o->nid;
r->ln=r->sn=NULL; r->ln=r->sn=NULL;
if (o->ln != NULL) if (o->ln != NULL)
{ {
i=strlen(o->ln)+1; i=strlen(o->ln)+1;
r->ln=ln=OPENSSL_malloc(i); ln=OPENSSL_malloc(i);
if (r->ln == NULL) goto err; 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); sn=OPENSSL_malloc(i);
if (r->sn == NULL) goto err; if (sn == NULL) goto err;
memcpy(s,o->sn,i); memcpy(sn,o->sn,i);
r->sn=sn;
} }
r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC| r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA); ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA);
return(r); return(r);
err: err:
OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE); OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
if (r != NULL) if (ln != NULL) OPENSSL_free(ln);
{ if (sn != NULL) OPENSSL_free(sn);
if (ln != NULL) OPENSSL_free(ln); if (data != NULL) OPENSSL_free(data);
if (r->data != NULL) OPENSSL_free(r->data); if (r != NULL) OPENSSL_free(r);
OPENSSL_free(r);
}
return(NULL); return(NULL);
} }