diff options
author | Richard Levitte <levitte@openssl.org> | 2018-08-13 20:37:43 +0200 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2018-08-14 10:50:12 +0100 |
commit | 7d4c97add12cfa5d4589880b09d6139c3203e2f4 (patch) | |
tree | b9a184cffb4bc48c4485252ae7da19372b6ebe8d /crypto/asn1 | |
parent | 0971432f6f6d8b40d797133621809bd31eb7bf4e (diff) |
i2d_ASN1_BOOLEAN(): allocate memory if the user didn't provide a buffer
Just as was done recently for i2d_ASN1_OBJECT, we also make
i2d_ASN1_BOOLEAN comply with the documentation.
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/6943)
Diffstat (limited to 'crypto/asn1')
-rw-r--r-- | crypto/asn1/a_bool.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/crypto/asn1/a_bool.c b/crypto/asn1/a_bool.c index 1b85bc9e61..98454f3b40 100644 --- a/crypto/asn1/a_bool.c +++ b/crypto/asn1/a_bool.c @@ -63,17 +63,31 @@ int i2d_ASN1_BOOLEAN(int a, unsigned char **pp) { int r; - unsigned char *p; + unsigned char *p, *allocated = NULL; r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN); if (pp == NULL) return (r); - p = *pp; + + if (*pp == NULL) { + if ((p = allocated = OPENSSL_malloc(r)) == NULL) { + ASN1err(ASN1_F_I2D_ASN1_OBJECT, ERR_R_MALLOC_FAILURE); + return 0; + } + } else { + p = *pp; + } ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL); - *(p++) = (unsigned char)a; - *pp = p; - return (r); + *p = (unsigned char)a; + + + /* + * If a new buffer was allocated, just return it back. + * If not, return the incremented buffer pointer. + */ + *pp = allocated != NULL ? allocated : p + 1; + return r; } int d2i_ASN1_BOOLEAN(int *a, const unsigned char **pp, long length) |