If we pass a pointer to NULL, the memory allocated by OpenSSL has to be
freed with OPENSSL_free(). Otherwise, this can lead to random
crashes/freezes for Windows builds as seen on AppVeyor. To not
complicate things for callers of this macro, we allocate our own memory,
which we already do for other i2d_* calls.
* @returns allocated chunk of the object, or chunk_empty
*/
#define openssl_i2chunk(type, obj) ({ \
- unsigned char *ptr = NULL; \
- int len = i2d_##type(obj, &ptr); \
- len < 0 ? chunk_empty : chunk_create(ptr, len);})
+ chunk_t chunk = chunk_empty; \
+ int len = i2d_##type(obj, NULL); \
+ if (len >= 0) { \
+ chunk = chunk_alloc(len); \
+ u_char *p = chunk.ptr; \
+ i2d_##type(obj, &p); \
+ } \
+ chunk; })
/**
* Convert an OpenSSL ASN1_OBJECT to a chunk.