crypto/evp/digest.c:99: error: USE_AFTER_FREE
  call to `EVP_DigestInit_ex()` eventually accesses memory that was invalidated by call to `free()` on line 98 indirectly during the call to `EVP_MD_CTX_reset()`.
Showing all 16 steps of the trace


crypto/evp/digest.c:96:1: invalidation part of the trace starts here
94.   }
95.   
96. > int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
97.   {
98.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:96:1: parameter `ctx` of EVP_DigestInit
  94.   }
  95.   
  96. > int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  97.   {
  98.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:98:5: when calling `EVP_MD_CTX_reset` here
  96.   int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  97.   {
  98.       EVP_MD_CTX_reset(ctx);
             ^
  99.       return EVP_DigestInit_ex(ctx, type, NULL);
  100.   }

crypto/evp/digest.c:20:1: parameter `ctx` of EVP_MD_CTX_reset
    18.   
    19.   /* This call frees resources associated with the context */
    20. > int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
    21.   {
    22.       if (ctx == NULL)

crypto/evp/digest.c:59:9: when calling `EVP_PKEY_CTX_free` here
    57.        */
    58.       if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
    59.           EVP_PKEY_CTX_free(ctx->pctx);
                  ^
    60.   #ifndef OPENSSL_NO_ENGINE
    61.       ENGINE_finish(ctx->engine);

crypto/evp/pmeth_lib.c:352:1: parameter `ctx` of EVP_PKEY_CTX_free
      350.   }
      351.   
      352. > void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
      353.   {
      354.       if (ctx == NULL)

crypto/evp/pmeth_lib.c:363:5: when calling `CRYPTO_free` here
      361.       ENGINE_finish(ctx->engine);
      362.   #endif
      363.       OPENSSL_free(ctx);
                 ^
      364.   }
      365.   

crypto/mem.c:295:1: parameter `str` of CRYPTO_free
        293.   }
        294.   
        295. > void CRYPTO_free(void *str, const char *file, int line)
        296.   {
        297.       INCREMENT(free_count);

crypto/mem.c:312:5: was invalidated by call to `free()`
        310.       }
        311.   #else
        312.       free(str);
                   ^
        313.   #endif
        314.   }

