apps/ts.c:538: error: USE_AFTER_FREE
  call to `EVP_MD_CTX_free()` eventually accesses memory that was invalidated by call to `free()` on line 537 indirectly during the call to `EVP_DigestFinal()`.
Showing all 20 steps of the trace


apps/ts.c:526:30: invalidation part of the trace starts here
524.   
525.       if (input) {
526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                    ^
527.           unsigned char buffer[4096];
528.           int length;

apps/ts.c:526:30: passed as argument to `EVP_MD_CTX_new`
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                      ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:526:30: return from call to `EVP_MD_CTX_new`
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                      ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:526:9: assigned
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                 ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:537:14: when calling `EVP_DigestFinal` here
  535.               EVP_DigestUpdate(md_ctx, buffer, length);
  536.           }
  537.           if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) {
                      ^
  538.               EVP_MD_CTX_free(md_ctx);
  539.               return 0;

crypto/evp/digest.c:261:1: parameter `ctx` of EVP_DigestFinal
    259.   
    260.   /* The caller can assume that this removes any secret data from the context */
    261. > int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
    262.   {
    263.       int ret;

crypto/evp/digest.c:265:5: when calling `EVP_MD_CTX_reset` here
    263.       int ret;
    264.       ret = EVP_DigestFinal_ex(ctx, md, size);
    265.       EVP_MD_CTX_reset(ctx);
               ^
    266.       return ret;
    267.   }

crypto/evp/digest.c:123:1: parameter `ctx` of EVP_MD_CTX_reset
      121.   
      122.   /* This call frees resources associated with the context */
      123. > int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
      124.   {
      125.       if (ctx == NULL)

crypto/evp/digest.c:137:9: when calling `CRYPTO_clear_free` here
      135.       if (ctx->digest && ctx->digest->ctx_size && ctx->md_data
      136.           && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
      137.           OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
                     ^
      138.       }
      139.       EVP_PKEY_CTX_free(ctx->pctx);

crypto/mem.c:249:1: parameter `str` of CRYPTO_clear_free
        247.   }
        248.   
        249. > void CRYPTO_clear_free(void *str, size_t num)
        250.   {
        251.       if (str == NULL)

crypto/mem.c:255:5: when calling `CRYPTO_free` here
        253.       if (num)
        254.           OPENSSL_cleanse(str, num);
        255.       CRYPTO_free(str);
                   ^
        256.   }

crypto/mem.c:234:1: parameter `str` of CRYPTO_free
          232.   }
          233.   
          234. > void CRYPTO_free(void *str)
          235.   {
          236.   #ifndef OPENSSL_NO_CRYPTO_MDEBUG

crypto/mem.c:245:5: was invalidated by call to `free()`
          243.       }
          244.   #else
          245.       free(str);
                     ^
          246.   #endif
          247.   }

apps/ts.c:526:30: use-after-lifetime part of the trace starts here
524.   
525.       if (input) {
526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                    ^
527.           unsigned char buffer[4096];
528.           int length;

apps/ts.c:526:30: passed as argument to `EVP_MD_CTX_new`
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                      ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:526:30: return from call to `EVP_MD_CTX_new`
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                                      ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:526:9: assigned
  524.   
  525.       if (input) {
  526.           EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
                 ^
  527.           unsigned char buffer[4096];
  528.           int length;

apps/ts.c:538:13: when calling `EVP_MD_CTX_free` here
  536.           }
  537.           if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) {
  538.               EVP_MD_CTX_free(md_ctx);
                     ^
  539.               return 0;
  540.           }

crypto/mem.c:234:1: parameter `str` of CRYPTO_free
    232.   }
    233.   
    234. > void CRYPTO_free(void *str)
    235.   {
    236.   #ifndef OPENSSL_NO_CRYPTO_MDEBUG

crypto/mem.c:245:5: invalid access occurs here
    243.       }
    244.   #else
    245.       free(str);
               ^
    246.   #endif
    247.   }

void CRYPTO_free(void *str)
{
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_free(str, 0);
        free(str);
        CRYPTO_mem_debug_free(str, 1);
    } else {
        free(str);
    }
#else
    free(str);
#endif
}
['static int create_digest(BIO *input, char *digest, const EVP_MD *md,\n                         unsigned char **md_value)\n{\n    int md_value_len;\n    md_value_len = EVP_MD_size(md);\n    if (md_value_len < 0)\n        return 0;\n    if (input) {\n        EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();\n        unsigned char buffer[4096];\n        int length;\n        if (md_ctx == NULL)\n            return 0;\n        *md_value = app_malloc(md_value_len, "digest buffer");\n        EVP_DigestInit(md_ctx, md);\n        while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {\n            EVP_DigestUpdate(md_ctx, buffer, length);\n        }\n        if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) {\n            EVP_MD_CTX_free(md_ctx);\n            return 0;\n        }\n        EVP_MD_CTX_free(md_ctx);\n    } else {\n        long digest_len;\n        *md_value = string_to_hex(digest, &digest_len);\n        if (!*md_value || md_value_len != digest_len) {\n            OPENSSL_free(*md_value);\n            *md_value = NULL;\n            BIO_printf(bio_err, "bad digest, %d bytes "\n                       "must be specified\\n", md_value_len);\n            return 0;\n        }\n    }\n    return md_value_len;\n}', 'int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)\n{\n    int ret;\n    ret = EVP_DigestFinal_ex(ctx, md, size);\n    EVP_MD_CTX_reset(ctx);\n    return ret;\n}', 'int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)\n{\n    if (ctx == NULL)\n        return 1;\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    EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine)\n        ENGINE_finish(ctx->engine);\n#endif\n    memset(ctx, 0, sizeof(*ctx));\n    return 1;\n}', 'void CRYPTO_clear_free(void *str, size_t num)\n{\n    if (str == NULL)\n        return;\n    if (num)\n        OPENSSL_cleanse(str, num);\n    CRYPTO_free(str);\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}']