test/bntest.c:342: error: BUFFER_OVERRUN_L3
  Offset: [0, 555] Size: [1, 556] by call to `BN_bntest_rand`.
Showing all 13 steps of the trace


test/bntest.c:52:1: Assignment
50.   #include "../crypto/bn/bn_lcl.h"
51.   
52. > static const int num0 = 100;           /* number of tests */
53.   static const int num1 = 50;            /* additional tests for some functions */
54.   static const int num2 = 5;             /* number of tests for slow functions */

test/bntest.c:342:9: Call
340.       BN_bntest_rand(a, 512, 0, 0);
341.       for (i = 0; i < num0; i++) {
342.           BN_bntest_rand(b, 450 + i, 0, 0);
               ^
343.           a->neg = rand_neg();
344.           b->neg = rand_neg();

crypto/bn/bn_rand.c:103:1: Parameter `bits`
  101.   }
  102.   
  103. > int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  104.   {
  105.       return bnrand(2, rnd, bits, top, bottom);

crypto/bn/bn_rand.c:105:12: Call
  103.   int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  104.   {
  105.       return bnrand(2, rnd, bits, top, bottom);
                    ^
  106.   }
  107.   

crypto/bn/bn_rand.c:57:14: <Offset trace>
55.           unsigned char c;
56.   
57.           for (i = 0; i < bytes; i++) {
                   ^
58.               if (RAND_bytes(&c, 1) <= 0)
59.                   goto err;

crypto/bn/bn_rand.c:57:14: Assignment
    55.           unsigned char c;
    56.   
    57.           for (i = 0; i < bytes; i++) {
                       ^
    58.               if (RAND_bytes(&c, 1) <= 0)
    59.                   goto err;

crypto/bn/bn_rand.c:17:1: <Length trace>
15.   #include <openssl/sha.h>
16.   
17. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
18.   {
19.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:17:1: Parameter `bits`
    15.   #include <openssl/sha.h>
    16.   
    17. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
    18.   {
    19.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:33:5: Assignment
    31.       }
    32.   
    33.       bytes = (bits + 7) / 8;
              ^
    34.       bit = (bits - 1) % 8;
    35.       mask = 0xff << (bit + 1);

crypto/bn/bn_rand.c:37:11: Call
    35.       mask = 0xff << (bit + 1);
    36.   
    37.       buf = OPENSSL_malloc(bytes);
                    ^
    38.       if (buf == NULL) {
    39.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/mem.c:79:9: Assignment
      77.   
      78.       if (num <= 0)
      79.           return NULL;
                    ^
      80.   
      81.       allow_customize = 0;

crypto/bn/bn_rand.c:37:5: Assignment
    35.       mask = 0xff << (bit + 1);
    36.   
    37.       buf = OPENSSL_malloc(bytes);
              ^
    38.       if (buf == NULL) {
    39.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_rand.c:63:17: Array access: Offset: [0, 555] Size: [1, 556] by call to `BN_bntest_rand` 
    61.                   buf[i] = buf[i - 1];
    62.               else if (c < 42)
    63.                   buf[i] = 0;
                          ^
    64.               else if (c < 84)
    65.                   buf[i] = 255;

static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
{
    unsigned char *buf = NULL;
    int ret = 0, bit, bytes, mask;
    time_t tim;
    if (bits < 0 || (bits == 1 && top > 0)) {
        BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
        return 0;
    }
    if (bits == 0) {
        BN_zero(rnd);
        return 1;
    }
    bytes = (bits + 7) / 8;
    bit = (bits - 1) % 8;
    mask = 0xff << (bit + 1);
    buf = OPENSSL_malloc(bytes);
    if (buf == NULL) {
        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    time(&tim);
    RAND_add(&tim, sizeof(tim), 0.0);
    if (RAND_bytes(buf, bytes) <= 0)
        goto err;
    if (pseudorand == 2) {
        int i;
        unsigned char c;
        for (i = 0; i < bytes; i++) {
            if (RAND_bytes(&c, 1) <= 0)
                goto err;
            if (c >= 128 && i > 0)
                buf[i] = buf[i - 1];
            else if (c < 42)
                buf[i] = 0;
            else if (c < 84)
                buf[i] = 255;
        }
    }
    if (top >= 0) {
        if (top) {
            if (bit == 0) {
                buf[0] = 1;
                buf[1] |= 0x80;
            } else {
                buf[0] |= (3 << (bit - 1));
            }
        } else {
            buf[0] |= (1 << bit);
        }
    }
    buf[0] &= ~mask;
    if (bottom)
        buf[bytes - 1] |= 1;
    if (!BN_bin2bn(buf, bytes, rnd))
        goto err;
    ret = 1;
 err:
    OPENSSL_clear_free(buf, bytes);
    bn_check_top(rnd);
    return (ret);
}
['int test_add(BIO *bp)\n{\n    BIGNUM *a, *b, *c;\n    int i;\n    a = BN_new();\n    b = BN_new();\n    c = BN_new();\n    BN_bntest_rand(a, 512, 0, 0);\n    for (i = 0; i < num0; i++) {\n        BN_bntest_rand(b, 450 + i, 0, 0);\n        a->neg = rand_neg();\n        b->neg = rand_neg();\n        BN_add(c, a, b);\n        if (bp != NULL) {\n            if (!results) {\n                BN_print(bp, a);\n                BIO_puts(bp, " + ");\n                BN_print(bp, b);\n                BIO_puts(bp, " - ");\n            }\n            BN_print(bp, c);\n            BIO_puts(bp, "\\n");\n        }\n        a->neg = !a->neg;\n        b->neg = !b->neg;\n        BN_add(c, c, b);\n        BN_add(c, c, a);\n        if (!BN_is_zero(c)) {\n            fprintf(stderr, "Add test failed!\\n");\n            return 0;\n        }\n    }\n    BN_free(a);\n    BN_free(b);\n    BN_free(c);\n    return (1);\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(2, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n    unsigned char *buf = NULL;\n    int ret = 0, bit, bytes, mask;\n    time_t tim;\n    if (bits < 0 || (bits == 1 && top > 0)) {\n        BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n        return 0;\n    }\n    if (bits == 0) {\n        BN_zero(rnd);\n        return 1;\n    }\n    bytes = (bits + 7) / 8;\n    bit = (bits - 1) % 8;\n    mask = 0xff << (bit + 1);\n    buf = OPENSSL_malloc(bytes);\n    if (buf == NULL) {\n        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    time(&tim);\n    RAND_add(&tim, sizeof(tim), 0.0);\n    if (RAND_bytes(buf, bytes) <= 0)\n        goto err;\n    if (pseudorand == 2) {\n        int i;\n        unsigned char c;\n        for (i = 0; i < bytes; i++) {\n            if (RAND_bytes(&c, 1) <= 0)\n                goto err;\n            if (c >= 128 && i > 0)\n                buf[i] = buf[i - 1];\n            else if (c < 42)\n                buf[i] = 0;\n            else if (c < 84)\n                buf[i] = 255;\n        }\n    }\n    if (top >= 0) {\n        if (top) {\n            if (bit == 0) {\n                buf[0] = 1;\n                buf[1] |= 0x80;\n            } else {\n                buf[0] |= (3 << (bit - 1));\n            }\n        } else {\n            buf[0] |= (1 << bit);\n        }\n    }\n    buf[0] &= ~mask;\n    if (bottom)\n        buf[bytes - 1] |= 1;\n    if (!BN_bin2bn(buf, bytes, rnd))\n        goto err;\n    ret = 1;\n err:\n    OPENSSL_clear_free(buf, bytes);\n    bn_check_top(rnd);\n    return (ret);\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num <= 0)\n        return NULL;\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']