crypto/pkcs12/p12_crt.c:311: error: USE_AFTER_FREE
  call to `sk_PKCS12_SAFEBAG_free()` eventually accesses memory that was invalidated by call to `free()` on line 309 indirectly during the call to `sk_PKCS12_SAFEBAG_push()`.
Showing all 22 steps of the trace


crypto/pkcs12/p12_crt.c:302:18: invalidation part of the trace starts here
300.           return 1;
301.       if (!*pbags) {
302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                        ^
303.           if (!*pbags)
304.               return 0;

crypto/pkcs12/p12_crt.c:302:18: passed as argument to `sk_PKCS12_SAFEBAG_new_null`
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                          ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:302:18: return from call to `sk_PKCS12_SAFEBAG_new_null`
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                          ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:302:9: assigned
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                 ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:309:10: when calling `sk_PKCS12_SAFEBAG_push` here
  307.           free_bags = 0;
  308.   
  309.       if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
                  ^
  310.           if (free_bags) {
  311.               sk_PKCS12_SAFEBAG_free(*pbags);

include/openssl/pkcs12.h:125:1: parameter `sk` of sk_PKCS12_SAFEBAG_push
    123.   } PKCS12_SAFEBAG;
    124.   
    125. > DEFINE_STACK_OF(PKCS12_SAFEBAG)
    126.   
    127.   typedef struct pkcs12_bag_st {

include/openssl/pkcs12.h:125:1: when calling `sk_push` here
    123.   } PKCS12_SAFEBAG;
    124.   
    125. > DEFINE_STACK_OF(PKCS12_SAFEBAG)
    126.   
    127.   typedef struct pkcs12_bag_st {

crypto/stack/stack.c:258:1: parameter `st` of sk_push
      256.   }
      257.   
      258. > int sk_push(_STACK *st, void *data)
      259.   {
      260.       return (sk_insert(st, data, st->num));

crypto/stack/stack.c:260:13: when calling `sk_insert` here
      258.   int sk_push(_STACK *st, void *data)
      259.   {
      260.       return (sk_insert(st, data, st->num));
                         ^
      261.   }
      262.   

crypto/stack/stack.c:166:1: parameter `st` of sk_insert
        164.   }
        165.   
        166. > int sk_insert(_STACK *st, void *data, int loc)
        167.   {
        168.       char **s;

crypto/stack/stack.c:173:13: when calling `CRYPTO_realloc` here
        171.           return 0;
        172.       if (st->num_alloc <= st->num + 1) {
        173.           s = OPENSSL_realloc((char *)st->data,
                           ^
        174.                               (unsigned int)sizeof(char *) * st->num_alloc * 2);
        175.           if (s == NULL)

crypto/mem.c:165:1: parameter `str` of CRYPTO_realloc
          163.   }
          164.   
          165. > void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
          166.   {
          167.       if (str == NULL)

crypto/mem.c:171:9: when calling `CRYPTO_free` here
          169.   
          170.       if (num == 0) {
          171.           CRYPTO_free(str);
                         ^
          172.           return NULL;
          173.       }

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

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

crypto/pkcs12/p12_crt.c:302:18: use-after-lifetime part of the trace starts here
300.           return 1;
301.       if (!*pbags) {
302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                        ^
303.           if (!*pbags)
304.               return 0;

crypto/pkcs12/p12_crt.c:302:18: passed as argument to `sk_PKCS12_SAFEBAG_new_null`
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                          ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:302:18: return from call to `sk_PKCS12_SAFEBAG_new_null`
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                          ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:302:9: assigned
  300.           return 1;
  301.       if (!*pbags) {
  302.           *pbags = sk_PKCS12_SAFEBAG_new_null();
                 ^
  303.           if (!*pbags)
  304.               return 0;

crypto/pkcs12/p12_crt.c:311:13: when calling `sk_PKCS12_SAFEBAG_free` here
  309.       if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
  310.           if (free_bags) {
  311.               sk_PKCS12_SAFEBAG_free(*pbags);
                     ^
  312.               *pbags = NULL;
  313.           }

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

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

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 pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,\n                          PKCS12_SAFEBAG *bag)\n{\n    int free_bags;\n    if (!pbags)\n        return 1;\n    if (!*pbags) {\n        *pbags = sk_PKCS12_SAFEBAG_new_null();\n        if (!*pbags)\n            return 0;\n        free_bags = 1;\n    } else\n        free_bags = 0;\n    if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {\n        if (free_bags) {\n            sk_PKCS12_SAFEBAG_free(*pbags);\n            *pbags = NULL;\n        }\n        return 0;\n    }\n    return 1;\n}', 'DEFINE_STACK_OF(PKCS12_SAFEBAG)', 'int sk_push(_STACK *st, void *data)\n{\n    return (sk_insert(st, data, st->num));\n}', 'int sk_insert(_STACK *st, void *data, int loc)\n{\n    char **s;\n    if (st == NULL)\n        return 0;\n    if (st->num_alloc <= st->num + 1) {\n        s = OPENSSL_realloc((char *)st->data,\n                            (unsigned int)sizeof(char *) * st->num_alloc * 2);\n        if (s == NULL)\n            return (0);\n        st->data = s;\n        st->num_alloc *= 2;\n    }\n    if ((loc >= (int)st->num) || (loc < 0))\n        st->data[st->num] = data;\n    else {\n        memmove(&(st->data[loc + 1]),\n                &(st->data[loc]), sizeof(char *) * (st->num - loc));\n        st->data[loc] = data;\n    }\n    st->num++;\n    st->sorted = 0;\n    return (st->num);\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n    if (str == NULL)\n        return CRYPTO_malloc(num, file, line);\n    if (num == 0) {\n        CRYPTO_free(str);\n        return NULL;\n    }\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        void *ret;\n        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n        ret = realloc(str, num);\n        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n        return ret;\n    }\n#else\n    (void)file;\n    (void)line;\n#endif\n    return realloc(str, num);\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}']