crypto/evp/digest.c:96:1: use-after-lifetime part of the trace starts here
94.   }
95.   
96. > int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
97.   {
98.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:96:1: parameter `ctx` of EVP_DigestInit
  94.   }
  95.   
  96. > int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
  97.   {
  98.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:99:12: when calling `EVP_DigestInit_ex` here
  97.   {
  98.       EVP_MD_CTX_reset(ctx);
  99.       return EVP_DigestInit_ex(ctx, type, NULL);
                    ^
  100.   }
  101.   

crypto/evp/digest.c:102:1: parameter `ctx` of EVP_DigestInit_ex
    100.   }
    101.   
    102. > int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
    103.   {
    104.       EVP_MD *provmd;

crypto/evp/digest.c:250:13: when calling `EVP_PKEY_CTX_ctrl` here
    248.       if (ctx->pctx) {
    249.           int r;
    250.           r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
                       ^
    251.                                 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
    252.           if (r <= 0 && (r != -2))

crypto/evp/pmeth_lib.c:366:1: parameter `ctx` of EVP_PKEY_CTX_ctrl
      364.   }
      365.   
      366. > int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
      367.                         int cmd, int p1, void *p2)
      368.   {

crypto/evp/pmeth_lib.c:393:11: invalid access occurs here
      391.   
      392.    doit:
      393.       ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
                       ^
      394.   
      395.       if (ret == -2)

int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
                      int cmd, int p1, void *p2)
{
    int ret;
    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
        return -2;
    }
    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
        return -1;
    if (ctx->pmeth->digest_custom != NULL)
        goto doit;
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
        return -1;
    }
    if ((optype != -1) && !(ctx->operation & optype)) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
        return -1;
    }
 doit:
    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
    if (ret == -2)
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
    return ret;
}
['int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)\n{\n    EVP_MD_CTX_reset(ctx);\n    return EVP_DigestInit_ex(ctx, type, NULL);\n}', 'int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)\n{\n    if (ctx == NULL)\n        return 1;\n    if (ctx->digest == NULL || ctx->digest->prov == NULL)\n        goto legacy;\n    if (ctx->provctx != NULL) {\n        if (ctx->digest->freectx != NULL)\n            ctx->digest->freectx(ctx->provctx);\n        ctx->provctx = NULL;\n        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n    }\n    if (ctx->pctx != NULL)\n        goto legacy;\n    return 1;\n legacy:\n    if (ctx->digest && ctx->digest->cleanup\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))\n        ctx->digest->cleanup(ctx);\n    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n    }\n    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))\n        EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_cleanse(ctx, sizeof(*ctx));\n    return 1;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    if (ctx->pmeth && ctx->pmeth->cleanup)\n        ctx->pmeth->cleanup(ctx);\n    EVP_PKEY_free(ctx->pkey);\n    EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n    EVP_MD *provmd;\n    ENGINE *tmpimpl = NULL;\n    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine && ctx->digest &&\n        (type == NULL || (type->type == ctx->digest->type)))\n        goto skip_to_init;\n    if (type != NULL && impl == NULL)\n        tmpimpl = ENGINE_get_digest_engine(type->type);\n#endif\n    if (ctx->engine != NULL\n            || impl != NULL\n            || tmpimpl != NULL\n            || ctx->pctx != NULL\n            || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {\n        if (ctx->digest == ctx->fetched_digest)\n            ctx->digest = NULL;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = NULL;\n        goto legacy;\n    }\n    if (type->prov == NULL) {\n        switch(type->type) {\n        case NID_sha256:\n            break;\n        default:\n            goto legacy;\n        }\n    }\n    if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n        ctx->md_data = NULL;\n    }\n    if (type->prov == NULL) {\n        provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");\n        if (provmd == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n        type = provmd;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = provmd;\n    }\n    ctx->digest = type;\n    if (ctx->provctx == NULL) {\n        ctx->provctx = ctx->digest->newctx();\n        if (ctx->provctx == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n    }\n    if (ctx->digest->dinit == NULL) {\n        EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n        return 0;\n    }\n    return ctx->digest->dinit(ctx->provctx);\n legacy:\n#ifndef OPENSSL_NO_ENGINE\n    if (type) {\n        ENGINE_finish(ctx->engine);\n        if (impl != NULL) {\n            if (!ENGINE_init(impl)) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                return 0;\n            }\n        } else {\n            impl = tmpimpl;\n        }\n        if (impl != NULL) {\n            const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n            if (d == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                ENGINE_finish(impl);\n                return 0;\n            }\n            type = d;\n            ctx->engine = impl;\n        } else\n            ctx->engine = NULL;\n    } else {\n        if (!ctx->digest) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n            return 0;\n        }\n        type = ctx->digest;\n    }\n#endif\n    if (ctx->digest != type) {\n        if (ctx->digest && ctx->digest->ctx_size) {\n            OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n            ctx->md_data = NULL;\n        }\n        ctx->digest = type;\n        if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n            ctx->update = type->update;\n            ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n            if (ctx->md_data == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n                return 0;\n            }\n        }\n    }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n    if (ctx->pctx) {\n        int r;\n        r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n                              EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n        if (r <= 0 && (r != -2))\n            return 0;\n    }\n    if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n        return 1;\n    return ctx->digest->init(ctx);\n}', 'int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,\n                      int cmd, int p1, void *p2)\n{\n    int ret;\n    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n        return -2;\n    }\n    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))\n        return -1;\n    if (ctx->pmeth->digest_custom != NULL)\n        goto doit;\n    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);\n        return -1;\n    }\n    if ((optype != -1) && !(ctx->operation & optype)) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);\n        return -1;\n    }\n doit:\n    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);\n    if (ret == -2)\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n    return ret;\n}']