crypto/bn/bn_div.c:415: error: INTEGER_OVERFLOW_L1
  ([-oo, 0] - 1):unsigned64.
Showing all 4 steps of the trace


crypto/bn/bn_div.c:273:13: <LHS trace>
271.               if (bn_wexpand(snum, snum->top + 1) == NULL)
272.                   goto err;
273.               snum->d[snum->top] = 0;
                   ^
274.               snum->top++;
275.           }

crypto/bn/bn_div.c:273:13: Assignment
271.               if (bn_wexpand(snum, snum->top + 1) == NULL)
272.                   goto err;
273.               snum->d[snum->top] = 0;
                   ^
274.               snum->top++;
275.           }

crypto/bn/bn_div.c:296:5: Assignment
294.       /* Get the top 2 words of sdiv */
295.       /* div_n=sdiv->top; */
296.       d0 = sdiv->d[div_n - 1];
           ^
297.       d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
298.   

crypto/bn/bn_div.c:415:21: Binary operation: ([-oo, 0] - 1):unsigned64
413.                       break;      /* don't let rem overflow */
414.                   if (t2l < d1)
415.                       t2h--;
                           ^
416.                   t2l -= d1;
417.               }

int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
           BN_CTX *ctx)
{
    int norm_shift, i, j, loop;
    BIGNUM *tmp, wnum, *snum, *sdiv, *res;
    BN_ULONG *resp, *wnump;
    BN_ULONG d0, d1;
    int num_n, div_n;
    int no_branch = 0;
    if ((num->top > 0 && num->d[num->top - 1] == 0) ||
        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
        return 0;
    }
    bn_check_top(num);
    bn_check_top(divisor);
    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)
        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
        no_branch = 1;
    }
    bn_check_top(dv);
    bn_check_top(rm);
    if (BN_is_zero(divisor)) {
        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
        return 0;
    }
    if (!no_branch && BN_ucmp(num, divisor) < 0) {
        if (rm != NULL) {
            if (BN_copy(rm, num) == NULL)
                return 0;
        }
        if (dv != NULL)
            BN_zero(dv);
        return 1;
    }
    BN_CTX_start(ctx);
    res = (dv == NULL) ? BN_CTX_get(ctx) : dv;
    tmp = BN_CTX_get(ctx);
    snum = BN_CTX_get(ctx);
    sdiv = BN_CTX_get(ctx);
    if (sdiv == NULL)
        goto err;
    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
    if (!(BN_lshift(sdiv, divisor, norm_shift)))
        goto err;
    sdiv->neg = 0;
    norm_shift += BN_BITS2;
    if (!(BN_lshift(snum, num, norm_shift)))
        goto err;
    snum->neg = 0;
    if (no_branch) {
        if (snum->top <= sdiv->top + 1) {
            if (bn_wexpand(snum, sdiv->top + 2) == NULL)
                goto err;
            for (i = snum->top; i < sdiv->top + 2; i++)
                snum->d[i] = 0;
            snum->top = sdiv->top + 2;
        } else {
            if (bn_wexpand(snum, snum->top + 1) == NULL)
                goto err;
            snum->d[snum->top] = 0;
            snum->top++;
        }
    }
    div_n = sdiv->top;
    num_n = snum->top;
    loop = num_n - div_n;
    wnum.neg = 0;
    wnum.d = &(snum->d[loop]);
    wnum.top = div_n;
    wnum.flags = BN_FLG_STATIC_DATA;
    wnum.dmax = snum->dmax - loop;
    d0 = sdiv->d[div_n - 1];
    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
    wnump = &(snum->d[num_n - 1]);
    if (!bn_wexpand(res, (loop + 1)))
        goto err;
    res->neg = (num->neg ^ divisor->neg);
    res->top = loop - no_branch;
    resp = &(res->d[loop - 1]);
    if (!bn_wexpand(tmp, (div_n + 1)))
        goto err;
    if (!no_branch) {
        if (BN_ucmp(&wnum, sdiv) >= 0) {
            bn_clear_top2max(&wnum);
            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
            *resp = 1;
        } else
            res->top--;
    }
    resp++;
    if (res->top == 0)
        res->neg = 0;
    else
        resp--;
    for (i = 0; i < loop - 1; i++, wnump--) {
        BN_ULONG q, l0;
# if defined(BN_DIV3W)
        q = bn_div_3_words(wnump, d1, d0);
# else
        BN_ULONG n0, n1, rem = 0;
        n0 = wnump[0];
        n1 = wnump[-1];
        if (n0 == d0)
            q = BN_MASK2;
        else {
#  ifdef BN_LLONG
            BN_ULLONG t2;
#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
#   else
            q = bn_div_words(n0, n1, d0);
#   endif
#   ifndef REMAINDER_IS_ALREADY_CALCULATED
            rem = (n1 - q * d0) & BN_MASK2;
#   endif
            t2 = (BN_ULLONG) d1 *q;
            for (;;) {
                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
                    break;
                q--;
                rem += d0;
                if (rem < d0)
                    break;
                t2 -= d1;
            }
#  else
            BN_ULONG t2l, t2h;
            q = bn_div_words(n0, n1, d0);
#   ifndef REMAINDER_IS_ALREADY_CALCULATED
            rem = (n1 - q * d0) & BN_MASK2;
#   endif
#   if defined(BN_UMULT_LOHI)
            BN_UMULT_LOHI(t2l, t2h, d1, q);
#   elif defined(BN_UMULT_HIGH)
            t2l = d1 * q;
            t2h = BN_UMULT_HIGH(d1, q);
#   else
            {
                BN_ULONG ql, qh;
                t2l = LBITS(d1);
                t2h = HBITS(d1);
                ql = LBITS(q);
                qh = HBITS(q);
                mul64(t2l, t2h, ql, qh);
            }
#   endif
            for (;;) {
                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
                    break;
                q--;
                rem += d0;
                if (rem < d0)
                    break;
                if (t2l < d1)
                    t2h--;
                t2l -= d1;
            }
#  endif
        }
# endif
        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
        tmp->d[div_n] = l0;
        wnum.d--;
        l0 = bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1);
        q -= l0;
        for (l0 = 0 - l0, j = 0; j < div_n; j++)
            tmp->d[j] = sdiv->d[j] & l0;
        l0 = bn_add_words(wnum.d, wnum.d, tmp->d, div_n);
        (*wnump) += l0;
        resp--;
        *resp = q;
    }
    bn_correct_top(snum);
    if (rm != NULL) {
        int neg = num->neg;
        BN_rshift(rm, snum, norm_shift);
        if (!BN_is_zero(rm))
            rm->neg = neg;
        bn_check_top(rm);
    }
    if (no_branch)
        bn_correct_top(res);
    BN_CTX_end(ctx);
    return 1;
 err:
    bn_check_top(rm);
    BN_CTX_end(ctx);
    return 0;
}
['int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int norm_shift, i, j, 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.flags = BN_FLG_STATIC_DATA;\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)\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        l0 = bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1);\n        q -= l0;\n        for (l0 = 0 - l0, j = 0; j < div_n; j++)\n            tmp->d[j] = sdiv->d[j] & l0;\n        l0 = bn_add_words(wnum.d, wnum.d, tmp->d, div_n);\n        (*wnump) += l0;\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}']