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


test/stack_test.c:178:26: invalidation part of the trace starts here
176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
177.       const int n = OSSL_NELEM(v);
178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                ^
179.       int i;
180.       int testresult = 0;

test/stack_test.c:178:26: passed as argument to `sk_uchar_new`
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                  ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:178:26: return from call to `sk_uchar_new`
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                  ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:178:5: assigned
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
             ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:192:9: when calling `sk_uchar_unshift` here
  190.               goto end;
  191.           }
  192.           sk_uchar_unshift(s, v + i);
                 ^
  193.       }
  194.       if (!TEST_int_eq(sk_uchar_num(s), n))

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

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

crypto/stack/stack.c:314:1: parameter `st` of OPENSSL_sk_unshift
      312.   }
      313.   
      314. > int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
      315.   {
      316.       return (OPENSSL_sk_insert(st, data, 0));

crypto/stack/stack.c:316:13: when calling `OPENSSL_sk_insert` here
      314.   int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
      315.   {
      316.       return (OPENSSL_sk_insert(st, data, 0));
                         ^
      317.   }
      318.   

crypto/stack/stack.c:222:1: parameter `st` of OPENSSL_sk_insert
        220.   }
        221.   
        222. > int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
        223.   {
        224.       if (st == NULL || st->num < 0 || st->num == max_nodes)

crypto/stack/stack.c:227:10: when calling `sk_reserve` here
        225.           return 0;
        226.   
        227.       if (!sk_reserve(st, 1, 0))
                        ^
        228.           return 0;
        229.   

crypto/stack/stack.c:166:1: parameter `st` of sk_reserve
          164.   
          165.   /* internal STACK storage allocation */
          166. > static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
          167.   {
          168.       const void **tmpdata;

crypto/stack/stack.c:203:15: when calling `CRYPTO_realloc` here
          201.       }
          202.   
          203.       tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
                               ^
          204.       if (tmpdata == NULL)
          205.           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:178:26: use-after-lifetime part of the trace starts here
176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
177.       const int n = OSSL_NELEM(v);
178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                ^
179.       int i;
180.       int testresult = 0;

test/stack_test.c:178:26: passed as argument to `sk_uchar_new`
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                  ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:178:26: return from call to `sk_uchar_new`
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
                                  ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:178:5: assigned
  176.       static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };
  177.       const int n = OSSL_NELEM(v);
  178.       STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;
             ^
  179.       int i;
  180.       int testresult = 0;

test/stack_test.c:192:9: when calling `sk_uchar_unshift` here
  190.               goto end;
  191.           }
  192.           sk_uchar_unshift(s, v + i);
                 ^
  193.       }
  194.       if (!TEST_int_eq(sk_uchar_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_uchar_stack(int reserve)\n{\n    static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 };\n    const int n = OSSL_NELEM(v);\n    STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL;\n    int i;\n    int testresult = 0;\n    if (!TEST_ptr(s)\n        || (reserve > 0 && !TEST_true(sk_uchar_reserve(s, 5 * reserve))))\n        goto end;\n    for (i = 0; i < n; i++) {\n        if (!TEST_int_eq(sk_uchar_num(s), i)) {\n            TEST_info("uchar stack size %d", i);\n            goto end;\n        }\n        sk_uchar_unshift(s, v + i);\n    }\n    if (!TEST_int_eq(sk_uchar_num(s), n))\n        goto end;\n    r = sk_uchar_dup(s);\n    if (!TEST_int_eq(sk_uchar_num(r), n))\n        goto end;\n    sk_uchar_sort(r);\n    for (i = 0; i < n; i++)\n        if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) {\n            TEST_info("uchar pop %d", i);\n            goto end;\n        }\n    sk_uchar_free(s);\n    s = NULL;\n    if (!TEST_int_eq(sk_uchar_num(r), n))\n        goto end;\n    sk_uchar_zero(r);\n    if (!TEST_int_eq(sk_uchar_num(r), 0))\n        goto end;\n    sk_uchar_insert(r, v, 0);\n    sk_uchar_insert(r, v + 2, -1);\n    sk_uchar_insert(r, v + 1, 1);\n    for (i = 0; i < 3; i++)\n        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {\n            TEST_info("uchar insert %d", i);\n            goto end;\n        }\n    if (!TEST_ptr_null(sk_uchar_delete(r, 12)))\n        goto end;\n    if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1))\n        goto end;\n    sk_uchar_set(r, 1, v + 1);\n    for (i = 0; i < 2; i++)\n        if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) {\n            TEST_info("uchar set %d", i);\n            goto end;\n        }\n    testresult = 1;\nend:\n    sk_uchar_free(r);\n    sk_uchar_free(s);\n    return testresult;\n}', 'DEFINE_SPECIAL_STACK_OF_CONST(uchar, unsigned char)', 'int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)\n{\n    return (OPENSSL_sk_insert(st, data, 0));\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n    if (st == NULL || st->num < 0 || 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}']