TheAssemblyArmada / Thyme

An open source re-implementation of Generals : Zero Hour written in C++.
GNU General Public License v2.0
584 stars 59 forks source link

Obsolete memory allocation in Move_Rows_Down, or bug? #1093

Open xezon opened 9 months ago

xezon commented 9 months ago
int Move_Rows_Down(_ListboxData *data, int row)
{
    int size = sizeof(_ListEntryRow) * (data->m_endPos - row);
    char *buf = new char[size];
    memcpy(buf, &data->m_listData[row], size);
    memcpy(&data->m_listData[row + 1], buf, size);
    delete[] buf;
...
}

It allocates a buf, copies data->m_listData[row] into buf, and copies buf into data->m_listData[row + 1].

This makes no sense. It can copy data->m_listData[row] directly into data->m_listData[row + 1]:

memcpy(&data->m_listData[row + 1], &data->m_listData[row], size);

Alternatively, perhaps this meant to swap the rows? In that case one would need to add as 2nd copy step:

memcpy(&data->m_listData[row], &data->m_listData[row + 1], size);