test/bntest.c:1366: error: INTEGER_OVERFLOW_L2
  (0 - [0, 18446744073709551615]):unsigned64 by call to `BN_div_word`.
Showing all 13 steps of the trace


test/bntest.c:1347:10: Call
1345.           goto err;
1346.   
1347.       if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
                 ^
1348.               || !equalBN("A / B", quotient, ret)
1349.               || !equalBN("A % B", remainder, ret2)

crypto/bn/bn_div.c:188:1: Parameter `*rm->d`
  186.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
  187.    */
  188. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
  189.              BN_CTX *ctx)
  190.   {

test/bntest.c:1359:14: Call
1357.        * small enough.
1358.        */
1359.       b_word = BN_get_word(b);
                     ^
1360.       if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1361.           BN_ULONG remainder_word = BN_get_word(remainder);

crypto/bn/bn_lib.c:352:9: Assignment
  350.   {
  351.       if (a->top > 1)
  352.           return BN_MASK2;
                 ^
  353.       else if (a->top == 1)
  354.           return a->d[0];

test/bntest.c:1359:5: Assignment
1357.        * small enough.
1358.        */
1359.       b_word = BN_get_word(b);
            ^
1360.       if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1361.           BN_ULONG remainder_word = BN_get_word(remainder);

test/bntest.c:1366:20: Call
1364.           if (!TEST_ptr(BN_copy(ret, a)))
1365.               goto err;
1366.           ret_word = BN_div_word(ret, b_word);
                           ^
1367.           if (ret_word != remainder_word) {
1368.   #ifdef BN_DEC_FMT1

crypto/bn/bn_word.c:61:1: Parameter `w`
  59.   }
  60.   
  61. > BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)
  62.   {
  63.       BN_ULONG ret = 0;

crypto/bn/bn_word.c:67:5: Assignment
  65.   
  66.       bn_check_top(a);
  67.       w &= BN_MASK2;
            ^
  68.   
  69.       if (!w)

crypto/bn/bn_word.c:76:20: Call
  74.   
  75.       /* normalize input (so bn_div_words doesn't complain) */
  76.       j = BN_BITS2 - BN_num_bits_word(w);
                           ^
  77.       w <<= j;
  78.       if (!BN_lshift(a, a, j))

crypto/bn/bn_lib.c:90:1: <RHS trace>
88.   }
89.   
90. > int BN_num_bits_word(BN_ULONG l)
91.   {
92.       BN_ULONG x, mask;

crypto/bn/bn_lib.c:90:1: Parameter `l`
    88.   }
    89.   
    90. > int BN_num_bits_word(BN_ULONG l)
    91.   {
    92.       BN_ULONG x, mask;

crypto/bn/bn_lib.c:96:5: Assignment
    94.   
    95.   #if BN_BITS2 > 32
    96.       x = l >> 32;
              ^
    97.       mask = (0 - x) & BN_MASK2;
    98.       mask = (0 - (mask >> (BN_BITS2 - 1)));

crypto/bn/bn_lib.c:97:5: Binary operation: (0 - [0, 18446744073709551615]):unsigned64 by call to `BN_div_word` 
    95.   #if BN_BITS2 > 32
    96.       x = l >> 32;
    97.       mask = (0 - x) & BN_MASK2;
               ^
    98.       mask = (0 - (mask >> (BN_BITS2 - 1)));
    99.       bits += 32 & mask;

int BN_num_bits_word(BN_ULONG l)
{
    BN_ULONG x, mask;
    int bits = (l != 0);
#if BN_BITS2 > 32
    x = l >> 32;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 32 & mask;
    l ^= (x ^ l) & mask;
#endif
    x = l >> 16;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 16 & mask;
    l ^= (x ^ l) & mask;
    x = l >> 8;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 8 & mask;
    l ^= (x ^ l) & mask;
    x = l >> 4;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 4 & mask;
    l ^= (x ^ l) & mask;
    x = l >> 2;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 2 & mask;
    l ^= (x ^ l) & mask;
    x = l >> 1;
    mask = (0 - x) & BN_MASK2;
    mask = (0 - (mask >> (BN_BITS2 - 1)));
    bits += 1 & mask;
    return bits;
}
['static int file_quotient(STANZA *s)\n{\n    BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;\n    BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;\n    BN_ULONG b_word, ret_word;\n    int st = 0;\n    if (!TEST_ptr(a = getBN(s, "A"))\n            || !TEST_ptr(b = getBN(s, "B"))\n            || !TEST_ptr(quotient = getBN(s, "Quotient"))\n            || !TEST_ptr(remainder = getBN(s, "Remainder"))\n            || !TEST_ptr(ret = BN_new())\n            || !TEST_ptr(ret2 = BN_new())\n            || !TEST_ptr(nnmod = BN_new()))\n        goto err;\n    if (!TEST_true(BN_div(ret, ret2, a, b, ctx))\n            || !equalBN("A / B", quotient, ret)\n            || !equalBN("A % B", remainder, ret2)\n            || !TEST_true(BN_mul(ret, quotient, b, ctx))\n            || !TEST_true(BN_add(ret, ret, remainder))\n            || !equalBN("Quotient * B + Remainder", a, ret))\n        goto err;\n    b_word = BN_get_word(b);\n    if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {\n        BN_ULONG remainder_word = BN_get_word(remainder);\n        assert(remainder_word != (BN_ULONG)-1);\n        if (!TEST_ptr(BN_copy(ret, a)))\n            goto err;\n        ret_word = BN_div_word(ret, b_word);\n        if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n            TEST_error(\n                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,\n                    ret_word, remainder_word);\n#else\n            TEST_error("Got A %% B (word) mismatch");\n#endif\n            goto err;\n        }\n        if (!equalBN ("A / B (word)", quotient, ret))\n            goto err;\n        ret_word = BN_mod_word(a, b_word);\n        if (ret_word != remainder_word) {\n#ifdef BN_DEC_FMT1\n            TEST_error(\n                    "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",\n                    ret_word, remainder_word);\n#else\n            TEST_error("Got A %% B (word) mismatch");\n#endif\n            goto err;\n        }\n    }\n    if (!BN_is_negative(b)) {\n        if (!TEST_true(BN_copy(nnmod, remainder))\n                || (BN_is_negative(nnmod)\n                        && !TEST_true(BN_add(nnmod, nnmod, b)))\n                || !TEST_true(BN_nnmod(ret, a, b, ctx))\n                || !equalBN("A % B (non-negative)", nnmod, ret))\n            goto err;\n    }\n    st = 1;\nerr:\n    BN_free(a);\n    BN_free(b);\n    BN_free(quotient);\n    BN_free(remainder);\n    BN_free(ret);\n    BN_free(ret2);\n    BN_free(nnmod);\n    return st;\n}', '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}', 'BN_ULONG BN_get_word(const BIGNUM *a)\n{\n    if (a->top > 1)\n        return BN_MASK2;\n    else if (a->top == 1)\n        return a->d[0];\n    return 0;\n}', 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n{\n    BN_ULONG ret = 0;\n    int i, j;\n    bn_check_top(a);\n    w &= BN_MASK2;\n    if (!w)\n        return (BN_ULONG)-1;\n    if (a->top == 0)\n        return 0;\n    j = BN_BITS2 - BN_num_bits_word(w);\n    w <<= j;\n    if (!BN_lshift(a, a, j))\n        return (BN_ULONG)-1;\n    for (i = a->top - 1; i >= 0; i--) {\n        BN_ULONG l, d;\n        l = a->d[i];\n        d = bn_div_words(ret, l, w);\n        ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;\n        a->d[i] = d;\n    }\n    if ((a->top > 0) && (a->d[a->top - 1] == 0))\n        a->top--;\n    ret >>= j;\n    if (!a->top)\n        a->neg = 0;\n    bn_check_top(a);\n    return ret;\n}', 'int BN_num_bits_word(BN_ULONG l)\n{\n    BN_ULONG x, mask;\n    int bits = (l != 0);\n#if BN_BITS2 > 32\n    x = l >> 32;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 32 & mask;\n    l ^= (x ^ l) & mask;\n#endif\n    x = l >> 16;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 16 & mask;\n    l ^= (x ^ l) & mask;\n    x = l >> 8;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 8 & mask;\n    l ^= (x ^ l) & mask;\n    x = l >> 4;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 4 & mask;\n    l ^= (x ^ l) & mask;\n    x = l >> 2;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 2 & mask;\n    l ^= (x ^ l) & mask;\n    x = l >> 1;\n    mask = (0 - x) & BN_MASK2;\n    mask = (0 - (mask >> (BN_BITS2 - 1)));\n    bits += 1 & mask;\n    return bits;\n}']