test/bntest.c:929: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_add`.
Showing all 25 steps of the trace


test/bntest.c:925:17: Call
923.       if (!TEST_ptr(a = getBN(s, "A"))
924.               || !TEST_ptr(b = getBN(s, "B"))
925.               || !TEST_ptr(sum = getBN(s, "Sum"))
                       ^
926.               || !TEST_ptr(ret = BN_new()))
927.           goto err;

test/bntest.c:119:9: Call
  117.       }
  118.   
  119.       if (parseBN(&ret, hex) != (int)strlen(hex)) {
                 ^
  120.           TEST_error("Could not decode '%s'", hex);
  121.           return NULL;

test/bntest.c:100:12: Call
    98.   {
    99.       *out = NULL;
    100.       return BN_hex2bn(out, in);
                      ^
    101.   }
    102.   

crypto/bn/bn_print.c:126:1: Parameter `(*bn)->top`
      124.   }
      125.   
      126. > int BN_hex2bn(BIGNUM **bn, const char *a)
      127.   {
      128.       BIGNUM *ret = NULL;

test/bntest.c:929:10: Call
927.           goto err;
928.   
929.       if (!TEST_true(BN_add(ret, a, b))
                ^
930.               || !equalBN("A + B", sum, ret)
931.               || !TEST_true(BN_sub(ret, sum, a))

crypto/bn/bn_add.c:14:1: Parameter `r->top`
  12.   
  13.   /* r can == a or b */
  14. > int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
  15.   {
  16.       int a_neg = a->neg, ret;

crypto/bn/bn_add.c:40:18: Call
  38.   
  39.           if (BN_ucmp(a, b) < 0) {
  40.               if (!BN_usub(r, b, a))
                         ^
  41.                   return 0;
  42.               r->neg = 1;

crypto/bn/bn_add.c:107:1: Parameter `r->top`
    105.   
    106.   /* unsigned subtraction of b from a, a must be larger than b. */
    107. > int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
    108.   {
    109.       int max, min, dif;

crypto/bn/bn_add.c:125:9: Call
    123.       }
    124.   
    125.       if (bn_wexpand(r, max) == NULL)
                   ^
    126.           return 0;
    127.   

crypto/bn/bn_lib.c:948:1: Parameter `a->top`
      946.   }
      947.   
      948. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
      949.   {
      950.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:950:37: Call
      948.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
      949.   {
      950.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                                 ^
      951.   }
      952.   

crypto/bn/bn_lib.c:284:1: Parameter `b->top`
        282.    */
        283.   
        284. > BIGNUM *bn_expand2(BIGNUM *b, int words)
        285.   {
        286.       bn_check_top(b);

crypto/bn/bn_lib.c:289:23: Call
        287.   
        288.       if (words > b->dmax) {
        289.           BN_ULONG *a = bn_expand_internal(b, words);
                                     ^
        290.           if (!a)
        291.               return NULL;

crypto/bn/bn_lib.c:246:1: <Offset trace>
244.   /* This is used by bn_expand2() */
245.   /* The caller MUST check that words > b->dmax before calling this */
246. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
247.   {
248.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:246:1: Parameter `b->top`
          244.   /* This is used by bn_expand2() */
          245.   /* The caller MUST check that words > b->dmax before calling this */
          246. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
          247.   {
          248.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:246:1: <Length trace>
244.   /* This is used by bn_expand2() */
245.   /* The caller MUST check that words > b->dmax before calling this */
246. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
247.   {
248.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:246:1: Parameter `words`
          244.   /* This is used by bn_expand2() */
          245.   /* The caller MUST check that words > b->dmax before calling this */
          246. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
          247.   {
          248.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:263:13: Call
          261.           a = OPENSSL_secure_zalloc(words * sizeof(*a));
          262.       else
          263.           a = OPENSSL_zalloc(words * sizeof(*a));
                             ^
          264.       if (a == NULL) {
          265.           BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);

crypto/mem.c:186:1: Parameter `num`
            184.   }
            185.   
            186. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
            187.   {
            188.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:188:17: Call
            186.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
            187.   {
            188.       void *ret = CRYPTO_malloc(num, file, line);
                                   ^
            189.   
            190.       FAILTEST();

crypto/mem.c:166:9: Assignment
              164.   
              165.       if (num == 0)
              166.           return NULL;
                             ^
              167.   
              168.       FAILTEST();

crypto/mem.c:188:5: Assignment
            186.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
            187.   {
            188.       void *ret = CRYPTO_malloc(num, file, line);
                       ^
            189.   
            190.       FAILTEST();

crypto/mem.c:193:5: Assignment
            191.       if (ret != NULL)
            192.           memset(ret, 0, num);
            193.       return ret;
                       ^
            194.   }
            195.   

crypto/bn/bn_lib.c:263:9: Assignment
          261.           a = OPENSSL_secure_zalloc(words * sizeof(*a));
          262.       else
          263.           a = OPENSSL_zalloc(words * sizeof(*a));
                         ^
          264.       if (a == NULL) {
          265.           BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_lib.c:271:9: Array access: Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_add` 
          269.       assert(b->top <= words);
          270.       if (b->top > 0)
          271.           memcpy(a, b->d, sizeof(*a) * b->top);
                         ^
          272.   
          273.       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 file_sum(STANZA *s)\n{\n    BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;\n    BN_ULONG b_word;\n    int st = 0;\n    if (!TEST_ptr(a = getBN(s, "A"))\n            || !TEST_ptr(b = getBN(s, "B"))\n            || !TEST_ptr(sum = getBN(s, "Sum"))\n            || !TEST_ptr(ret = BN_new()))\n        goto err;\n    if (!TEST_true(BN_add(ret, a, b))\n            || !equalBN("A + B", sum, ret)\n            || !TEST_true(BN_sub(ret, sum, a))\n            || !equalBN("Sum - A", b, ret)\n            || !TEST_true(BN_sub(ret, sum, b))\n            || !equalBN("Sum - B", a, ret))\n        goto err;\n    if (!TEST_true(BN_copy(ret, a))\n            || !TEST_true(BN_add(ret, ret, b))\n            || !equalBN("A + B (r is a)", sum, ret)\n            || !TEST_true(BN_copy(ret, b))\n            || !TEST_true(BN_add(ret, a, ret))\n            || !equalBN("A + B (r is b)", sum, ret)\n            || !TEST_true(BN_copy(ret, sum))\n            || !TEST_true(BN_sub(ret, ret, a))\n            || !equalBN("Sum - A (r is a)", b, ret)\n            || !TEST_true(BN_copy(ret, a))\n            || !TEST_true(BN_sub(ret, sum, ret))\n            || !equalBN("Sum - A (r is b)", b, ret)\n            || !TEST_true(BN_copy(ret, sum))\n            || !TEST_true(BN_sub(ret, ret, b))\n            || !equalBN("Sum - B (r is a)", a, ret)\n            || !TEST_true(BN_copy(ret, b))\n            || !TEST_true(BN_sub(ret, sum, ret))\n            || !equalBN("Sum - B (r is b)", a, ret))\n        goto err;\n    if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {\n        if (!TEST_true(BN_uadd(ret, a, b))\n                || !equalBN("A +u B", sum, ret)\n                || !TEST_true(BN_usub(ret, sum, a))\n                || !equalBN("Sum -u A", b, ret)\n                || !TEST_true(BN_usub(ret, sum, b))\n                || !equalBN("Sum -u B", a, ret))\n            goto err;\n        if (!TEST_true(BN_copy(ret, a))\n                || !TEST_true(BN_uadd(ret, ret, b))\n                || !equalBN("A +u B (r is a)", sum, ret)\n                || !TEST_true(BN_copy(ret, b))\n                || !TEST_true(BN_uadd(ret, a, ret))\n                || !equalBN("A +u B (r is b)", sum, ret)\n                || !TEST_true(BN_copy(ret, sum))\n                || !TEST_true(BN_usub(ret, ret, a))\n                || !equalBN("Sum -u A (r is a)", b, ret)\n                || !TEST_true(BN_copy(ret, a))\n                || !TEST_true(BN_usub(ret, sum, ret))\n                || !equalBN("Sum -u A (r is b)", b, ret)\n                || !TEST_true(BN_copy(ret, sum))\n                || !TEST_true(BN_usub(ret, ret, b))\n                || !equalBN("Sum -u B (r is a)", a, ret)\n                || !TEST_true(BN_copy(ret, b))\n                || !TEST_true(BN_usub(ret, sum, ret))\n                || !equalBN("Sum -u B (r is b)", a, ret))\n            goto err;\n    }\n    b_word = BN_get_word(b);\n    if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {\n        if (!TEST_true(BN_copy(ret, a))\n                || !TEST_true(BN_add_word(ret, b_word))\n                || !equalBN("A + B (word)", sum, ret)\n                || !TEST_true(BN_copy(ret, sum))\n                || !TEST_true(BN_sub_word(ret, b_word))\n                || !equalBN("Sum - B (word)", a, ret))\n            goto err;\n    }\n    st = 1;\nerr:\n    BN_free(a);\n    BN_free(b);\n    BN_free(sum);\n    BN_free(ret);\n    return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n    const char *hex;\n    BIGNUM *ret = NULL;\n    if ((hex = findattr(s, attribute)) == NULL) {\n        TEST_error("Can\'t find %s in test at line %d", attribute, s->start);\n        return NULL;\n    }\n    if (parseBN(&ret, hex) != (int)strlen(hex)) {\n        TEST_error("Could not decode \'%s\'", hex);\n        return NULL;\n    }\n    return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n    *out = NULL;\n    return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n    BIGNUM *ret = NULL;\n    BN_ULONG l = 0;\n    int neg = 0, h, m, i, j, k, c;\n    int num;\n    if ((a == NULL) || (*a == '\\0'))\n        return (0);\n    if (*a == '-') {\n        neg = 1;\n        a++;\n    }\n    for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)\n        continue;\n    if (i == 0 || i > INT_MAX/4)\n        goto err;\n    num = i + neg;\n    if (bn == NULL)\n        return (num);\n    if (*bn == NULL) {\n        if ((ret = BN_new()) == NULL)\n            return (0);\n    } else {\n        ret = *bn;\n        BN_zero(ret);\n    }\n    if (bn_expand(ret, i * 4) == NULL)\n        goto err;\n    j = i;\n    m = 0;\n    h = 0;\n    while (j > 0) {\n        m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;\n        l = 0;\n        for (;;) {\n            c = a[j - m];\n            k = OPENSSL_hexchar2int(c);\n            if (k < 0)\n                k = 0;\n            l = (l << 4) | k;\n            if (--m <= 0) {\n                ret->d[h++] = l;\n                break;\n            }\n        }\n        j -= (BN_BYTES * 2);\n    }\n    ret->top = h;\n    bn_correct_top(ret);\n    *bn = ret;\n    bn_check_top(ret);\n    if (ret->top != 0)\n        ret->neg = neg;\n    return (num);\n err:\n    if (*bn == NULL)\n        BN_free(ret);\n    return (0);\n}", 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int a_neg = a->neg, ret;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a_neg ^ b->neg) {\n        if (a_neg) {\n            const BIGNUM *tmp;\n            tmp = a;\n            a = b;\n            b = tmp;\n        }\n        if (BN_ucmp(a, b) < 0) {\n            if (!BN_usub(r, b, a))\n                return 0;\n            r->neg = 1;\n        } else {\n            if (!BN_usub(r, a, b))\n                return 0;\n            r->neg = 0;\n        }\n        return 1;\n    }\n    ret = BN_uadd(r, a, b);\n    r->neg = a_neg;\n    bn_check_top(r);\n    return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int max, min, dif;\n    BN_ULONG t1, t2, borrow, *rp;\n    const BN_ULONG *ap, *bp;\n    bn_check_top(a);\n    bn_check_top(b);\n    max = a->top;\n    min = b->top;\n    dif = max - min;\n    if (dif < 0) {\n        BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n        return 0;\n    }\n    if (bn_wexpand(r, max) == NULL)\n        return 0;\n    ap = a->d;\n    bp = b->d;\n    rp = r->d;\n    borrow = bn_sub_words(rp, ap, bp, min);\n    ap += min;\n    rp += min;\n    while (dif) {\n        dif--;\n        t1 = *(ap++);\n        t2 = (t1 - borrow) & BN_MASK2;\n        *(rp++) = t2;\n        borrow &= (t1 == 0);\n    }\n    r->top = max;\n    r->neg = 0;\n    bn_correct_top(r);\n    return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\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}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\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    FAILTEST();\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}']