test/bntest.c:216: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, +oo] by call to `BN_div_recp`.
Showing all 18 steps of the trace


test/bntest.c:215:9: Call
213.           BN_set_negative(a, rand_neg());
214.           BN_set_negative(b, rand_neg());
215.           BN_RECP_CTX_set(recp, b, ctx);
               ^
216.           BN_div_recp(d, c, a, recp, ctx);
217.           BN_mul(e, d, b, ctx);

crypto/bn/bn_recp.c:44:1: Parameter `*recp->Nr.d`
  42.   }
  43.   
  44. > int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
  45.   {
  46.       if (!BN_copy(&(recp->N), d))

test/bntest.c:216:9: Call
214.           BN_set_negative(b, rand_neg());
215.           BN_RECP_CTX_set(recp, b, ctx);
216.           BN_div_recp(d, c, a, recp, ctx);
               ^
217.           BN_mul(e, d, b, ctx);
218.           BN_add(d, e, c);

crypto/bn/bn_recp.c:83:1: Parameter `*recp->Nr.d`
  81.   }
  82.   
  83. > int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
  84.                   BN_RECP_CTX *recp, BN_CTX *ctx)
  85.   {

crypto/bn/bn_recp.c:120:23: Call
  118.       /* Nr := round(2^i / N) */
  119.       if (i != recp->shift)
  120.           recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
                               ^
  121.       /* BN_reciprocal could have returned -1 for an error */
  122.       if (recp->shift == -1)

crypto/bn/bn_recp.c:173:1: Parameter `*r->d`
    171.    */
    172.   /* r := 2^len / m */
    173. > int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
    174.   {
    175.       int ret = -1;

crypto/bn/bn_recp.c:185:10: Call
    183.           goto err;
    184.   
    185.       if (!BN_div(r, NULL, t, m, ctx))
                    ^
    186.           goto err;
    187.   

crypto/bn/bn_div.c:137:1: Parameter `*dv->d`
      135.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
      136.    */
      137. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
      138.              BN_CTX *ctx)
      139.   {

crypto/bn/bn_div.c:251:10: Call
      249.   
      250.       /* Setup to 'res' */
      251.       if (!bn_wexpand(res, (loop + 1)))
                      ^
      252.           goto err;
      253.       res->neg = (num->neg ^ divisor->neg);

crypto/bn/bn_lib.c:937:1: Parameter `*a->d`
        935.   }
        936.   
        937. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
        938.   {
        939.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:939:37: Call
        937.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
        938.   {
        939.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                                   ^
        940.   }
        941.   

crypto/bn/bn_lib.c:273:1: Parameter `*b->d`
          271.    */
          272.   
          273. > BIGNUM *bn_expand2(BIGNUM *b, int words)
          274.   {
          275.       bn_check_top(b);

crypto/bn/bn_lib.c:278:23: Call
          276.   
          277.       if (words > b->dmax) {
          278.           BN_ULONG *a = bn_expand_internal(b, words);
                                       ^
          279.           if (!a)
          280.               return NULL;

crypto/bn/bn_lib.c:235:1: <Offset trace>
233.   /* This is used by bn_expand2() */
234.   /* The caller MUST check that words > b->dmax before calling this */
235. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
236.   {
237.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:235:1: Parameter `b->top`
            233.   /* This is used by bn_expand2() */
            234.   /* The caller MUST check that words > b->dmax before calling this */
            235. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
            236.   {
            237.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:235:1: <Length trace>
233.   /* This is used by bn_expand2() */
234.   /* The caller MUST check that words > b->dmax before calling this */
235. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
236.   {
237.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:235:1: Parameter `*b->d`
            233.   /* This is used by bn_expand2() */
            234.   /* The caller MUST check that words > b->dmax before calling this */
            235. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
            236.   {
            237.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:260:9: Array access: Offset added: [8, +oo] Size: [0, +oo] by call to `BN_div_recp` 
            258.       assert(b->top <= words);
            259.       if (b->top > 0)
            260.           memcpy(a, b->d, sizeof(*a) * b->top);
                           ^
            261.   
            262.       return a;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *a = NULL;
    bn_check_top(b);
    if (words > (INT_MAX / (4 * BN_BITS2))) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_SECURE))
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
    else
        a = OPENSSL_zalloc(words * sizeof(*a));
    if (a == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    assert(b->top <= words);
    if (b->top > 0)
        memcpy(a, b->d, sizeof(*a) * b->top);
    return a;
}
['static int test_div_recip(void)\n{\n    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;\n    BN_RECP_CTX *recp = NULL;\n    int st = 0, i;\n    if (!TEST_ptr(a = BN_new())\n            || !TEST_ptr(b = BN_new())\n            || !TEST_ptr(c = BN_new())\n            || !TEST_ptr(d = BN_new())\n            || !TEST_ptr(e = BN_new())\n            || !TEST_ptr(recp = BN_RECP_CTX_new()))\n        goto err;\n    for (i = 0; i < NUM0 + NUM1; i++) {\n        if (i < NUM1) {\n            BN_bntest_rand(a, 400, 0, 0);\n            BN_copy(b, a);\n            BN_lshift(a, a, i);\n            BN_add_word(a, i);\n        } else\n            BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);\n        BN_set_negative(a, rand_neg());\n        BN_set_negative(b, rand_neg());\n        BN_RECP_CTX_set(recp, b, ctx);\n        BN_div_recp(d, c, a, recp, ctx);\n        BN_mul(e, d, b, ctx);\n        BN_add(d, e, c);\n        BN_sub(d, d, a);\n        if (!TEST_BN_eq_zero(d))\n            goto err;\n    }\n    st = 1;\nerr:\n    BN_free(a);\n    BN_free(b);\n    BN_free(c);\n    BN_free(d);\n    BN_free(e);\n    BN_RECP_CTX_free(recp);\n    return st;\n}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n{\n    if (!BN_copy(&(recp->N), d))\n        return 0;\n    BN_zero(&(recp->Nr));\n    recp->num_bits = BN_num_bits(d);\n    recp->shift = 0;\n    return 1;\n}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n                BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n    int i, j, ret = 0;\n    BIGNUM *a, *b, *d, *r;\n    BN_CTX_start(ctx);\n    d = (dv != NULL) ? dv : BN_CTX_get(ctx);\n    r = (rem != NULL) ? rem : BN_CTX_get(ctx);\n    a = BN_CTX_get(ctx);\n    b = BN_CTX_get(ctx);\n    if (b == NULL)\n        goto err;\n    if (BN_ucmp(m, &(recp->N)) < 0) {\n        BN_zero(d);\n        if (!BN_copy(r, m)) {\n            BN_CTX_end(ctx);\n            return 0;\n        }\n        BN_CTX_end(ctx);\n        return 1;\n    }\n    i = BN_num_bits(m);\n    j = recp->num_bits << 1;\n    if (j > i)\n        i = j;\n    if (i != recp->shift)\n        recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n    if (recp->shift == -1)\n        goto err;\n    if (!BN_rshift(a, m, recp->num_bits))\n        goto err;\n    if (!BN_mul(b, a, &(recp->Nr), ctx))\n        goto err;\n    if (!BN_rshift(d, b, i - recp->num_bits))\n        goto err;\n    d->neg = 0;\n    if (!BN_mul(b, &(recp->N), d, ctx))\n        goto err;\n    if (!BN_usub(r, m, b))\n        goto err;\n    r->neg = 0;\n    j = 0;\n    while (BN_ucmp(r, &(recp->N)) >= 0) {\n        if (j++ > 2) {\n            BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n            goto err;\n        }\n        if (!BN_usub(r, r, &(recp->N)))\n            goto err;\n        if (!BN_add_word(d, 1))\n            goto err;\n    }\n    r->neg = BN_is_zero(r) ? 0 : m->neg;\n    d->neg = m->neg ^ recp->N.neg;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    bn_check_top(dv);\n    bn_check_top(rem);\n    return ret;\n}', 'int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)\n{\n    int ret = -1;\n    BIGNUM *t;\n    BN_CTX_start(ctx);\n    if ((t = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (!BN_set_bit(t, len))\n        goto err;\n    if (!BN_div(r, NULL, t, m, ctx))\n        goto err;\n    ret = len;\n err:\n    bn_check_top(r);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int norm_shift, i, loop;\n    BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnump;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    int no_branch = 0;\n    if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    bn_check_top(num);\n    bn_check_top(divisor);\n    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n        no_branch = 1;\n    }\n    bn_check_top(dv);\n    bn_check_top(rm);\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return 0;\n    }\n    if (!no_branch && BN_ucmp(num, divisor) < 0) {\n        if (rm != NULL) {\n            if (BN_copy(rm, num) == NULL)\n                return 0;\n        }\n        if (dv != NULL)\n            BN_zero(dv);\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (sdiv == NULL)\n        goto err;\n    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n    if (!(BN_lshift(sdiv, divisor, norm_shift)))\n        goto err;\n    sdiv->neg = 0;\n    norm_shift += BN_BITS2;\n    if (!(BN_lshift(snum, num, norm_shift)))\n        goto err;\n    snum->neg = 0;\n    if (no_branch) {\n        if (snum->top <= sdiv->top + 1) {\n            if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n                goto err;\n            for (i = snum->top; i < sdiv->top + 2; i++)\n                snum->d[i] = 0;\n            snum->top = sdiv->top + 2;\n        } else {\n            if (bn_wexpand(snum, snum->top + 1) == NULL)\n                goto err;\n            snum->d[snum->top] = 0;\n            snum->top++;\n        }\n    }\n    div_n = sdiv->top;\n    num_n = snum->top;\n    loop = num_n - div_n;\n    wnum.neg = 0;\n    wnum.d = &(snum->d[loop]);\n    wnum.top = div_n;\n    wnum.dmax = snum->dmax - loop;\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    wnump = &(snum->d[num_n - 1]);\n    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\n    res->neg = (num->neg ^ divisor->neg);\n    res->top = loop - no_branch;\n    resp = &(res->d[loop - 1]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    if (!no_branch) {\n        if (BN_ucmp(&wnum, sdiv) >= 0) {\n            bn_clear_top2max(&wnum);\n            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n            *resp = 1;\n        } else\n            res->top--;\n    }\n    resp++;\n    if (res->top == 0)\n        res->neg = 0;\n    else\n        resp--;\n    for (i = 0; i < loop - 1; i++, wnump--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n        BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n        q = bn_div_3_words(wnump, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnump[0];\n        n1 = wnump[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n#  ifdef BN_LLONG\n            BN_ULLONG t2;\n#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n#   else\n            q = bn_div_words(n0, n1, d0);\n#   endif\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n            t2 = (BN_ULLONG) d1 *q;\n            for (;;) {\n                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                t2 -= d1;\n            }\n#  else\n            BN_ULONG t2l, t2h;\n            q = bn_div_words(n0, n1, d0);\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n#   if defined(BN_UMULT_LOHI)\n            BN_UMULT_LOHI(t2l, t2h, d1, q);\n#   elif defined(BN_UMULT_HIGH)\n            t2l = d1 * q;\n            t2h = BN_UMULT_HIGH(d1, q);\n#   else\n            {\n                BN_ULONG ql, qh;\n                t2l = LBITS(d1);\n                t2h = HBITS(d1);\n                ql = LBITS(q);\n                qh = HBITS(q);\n                mul64(t2l, t2h, ql, qh);\n            }\n#   endif\n            for (;;) {\n                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                if (t2l < d1)\n                    t2h--;\n                t2l -= d1;\n            }\n#  endif\n        }\n# endif\n        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n        tmp->d[div_n] = l0;\n        wnum.d--;\n        if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n            q--;\n            if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n                (*wnump)++;\n        }\n        resp--;\n        *resp = q;\n    }\n    bn_correct_top(snum);\n    if (rm != NULL) {\n        int neg = num->neg;\n        BN_rshift(rm, snum, norm_shift);\n        if (!BN_is_zero(rm))\n            rm->neg = neg;\n        bn_check_top(rm);\n    }\n    if (no_branch)\n        bn_correct_top(res);\n    BN_CTX_end(ctx);\n    return 1;\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return 0;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n    bn_check_top(b);\n    if (words > b->dmax) {\n        BN_ULONG *a = bn_expand_internal(b, words);\n        if (!a)\n            return NULL;\n        if (b->d) {\n            OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n            bn_free_d(b);\n        }\n        b->d = a;\n        b->dmax = words;\n    }\n    bn_check_top(b);\n    return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n    BN_ULONG *a = NULL;\n    bn_check_top(b);\n    if (words > (INT_MAX / (4 * BN_BITS2))) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_SECURE))\n        a = OPENSSL_secure_zalloc(words * sizeof(*a));\n    else\n        a = OPENSSL_zalloc(words * sizeof(*a));\n    if (a == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    assert(b->top <= words);\n    if (b->top > 0)\n        memcpy(a, b->d, sizeof(*a) * b->top);\n    return a;\n}']