test/exptest.c:61: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_set_word`.
Showing all 15 steps of the trace


test/exptest.c:60:5: Call
58.           goto err;
59.   
60.       BN_one(m);
          ^
61.       BN_one(a);
62.       BN_zero(p);

crypto/bn/bn_lib.c:394:15: Assignment
  392.       a->neg = 0;
  393.       a->d[0] = w;
  394.       a->top = (w ? 1 : 0);
                       ^
  395.       bn_check_top(a);
  396.       return 1;

crypto/bn/bn_lib.c:394:5: Assignment
  392.       a->neg = 0;
  393.       a->d[0] = w;
  394.       a->top = (w ? 1 : 0);
             ^
  395.       bn_check_top(a);
  396.       return 1;

test/exptest.c:61:5: Call
59.   
60.       BN_one(m);
61.       BN_one(a);
          ^
62.       BN_zero(p);
63.   

crypto/bn/bn_lib.c:387:1: Parameter `*a->d`
  385.   }
  386.   
  387. > int BN_set_word(BIGNUM *a, BN_ULONG w)
  388.   {
  389.       bn_check_top(a);

crypto/bn/bn_lib.c:390:9: Call
  388.   {
  389.       bn_check_top(a);
  390.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                 ^
  391.           return 0;
  392.       a->neg = 0;

crypto/bn/bn_lcl.h:651:1: Parameter `*a->d`
    649.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
    650.   
    651. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
    652.   {
    653.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_lcl.h:659:12: Call
    657.           return a;
    658.   
    659.       return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
                      ^
    660.   }
    661.   

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, 536870848] by call to `BN_set_word` 
        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_mod_exp_zero(void)\n{\n    BIGNUM *a = NULL, *p = NULL, *m = NULL;\n    BIGNUM *r = NULL;\n    BN_ULONG one_word = 1;\n    BN_CTX *ctx = BN_CTX_new();\n    int ret = 1, failed = 0;\n    if (!TEST_ptr(m = BN_new())\n        || !TEST_ptr(a = BN_new())\n        || !TEST_ptr(p = BN_new())\n        || !TEST_ptr(r = BN_new()))\n        goto err;\n    BN_one(m);\n    BN_one(a);\n    BN_zero(p);\n    if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))\n        goto err;\n    if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))\n        goto err;\n    if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))\n        failed = 1;\n    if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))\n        goto err;\n    if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))\n        failed = 1;\n    if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))\n        goto err;\n    if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))\n        failed = 1;\n    if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))\n        goto err;\n    if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))\n        failed = 1;\n    if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))\n        goto err;\n    if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))\n        failed = 1;\n    if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))\n        goto err;\n    if (!TEST_BN_eq_zero(r)) {\n        TEST_error("BN_mod_exp_mont_word failed: "\n                   "1 ** 0 mod 1 = r (should be 0)");\n        BN_print_var(r);\n        goto err;\n    }\n    ret = !failed;\n err:\n    BN_free(r);\n    BN_free(a);\n    BN_free(p);\n    BN_free(m);\n    BN_CTX_free(ctx);\n    return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return 0;\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    bn_check_top(a);\n    return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\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}']