Don't leak memory on ASN1_GENERALIZEDTIME_adj() error path

The ASN1_GENERALIZEDTIME_adj() function leaks an ASN1_GENERALIZEDTIME
object on an error path.

(Imported from upstream's fe71bb3ad97ed01ccf92812891cc2bc3ef3dce76.)

Thanks to Jinguang Dong for pointing out the bug.

Change-Id: I2c14662bb03b0cf957bd277bda487f05f07e89e7
Reviewed-on: https://boringssl-review.googlesource.com/12185
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2016-11-10 09:18:08 -05:00 committed by Adam Langley
parent 7f2ee3522d
commit 126fa278f8

View File

@ -220,37 +220,43 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
struct tm *ts; struct tm *ts;
struct tm data; struct tm data;
size_t len = 20; size_t len = 20;
ASN1_GENERALIZEDTIME *tmps = NULL;
if (s == NULL) if (s == NULL)
s = M_ASN1_GENERALIZEDTIME_new(); tmps = ASN1_GENERALIZEDTIME_new();
if (s == NULL) else
return (NULL); tmps = s;
if (tmps == NULL)
return NULL;
ts = OPENSSL_gmtime(&t, &data); ts = OPENSSL_gmtime(&t, &data);
if (ts == NULL) if (ts == NULL)
return (NULL); goto err;
if (offset_day || offset_sec) { if (offset_day || offset_sec) {
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
return NULL; goto err;
} }
p = (char *)s->data; p = (char *)tmps->data;
if ((p == NULL) || ((size_t)s->length < len)) { if ((p == NULL) || ((size_t)tmps->length < len)) {
p = OPENSSL_malloc(len); p = OPENSSL_malloc(len);
if (p == NULL) { if (p == NULL) {
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE); OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
return (NULL); goto err;
} }
if (s->data != NULL) OPENSSL_free(tmps->data);
OPENSSL_free(s->data); tmps->data = (unsigned char *)p;
s->data = (unsigned char *)p;
} }
BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900, BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
ts->tm_sec); ts->tm_sec);
s->length = strlen(p); tmps->length = strlen(p);
s->type = V_ASN1_GENERALIZEDTIME; tmps->type = V_ASN1_GENERALIZEDTIME;
return (s); return tmps;
err:
if (s == NULL)
ASN1_GENERALIZEDTIME_free(tmps);
return NULL;
} }