Changeset 1252


Ignore:
Timestamp:
Apr 21, 2012, 5:01:10 PM (11 years ago)
Author:
sam
Message:

core: fix a bug in the Array class where we would corrupt the data when
trying to realloc the array and insert a reference to a member of our own.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/array.h

    r1244 r1252  
    4343    inline Array<T1, T2, T3> const& operator+=(Element const &x)
    4444    {
    45         Append(x.m1, x.m2, x.m3);
     45        if (m_count >= m_reserved)
     46        {
     47            /* Protect ourselves against insertion of an element that is
     48             * already in m_data. */
     49            Element tmp = x;
     50            Reserve(m_count * 13 / 8 + 8);
     51            m_data[m_count++] = tmp;
     52        }
     53        else
     54            m_data[m_count++] = x;
    4655        return *this;
    4756    }
     
    5059    {
    5160        if (m_count >= m_reserved)
    52             Reserve(m_count * 13 / 8 + 8);
    53         m_data[m_count].m1 = m1;
    54         m_data[m_count].m2 = m2;
    55         m_data[m_count].m3 = m3;
     61        {
     62            T1 tmp1 = m1; T2 tmp2 = m2; T3 tmp3 = m3;
     63            Reserve(m_count * 13 / 8 + 8);
     64            m_data[m_count].m1 = tmp1;
     65            m_data[m_count].m2 = tmp2;
     66            m_data[m_count].m3 = tmp3;
     67        }
     68        else
     69        {
     70            m_data[m_count].m1 = m1;
     71            m_data[m_count].m2 = m2;
     72            m_data[m_count].m3 = m3;
     73        }
    5674        ++m_count;
    5775    }
     
    112130    inline Array<T1, T2> const& operator+=(Element const &x)
    113131    {
    114         Append(x.m1, x.m2);
     132        if (m_count >= m_reserved)
     133        {
     134            /* Protect ourselves against insertion of an element that is
     135             * already in m_data. */
     136            Element tmp = x;
     137            Reserve(m_count * 13 / 8 + 8);
     138            m_data[m_count++] = tmp;
     139        }
     140        else
     141            m_data[m_count++] = x;
    115142        return *this;
    116143    }
     
    119146    {
    120147        if (m_count >= m_reserved)
    121             Reserve(m_count * 13 / 8 + 8);
    122         m_data[m_count].m1 = m1;
    123         m_data[m_count].m2 = m2;
     148        {
     149            T1 tmp1 = m1; T2 tmp2 = m2;
     150            Reserve(m_count * 13 / 8 + 8);
     151            m_data[m_count].m1 = tmp1;
     152            m_data[m_count].m2 = tmp2;
     153        }
     154        else
     155        {
     156            m_data[m_count].m1 = m1;
     157            m_data[m_count].m2 = m2;
     158        }
    124159        ++m_count;
    125160    }
     
    179214    {
    180215        if (m_count >= m_reserved)
    181             Reserve(m_count * 13 / 8 + 8);
    182         m_data[m_count++] = x;
     216        {
     217            T1 tmp = x;
     218            Reserve(m_count * 13 / 8 + 8);
     219            m_data[m_count++] = tmp;
     220        }
     221        else
     222        {
     223            m_data[m_count++] = x;
     224        }
    183225        return *this;
    184226    }
  • trunk/win32/lolcore.vcxproj

    r1250 r1252  
    126126  <ItemGroup>
    127127    <ClInclude Include="..\src\application\application.h" />
     128    <ClInclude Include="..\src\array.h" />
    128129    <ClInclude Include="..\src\audio.h" />
    129130    <ClInclude Include="..\src\bitfield.h" />
  • trunk/win32/lolcore.vcxproj.filters

    r1250 r1252  
    362362      <Filter>src\gpu</Filter>
    363363    </ClInclude>
     364    <ClInclude Include="..\src\array.h">
     365      <Filter>src</Filter>
     366    </ClInclude>
    364367  </ItemGroup>
    365368</Project>
Note: See TracChangeset for help on using the changeset viewer.