From: Greg Hudson Date: Thu, 12 Feb 2009 18:21:33 +0000 (+0000) Subject: Modify asn12krb5_buf and asn1_do_full_encode to make output parameter X-Git-Tag: krb5-1.8-alpha1~641 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=bcffa165ee7841337bfbbab5043e7bddb5ef7fca;p=krb5.git Modify asn12krb5_buf and asn1_do_full_encode to make output parameter values well-defined on error. Clean up memory handling and an unused variable in asn1_do_full_encode. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21981 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/asn.1/asn1_encode.c b/src/lib/krb5/asn.1/asn1_encode.c index d79546493..f67c8cfee 100644 --- a/src/lib/krb5/asn.1/asn1_encode.c +++ b/src/lib/krb5/asn.1/asn1_encode.c @@ -675,10 +675,13 @@ krb5int_asn1_do_full_encode(const void *rep, krb5_data **code, { unsigned int length; asn1_error_code retval; - unsigned int sum = 0; asn1buf *buf = NULL; + krb5_data *d; + + *code = NULL; - if (rep == NULL) return ASN1_MISSING_FIELD; + if (rep == NULL) + return ASN1_MISSING_FIELD; retval = asn1buf_create(&buf); if (retval) @@ -686,9 +689,12 @@ krb5int_asn1_do_full_encode(const void *rep, krb5_data **code, retval = krb5int_asn1_encode_a_thing(buf, rep, a, &length); if (retval) - return retval; - sum += length; - retval = asn12krb5_buf(buf, code); + goto cleanup; + retval = asn12krb5_buf(buf, &d); + if (retval) + goto cleanup; + *code = d; +cleanup: asn1buf_destroy(&buf); return retval; } diff --git a/src/lib/krb5/asn.1/asn1buf.c b/src/lib/krb5/asn.1/asn1buf.c index c6c80a474..5793a0303 100644 --- a/src/lib/krb5/asn.1/asn1buf.c +++ b/src/lib/krb5/asn.1/asn1buf.c @@ -253,21 +253,24 @@ int asn1buf_remains(asn1buf *buf, int indef) asn1_error_code asn12krb5_buf(const asn1buf *buf, krb5_data **code) { unsigned int i; - *code = (krb5_data*)calloc(1,sizeof(krb5_data)); - if (*code == NULL) return ENOMEM; - (*code)->magic = KV5M_DATA; - (*code)->data = NULL; - (*code)->length = 0; - (*code)->length = asn1buf_len(buf); - (*code)->data = (char*)malloc((((*code)->length)+1)*sizeof(char)); - if ((*code)->data == NULL) { - free(*code); - *code = NULL; + krb5_data *d; + + *code = NULL; + + d = calloc(1, sizeof(krb5_data)); + if (d == NULL) + return ENOMEM; + d->length = asn1buf_len(buf); + d->data = malloc(d->length + 1); + if (d->data == NULL) { + free(d); return ENOMEM; } - for (i=0; i < (*code)->length; i++) - ((*code)->data)[i] = (buf->base)[((*code)->length)-i-1]; - ((*code)->data)[(*code)->length] = '\0'; + for (i=0; i < d->length; i++) + d->data[i] = buf->base[d->length - i - 1]; + d->data[d->length] = '\0'; + d->magic = KV5M_DATA; + *code = d; return 0; }