Ticket #10435: trac_10435.patch

File trac_10435.patch, 4.3 KB (added by rlm, 10 years ago)
  • sage/misc/bitset.pxi

    # HG changeset patch
    # User Robert L. Miller <rlm@rlmiller.org>
    # Date 1291654487 0
    # Node ID 5a726e2f23854fd98fc6a91d3dbad8fbf96deb3b
    # Parent  b71ec0d61679a6d3b3c2daef99e3c2455702249f
    #10435: Invalid write in bitsets
    
    diff --git a/sage/misc/bitset.pxi b/sage/misc/bitset.pxi
    a b  
    263263    cdef long index = n >> index_shift
    264264    for i from 0 <= i < index:
    265265        bits.bits[i] = ~0
    266     bits.bits[index] = ((<unsigned long>1) << (n & offset_mask)) - 1
     266    if index < bits.limbs:
     267        bits.bits[index] = ((<unsigned long>1) << (n & offset_mask)) - 1
    267268    for i from index < i < bits.limbs:
    268269        bits.bits[i] = 0
    269270
  • sage/misc/misc_c.pyx

    diff --git a/sage/misc/misc_c.pyx b/sage/misc/misc_c.pyx
    a b  
    404404    """
    405405    Test the Cython bitset functions so we can have some relevant doctests.
    406406
    407     TESTS:
     407    TESTS::
     408
    408409        sage: from sage.misc.misc_c import test_bitset
    409410        sage: test_bitset('00101', '01110', 4)
    410411        a 00101
     
    446447        to size 8          00100000
    447448        to original size    00100
    448449
     450    ::
     451
    449452        sage: test_bitset('11101', '11001', 2)
    450453        a 11101
    451454        list a [0, 1, 2, 4]
     
    486489        to size 4          1100
    487490        to original size    11000
    488491
    489     Test a corner-case: a bitset that is a multiple of words
     492    Test a corner-case: a bitset that is a multiple of words::
     493
    490494        sage: test_bitset('00'*64, '01'*64, 127)
    491495        a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    492496        list a []
     
    537541        to size 254          00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    538542        to original size    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    539543
    540     Large enough to span multiple limbs.  We don't explicitly check the number of limbs below because it will be different in the 32 bit versus 64 bit cases:
     544    Large enough to span multiple limbs.  We don't explicitly check the number of limbs below because it will be different in the 32 bit versus 64 bit cases::
     545
    541546        sage: test_bitset('111001'*25, RealField(151)(pi).str(2)[2:], 69)
    542547        a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001
    543548        list a [0, 1, 2, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20, 23, 24, 25, 26, 29, 30, 31, 32, 35, 36, 37, 38, 41, 42, 43, 44, 47, 48, 49, 50, 53, 54, 55, 56, 59, 60, 61, 62, 65, 66, 67, 68, 71, 72, 73, 74, 77, 78, 79, 80, 83, 84, 85, 86, 89, 90, 91, 92, 95, 96, 97, 98, 101, 102, 103, 104, 107, 108, 109, 110, 113, 114, 115, 116, 119, 120, 121, 122, 125, 126, 127, 128, 131, 132, 133, 134, 137, 138, 139, 140, 143, 144, 145, 146, 149]
     
    587592        to size 69          111001111001111001111001111001111001111001111001111001111001111001111
    588593        to size 138          111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000
    589594        to original size    111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000000000000000
     595
    590596    """
    591597    cdef bint bit = True
    592598    cdef bitset_t a, b, r
     
    727733    bitset_free(r)
    728734    bitset_free(s)
    729735
     736def test_bitset_set_first_n(py_a, long n):
     737    """
     738    Test the bitset function set_first_n.
     739
     740    TESTS::
     741
     742        sage: from sage.misc.misc_c import test_bitset_set_first_n
     743        sage: test_bitset_set_first_n('00'*64, 128)
     744        a.set_first_n(n)    11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
     745
     746    """
     747    cdef bint bit = True
     748    cdef bitset_t a
     749
     750    bitset_from_str(a, py_a)
     751    bitset_set_first_n(a, n)
     752    print "a.set_first_n(n)   ", bitset_string(a)
     753    bitset_free(a)
    730754
    731755def test_bitset_remove(py_a, long n):
    732756    """