Changeset 2157 for trunk/src


Ignore:
Timestamp:
Dec 20, 2012, 7:14:03 PM (7 years ago)
Author:
sam
Message:

core: fix a memory corruption in the Array class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/core/array.h

    r2105 r2157  
    6262        if ((uintptr_t)this != (uintptr_t)&that)
    6363        {
     64            /* FIXME: there is an opportunity for optimisation here if we
     65             * find a way to ask Reserve not to create new elements, since
     66             * we're going to overwrite them anyway. */
    6467            if (m_reserved < that.m_count)
    6568            {
    66                 /* If not enough space, reserve memory and use placement
    67                  * new directly for all elements. */
     69                /* If not enough space, reserve memory, overwrite the first
     70                 * elements, then use placement new directly for the
     71                 * remaining elements. */
    6872                Reserve(that.m_count);
    69                 for (int i = 0; i < that.m_count; i++)
     73                for (int i = 0; i < m_count && i < that.m_count; i++)
     74                    m_data[i] = Element(that[i]);
     75                for (int i = m_count; i < that.m_count; i++)
    7076                    new(&m_data[i]) Element(that[i]);
    7177            }
     
    293299            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7; T8 tmp8 = m8;
    294300            this->Reserve(this->m_count * 13 / 8 + 8);
    295             this->m_data[this->m_count].m1 = tmp1;
    296             this->m_data[this->m_count].m2 = tmp2;
    297             this->m_data[this->m_count].m3 = tmp3;
    298             this->m_data[this->m_count].m4 = tmp4;
    299             this->m_data[this->m_count].m5 = tmp5;
    300             this->m_data[this->m_count].m6 = tmp6;
    301             this->m_data[this->m_count].m7 = tmp7;
    302             this->m_data[this->m_count].m8 = tmp8;
     301            new (&this->m_data[this->m_count].m1) T1(tmp1);
     302            new (&this->m_data[this->m_count].m2) T2(tmp2);
     303            new (&this->m_data[this->m_count].m3) T3(tmp3);
     304            new (&this->m_data[this->m_count].m4) T4(tmp4);
     305            new (&this->m_data[this->m_count].m5) T5(tmp5);
     306            new (&this->m_data[this->m_count].m6) T6(tmp6);
     307            new (&this->m_data[this->m_count].m7) T7(tmp7);
     308            new (&this->m_data[this->m_count].m8) T8(tmp8);
    303309        }
    304310        else
     
    332338            T5 tmp5 = m5; T6 tmp6 = m6; T7 tmp7 = m7;
    333339            this->Reserve(this->m_count * 13 / 8 + 8);
    334             this->m_data[this->m_count].m1 = tmp1;
    335             this->m_data[this->m_count].m2 = tmp2;
    336             this->m_data[this->m_count].m3 = tmp3;
    337             this->m_data[this->m_count].m4 = tmp4;
    338             this->m_data[this->m_count].m5 = tmp5;
    339             this->m_data[this->m_count].m6 = tmp6;
    340             this->m_data[this->m_count].m7 = tmp7;
     340            new (&this->m_data[this->m_count].m1) T1(tmp1);
     341            new (&this->m_data[this->m_count].m2) T2(tmp2);
     342            new (&this->m_data[this->m_count].m3) T3(tmp3);
     343            new (&this->m_data[this->m_count].m4) T4(tmp4);
     344            new (&this->m_data[this->m_count].m5) T5(tmp5);
     345            new (&this->m_data[this->m_count].m6) T6(tmp6);
     346            new (&this->m_data[this->m_count].m7) T7(tmp7);
    341347        }
    342348        else
     
    369375            T5 tmp5 = m5; T6 tmp6 = m6;
    370376            this->Reserve(this->m_count * 13 / 8 + 8);
    371             this->m_data[this->m_count].m1 = tmp1;
    372             this->m_data[this->m_count].m2 = tmp2;
    373             this->m_data[this->m_count].m3 = tmp3;
    374             this->m_data[this->m_count].m4 = tmp4;
    375             this->m_data[this->m_count].m5 = tmp5;
    376             this->m_data[this->m_count].m6 = tmp6;
     377            new (&this->m_data[this->m_count].m1) T1(tmp1);
     378            new (&this->m_data[this->m_count].m2) T2(tmp2);
     379            new (&this->m_data[this->m_count].m3) T3(tmp3);
     380            new (&this->m_data[this->m_count].m4) T4(tmp4);
     381            new (&this->m_data[this->m_count].m5) T5(tmp5);
     382            new (&this->m_data[this->m_count].m6) T6(tmp6);
    377383        }
    378384        else
     
    403409            T5 tmp5 = m5;
    404410            this->Reserve(this->m_count * 13 / 8 + 8);
    405             this->m_data[this->m_count].m1 = tmp1;
    406             this->m_data[this->m_count].m2 = tmp2;
    407             this->m_data[this->m_count].m3 = tmp3;
    408             this->m_data[this->m_count].m4 = tmp4;
    409             this->m_data[this->m_count].m5 = tmp5;
     411            new (&this->m_data[this->m_count].m1) T1(tmp1);
     412            new (&this->m_data[this->m_count].m2) T2(tmp2);
     413            new (&this->m_data[this->m_count].m3) T3(tmp3);
     414            new (&this->m_data[this->m_count].m4) T4(tmp4);
     415            new (&this->m_data[this->m_count].m5) T5(tmp5);
    410416        }
    411417        else
     
    433439            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3; T4 tmp4 = m4;
    434440            this->Reserve(this->m_count * 13 / 8 + 8);
    435             this->m_data[this->m_count].m1 = tmp1;
    436             this->m_data[this->m_count].m2 = tmp2;
    437             this->m_data[this->m_count].m3 = tmp3;
    438             this->m_data[this->m_count].m4 = tmp4;
     441            new (&this->m_data[this->m_count].m1) T1(tmp1);
     442            new (&this->m_data[this->m_count].m2) T2(tmp2);
     443            new (&this->m_data[this->m_count].m3) T3(tmp3);
     444            new (&this->m_data[this->m_count].m4) T4(tmp4);
    439445        }
    440446        else
     
    461467            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3;
    462468            this->Reserve(this->m_count * 13 / 8 + 8);
    463             this->m_data[this->m_count].m1 = tmp1;
    464             this->m_data[this->m_count].m2 = tmp2;
    465             this->m_data[this->m_count].m3 = tmp3;
     469            new (&this->m_data[this->m_count].m1) T1(tmp1);
     470            new (&this->m_data[this->m_count].m2) T2(tmp2);
     471            new (&this->m_data[this->m_count].m3) T3(tmp3);
    466472        }
    467473        else
     
    487493            T1 tmp1 = m1; T2 tmp2 = m2;
    488494            this->Reserve(this->m_count * 13 / 8 + 8);
    489             this->m_data[this->m_count].m1 = tmp1;
    490             this->m_data[this->m_count].m2 = tmp2;
     495            new (&this->m_data[this->m_count].m1) T1(tmp1);
     496            new (&this->m_data[this->m_count].m2) T2(tmp2);
    491497        }
    492498        else
Note: See TracChangeset for help on using the changeset viewer.