void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
                           const char *file, int line)
{
    void *ret = NULL;
    if (str == NULL)
        return CRYPTO_malloc(num, file, line);
    if (num == 0) {
        CRYPTO_clear_free(str, old_len);
        return NULL;
    }
    if (num < old_len) {
        memset((char*)str + num, 0, old_len - num);
        return str;
    }
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
        ret = malloc(num);
        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
    } else {
        ret = malloc(num);
    }
#else
    (void)file;
    (void)line;
    ret = malloc(num);
#endif
    if (ret)
        memcpy(ret, str, old_len);
    CRYPTO_clear_free(str, old_len);
    return ret;
}
['static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,\n                                 const unsigned char **in, long inlen,\n                                 const ASN1_ITEM *it,\n                                 int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n    int ret = 0, utype;\n    long plen;\n    char cst, inf, free_cont = 0;\n    const unsigned char *p;\n    BUF_MEM buf = { 0, NULL, 0, 0 };\n    const unsigned char *cont = NULL;\n    long len;\n    if (!pval) {\n        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);\n        return 0;\n    }\n    if (it->itype == ASN1_ITYPE_MSTRING) {\n        utype = tag;\n        tag = -1;\n    } else\n        utype = it->utype;\n    if (utype == V_ASN1_ANY) {\n        unsigned char oclass;\n        if (tag >= 0) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);\n            return 0;\n        }\n        if (opt) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,\n                    ASN1_R_ILLEGAL_OPTIONAL_ANY);\n            return 0;\n        }\n        p = *in;\n        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,\n                              &p, inlen, -1, 0, 0, ctx);\n        if (!ret) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);\n            return 0;\n        }\n        if (oclass != V_ASN1_UNIVERSAL)\n            utype = V_ASN1_OTHER;\n    }\n    if (tag == -1) {\n        tag = utype;\n        aclass = V_ASN1_UNIVERSAL;\n    }\n    p = *in;\n    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,\n                          &p, inlen, tag, aclass, opt, ctx);\n    if (!ret) {\n        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);\n        return 0;\n    } else if (ret == -1)\n        return -1;\n    ret = 0;\n    if ((utype == V_ASN1_SEQUENCE)\n        || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {\n        if (utype == V_ASN1_OTHER) {\n            asn1_tlc_clear(ctx);\n        }\n        else if (!cst) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,\n                    ASN1_R_TYPE_NOT_CONSTRUCTED);\n            return 0;\n        }\n        cont = *in;\n        if (inf) {\n            if (!asn1_find_end(&p, plen, inf))\n                goto err;\n            len = p - cont;\n        } else {\n            len = p - cont + plen;\n            p += plen;\n        }\n    } else if (cst) {\n        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN\n            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER\n            || utype == V_ASN1_ENUMERATED) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);\n            return 0;\n        }\n        free_cont = 1;\n        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {\n            goto err;\n        }\n        len = buf.length;\n        if (!BUF_MEM_grow_clean(&buf, len + 1)) {\n            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        buf.data[len] = 0;\n        cont = (const unsigned char *)buf.data;\n    } else {\n        cont = p;\n        len = plen;\n        p += plen;\n    }\n    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))\n        goto err;\n    *in = p;\n    ret = 1;\n err:\n    if (free_cont)\n        OPENSSL_free(buf.data);\n    return ret;\n}', 'static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,\n                        char inf, int tag, int aclass, int depth)\n{\n    const unsigned char *p, *q;\n    long plen;\n    char cst, ininf;\n    p = *in;\n    inf &= 1;\n    if (!buf && !inf) {\n        *in += len;\n        return 1;\n    }\n    while (len > 0) {\n        q = p;\n        if (asn1_check_eoc(&p, len)) {\n            if (!inf) {\n                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);\n                return 0;\n            }\n            inf = 0;\n            break;\n        }\n        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,\n                             len, tag, aclass, 0, NULL)) {\n            ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);\n            return 0;\n        }\n        if (cst) {\n            if (depth >= ASN1_MAX_STRING_NEST) {\n                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);\n                return 0;\n            }\n            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))\n                return 0;\n        } else if (plen && !collect_data(buf, &p, plen))\n            return 0;\n        len -= p - q;\n    }\n    if (inf) {\n        ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);\n        return 0;\n    }\n    *in = p;\n    return 1;\n}', 'size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)\n{\n    char *ret;\n    size_t n;\n    if (str->length >= len) {\n        memset(&str->data[len], 0, str->length - len);\n        str->length = len;\n        return (len);\n    }\n    if (str->max >= len) {\n        memset(&str->data[str->length], 0, len - str->length);\n        str->length = len;\n        return (len);\n    }\n    if (len > LIMIT_BEFORE_EXPANSION) {\n        BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    n = (len + 3) / 3 * 4;\n    if ((str->flags & BUF_MEM_FLAG_SECURE))\n        ret = sec_alloc_realloc(str, n);\n    else\n        ret = OPENSSL_clear_realloc(str->data, str->max, n);\n    if (ret == NULL) {\n        BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);\n        len = 0;\n    } else {\n        str->data = ret;\n        str->max = n;\n        memset(&str->data[str->length], 0, len - str->length);\n        str->length = len;\n    }\n    return (len);\n}', 'void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,\n                           const char *file, int line)\n{\n    void *ret = NULL;\n    if (str == NULL)\n        return CRYPTO_malloc(num, file, line);\n    if (num == 0) {\n        CRYPTO_clear_free(str, old_len);\n        return NULL;\n    }\n    if (num < old_len) {\n        memset((char*)str + num, 0, old_len - num);\n        return str;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    (void)file;\n    (void)line;\n    ret = malloc(num);\n#endif\n    if (ret)\n        memcpy(ret, str, old_len);\n    CRYPTO_clear_free(str, old_len);\n    return ret;\n}']