test/stack_test.c:97: error: USE_AFTER_FREE
  call to `sk_sint_push()` eventually accesses memory that was invalidated by call to `free()` on line 97 indirectly during the call to `sk_sint_push()`.
Showing all 24 steps of the trace


test/stack_test.c:83:25: invalidation part of the trace starts here
81.       };
82.       const int n_exfinds = OSSL_NELEM(exfinds);
83.       STACK_OF(sint) *s = sk_sint_new_null();
                              ^
84.       int i;
85.       int testresult = 0;

test/stack_test.c:83:25: passed as argument to `sk_sint_new_null`
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
                                ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:83:25: return from call to `sk_sint_new_null`
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
                                ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:83:5: assigned
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
            ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:97:9: when calling `sk_sint_push` here
  95.               goto end;
  96.           }
  97.           sk_sint_push(s, v + i);
                 ^
  98.       }
  99.       if (!TEST_int_eq(sk_sint_num(s), n))

test/stack_test.c:39:1: parameter `sk` of sk_sint_push
    37.   } SU;
    38.   
    39. > DEFINE_SPECIAL_STACK_OF(sint, int)
    40.   DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
    41.   DEFINE_STACK_OF(SS)

test/stack_test.c:39:1: when calling `OPENSSL_sk_push` here
    37.   } SU;
    38.   
    39. > DEFINE_SPECIAL_STACK_OF(sint, int)
    40.   DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)
    41.   DEFINE_STACK_OF(SS)

crypto/stack/stack.c:305:1: parameter `st` of OPENSSL_sk_push
      303.   }
      304.   
      305. > int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
      306.   {
      307.       if (st == NULL)

crypto/stack/stack.c:309:12: when calling `OPENSSL_sk_insert` here
      307.       if (st == NULL)
      308.           return -1;
      309.       return OPENSSL_sk_insert(st, data, st->num);
                        ^
      310.   }
      311.   

crypto/stack/stack.c:216:1: parameter `st` of OPENSSL_sk_insert
        214.   }
        215.   
        216. > int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
        217.   {
        218.       if (st == NULL || st->num == max_nodes)

crypto/stack/stack.c:221:10: when calling `sk_reserve` here
        219.           return 0;
        220.   
        221.       if (!sk_reserve(st, 1, 0))
                        ^
        222.           return 0;
        223.   

crypto/stack/stack.c:160:1: parameter `st` of sk_reserve
          158.   
          159.   /* internal STACK storage allocation */
          160. > static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
          161.   {
          162.       const void **tmpdata;

crypto/stack/stack.c:197:15: when calling `CRYPTO_realloc` here
          195.       }
          196.   
          197.       tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
                               ^
          198.       if (tmpdata == NULL)
          199.           return 0;

crypto/mem.c:208:1: parameter `str` of CRYPTO_realloc
            206.   }
            207.   
            208. > void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
            209.   {
            210.       if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)

crypto/mem.c:218:9: when calling `CRYPTO_free` here
            216.   
            217.       if (num == 0) {
            218.           CRYPTO_free(str, file, line);
                           ^
            219.           return NULL;
            220.       }

crypto/mem.c:265:1: parameter `str` of CRYPTO_free
              263.   }
              264.   
              265. > void CRYPTO_free(void *str, const char *file, int line)
              266.   {
              267.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:281:5: was invalidated by call to `free()`
              279.       }
              280.   #else
              281.       free(str);
                         ^
              282.   #endif
              283.   }

test/stack_test.c:83:25: use-after-lifetime part of the trace starts here
81.       };
82.       const int n_exfinds = OSSL_NELEM(exfinds);
83.       STACK_OF(sint) *s = sk_sint_new_null();
                              ^
84.       int i;
85.       int testresult = 0;

test/stack_test.c:83:25: passed as argument to `sk_sint_new_null`
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
                                ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:83:25: return from call to `sk_sint_new_null`
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
                                ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:83:5: assigned
  81.       };
  82.       const int n_exfinds = OSSL_NELEM(exfinds);
  83.       STACK_OF(sint) *s = sk_sint_new_null();
            ^
  84.       int i;
  85.       int testresult = 0;

test/stack_test.c:97:9: when calling `sk_sint_push` here
  95.               goto end;
  96.           }
  97.           sk_sint_push(s, v + i);
                 ^
  98.       }
  99.       if (!TEST_int_eq(sk_sint_num(s), n))

crypto/mem.c:265:1: parameter `str` of CRYPTO_free
    263.   }
    264.   
    265. > void CRYPTO_free(void *str, const char *file, int line)
    266.   {
    267.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:281:5: invalid access occurs here
    279.       }
    280.   #else
    281.       free(str);
               ^
    282.   #endif
    283.   }

void CRYPTO_free(void *str, const char *file, int line)
{
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_free(str, 0, file, line);
        free(str);
        CRYPTO_mem_debug_free(str, 1, file, line);
    } else {
        free(str);
    }
