test/bntest.c:1243: error: BUFFER_OVERRUN_L3
  Offset: [0, 518] Size: [1, 519] by call to `BN_rand`.
Showing all 12 steps of the trace


test/bntest.c:1243:9: Call
1241.   
1242.       for (i = 0; i < num0; i++) {
1243.           BN_rand(a, 512, 0, 0);
                ^
1244.           BN_copy(b, BN_value_one());
1245.           a->neg = rand_neg();

crypto/bn/bn_rand.c:200:1: Parameter `bits`
  198.   }
  199.   
  200. > int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  201.   {
  202.       return bnrand(0, rnd, bits, top, bottom);

crypto/bn/bn_rand.c:202:12: Call
  200.   int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  201.   {
  202.       return bnrand(0, rnd, bits, top, bottom);
                    ^
  203.   }
  204.   

crypto/bn/bn_rand.c:164:14: <Offset trace>
162.           unsigned char c;
163.   
164.           for (i = 0; i < bytes; i++) {
                    ^
165.               if (RAND_bytes(&c, 1) <= 0)
166.                   goto err;

crypto/bn/bn_rand.c:164:14: Assignment
    162.           unsigned char c;
    163.   
    164.           for (i = 0; i < bytes; i++) {
                        ^
    165.               if (RAND_bytes(&c, 1) <= 0)
    166.                   goto err;

crypto/bn/bn_rand.c:119:1: <Length trace>
117.   #include <openssl/sha.h>
118.   
119. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
120.   {
121.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:119:1: Parameter `bits`
    117.   #include <openssl/sha.h>
    118.   
    119. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
    120.   {
    121.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:135:5: Assignment
    133.       }
    134.   
    135.       bytes = (bits + 7) / 8;
               ^
    136.       bit = (bits - 1) % 8;
    137.       mask = 0xff << (bit + 1);

crypto/bn/bn_rand.c:139:11: Call
    137.       mask = 0xff << (bit + 1);
    138.   
    139.       buf = OPENSSL_malloc(bytes);
                     ^
    140.       if (buf == NULL) {
    141.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/mem.c:125:9: Assignment
      123.   
      124.       if (num <= 0)
      125.           return NULL;
                     ^
      126.   
      127.       allow_customize = 0;

crypto/bn/bn_rand.c:139:5: Assignment
    137.       mask = 0xff << (bit + 1);
    138.   
    139.       buf = OPENSSL_malloc(bytes);
               ^
    140.       if (buf == NULL) {
    141.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_rand.c:170:17: Array access: Offset: [0, 518] Size: [1, 519] by call to `BN_rand` 
    168.                   buf[i] = buf[i - 1];
    169.               else if (c < 42)
    170.                   buf[i] = 0;
                           ^
    171.               else if (c < 84)
    172.                   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 (pseudorand) {
        if (RAND_bytes(buf, bytes) <= 0)
            goto err;
    } else {
        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_gf2m_add(BIO *bp)\n{\n    BIGNUM *a, *b, *c;\n    int i, ret = 0;\n    a = BN_new();\n    b = BN_new();\n    c = BN_new();\n    for (i = 0; i < num0; i++) {\n        BN_rand(a, 512, 0, 0);\n        BN_copy(b, BN_value_one());\n        a->neg = rand_neg();\n        b->neg = rand_neg();\n        BN_GF2m_add(c, a, b);\n        if ((BN_is_odd(a) && BN_is_odd(c))\n            || (!BN_is_odd(a) && !BN_is_odd(c))) {\n            fprintf(stderr, "GF(2^m) addition test (a) failed!\\n");\n            goto err;\n        }\n        BN_GF2m_add(c, c, c);\n        if (!BN_is_zero(c)) {\n            fprintf(stderr, "GF(2^m) addition test (b) failed!\\n");\n            goto err;\n        }\n    }\n    ret = 1;\n err:\n    BN_free(a);\n    BN_free(b);\n    BN_free(c);\n    return ret;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(0, 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 (pseudorand) {\n        if (RAND_bytes(buf, bytes) <= 0)\n            goto err;\n    } else {\n        if (RAND_bytes(buf, bytes) <= 0)\n            goto err;\n    }\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 (num <= 0)\n        return NULL;\n    allow_customize = 0;\n#ifdef 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    (void)file;\n    (void)line;\n    ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n    if (ret && (num > 2048)) {\n        extern unsigned char cleanse_ctr;\n        ((unsigned char *)ret)[0] = cleanse_ctr;\n    }\n#endif\n    return ret;\n}']