#else
    free(str);
#endif
}
['static int test_int_stack(int reserve)\n{\n    static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 };\n    static int notpresent = -1;\n    const int n = OSSL_NELEM(v);\n    static struct {\n        int value;\n        int unsorted;\n        int sorted;\n        int ex;\n    } finds[] = {\n        { 2,    1,  5,  5   },\n        { 9,    7,  6,  6   },\n        { -173, 5,  0,  0   },\n        { 999,  3,  8,  8   },\n        { 0,   -1, -1,  1   }\n    };\n    const int n_finds = OSSL_NELEM(finds);\n    static struct {\n        int value;\n        int ex;\n    } exfinds[] = {\n        { 3,    5   },\n        { 1000, 8   },\n        { 20,   8   },\n        { -999, 0   },\n        { -5,   0   },\n        { 8,    5   }\n    };\n    const int n_exfinds = OSSL_NELEM(exfinds);\n    STACK_OF(sint) *s = sk_sint_new_null();\n    int i;\n    int testresult = 0;\n    if (!TEST_ptr(s)\n        || (reserve > 0 && !TEST_true(sk_sint_reserve(s, 5 * reserve))))\n        goto end;\n    for (i = 0; i < n; i++) {\n        if (!TEST_int_eq(sk_sint_num(s), i)) {\n            TEST_info("int stack size %d", i);\n            goto end;\n        }\n        sk_sint_push(s, v + i);\n    }\n    if (!TEST_int_eq(sk_sint_num(s), n))\n        goto end;\n    for (i = 0; i < n; i++)\n        if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) {\n            TEST_info("int value %d", i);\n            goto end;\n        }\n    for (i = 0; i < n_finds; i++) {\n        int *val = (finds[i].unsorted == -1) ? &notpresent\n                                             : v + finds[i].unsorted;\n        if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) {\n            TEST_info("int unsorted find %d", i);\n            goto end;\n        }\n    }\n    for (i = 0; i < n_finds; i++) {\n        int *val = (finds[i].unsorted == -1) ? &notpresent\n                                             : v + finds[i].unsorted;\n        if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) {\n            TEST_info("int unsorted find_ex %d", i);\n            goto end;\n        }\n    }\n    if (!TEST_false(sk_sint_is_sorted(s)))\n        goto end;\n    sk_sint_set_cmp_func(s, &int_compare);\n    sk_sint_sort(s);\n    if (!TEST_true(sk_sint_is_sorted(s)))\n        goto end;\n    for (i = 0; i < n_finds; i++)\n        if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) {\n            TEST_info("int sorted find %d", i);\n            goto end;\n        }\n    for (i = 0; i < n_finds; i++)\n        if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) {\n            TEST_info("int sorted find_ex present %d", i);\n            goto end;\n        }\n    for (i = 0; i < n_exfinds; i++)\n        if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){\n            TEST_info("int sorted find_ex absent %d", i);\n            goto end;\n        }\n    if (!TEST_ptr_eq(sk_sint_shift(s), v + 6))\n        goto end;\n    testresult = 1;\nend:\n    sk_sint_free(s);\n    return testresult;\n}', 'DEFINE_SPECIAL_STACK_OF(sint, int)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n    if (st == NULL)\n        return -1;\n    return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n    if (st == NULL || st->num == max_nodes)\n        return 0;\n    if (!sk_reserve(st, 1, 0))\n        return 0;\n    if ((loc >= st->num) || (loc < 0)) {\n        st->data[st->num] = data;\n    } else {\n        memmove(&st->data[loc + 1], &st->data[loc],\n                sizeof(st->data[0]) * (st->num - loc));\n        st->data[loc] = data;\n    }\n    st->num++;\n    st->sorted = 0;\n    return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n    const void **tmpdata;\n    int num_alloc;\n    if (n > max_nodes - st->num)\n        return 0;\n    num_alloc = st->num + n;\n    if (num_alloc < min_nodes)\n        num_alloc = min_nodes;\n    if (st->data == NULL) {\n        st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);\n        if (st->data == NULL)\n            return 0;\n        st->num_alloc = num_alloc;\n        return 1;\n    }\n    if (!exact) {\n        if (num_alloc <= st->num_alloc)\n            return 1;\n        num_alloc = compute_growth(num_alloc, st->num_alloc);\n        if (num_alloc == 0)\n            return 0;\n    } else if (num_alloc == st->num_alloc) {\n        return 1;\n    }\n    tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n    if (tmpdata == NULL)\n        return 0;\n    st->data = tmpdata;\n    st->num_alloc = num_alloc;\n    return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n    if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n        return realloc_impl(str, num, file, line);\n    FAILTEST();\n    if (str == NULL)\n        return CRYPTO_malloc(num, file, line);\n    if (num == 0) {\n        CRYPTO_free(str, file, line);\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); (void)(line);\n#endif\n    return realloc(str, num);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\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}']