Skip to content

Commit c451a82

Browse files
Merge pull request scp-fs2open#6966 from MjnMixael/fix_qtfred_variables_strings
QtFRED Fix variables heap corruption crash
2 parents 56a3ddd + eb36b56 commit c451a82

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

qtfred/src/ui/dialogs/VariableDialog.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ void VariableDialog::onVariablesTableUpdated()
280280
}
281281

282282
auto item = ui->variablesTable->item(currentRow, 0);
283-
SCP_string itemText = item->text().toStdString();
283+
SCP_string itemText = item->text().toUtf8().constData();
284284
bool apply = false;
285285

286286
// This will only be true if the user is trying to add a new variable.
@@ -327,7 +327,7 @@ void VariableDialog::onVariablesTableUpdated()
327327
auto ret = _model->changeVariableName(item->row(), itemText);
328328

329329
// we put something in the cell, but the model couldn't process it.
330-
if (strlen(item->text().toStdString().c_str()) && ret.empty()){
330+
if (strlen(item->text().toUtf8().constData()) && ret.empty()) {
331331
// update of variable name failed, resync UI
332332
apply = true;
333333

@@ -341,7 +341,7 @@ void VariableDialog::onVariablesTableUpdated()
341341

342342
// now work on the variable data cell
343343
item = ui->variablesTable->item(currentRow, 1);
344-
itemText = item->text().toStdString();
344+
itemText = item->text().toUtf8().constData();
345345

346346
// check if data column was altered
347347
if (itemText != _currentVariableData) {
@@ -360,7 +360,7 @@ void VariableDialog::onVariablesTableUpdated()
360360

361361
// Variable is a number
362362
} else {
363-
SCP_string source = item->text().toStdString();
363+
SCP_string source = item->text().toUtf8().constData();
364364
SCP_string temp = _model->trimIntegerString(source);
365365

366366
try {
@@ -404,13 +404,13 @@ void VariableDialog::onVariablesSelectionChanged()
404404
auto item = ui->variablesTable->item(row, 0);
405405

406406
if (item){
407-
newVariableName = item->text().toStdString();
407+
newVariableName = item->text().toUtf8().constData();
408408
}
409409

410410
item = ui->variablesTable->item(row, 1);
411411

412412
if (item){
413-
_currentVariableData = item->text().toStdString();
413+
_currentVariableData = item->text().toUtf8().constData();
414414
}
415415

416416
if (newVariableName != _currentVariable){
@@ -439,7 +439,7 @@ void VariableDialog::onContainersTableUpdated()
439439
// Are they adding a new container?
440440
if (row == ui->containersTable->rowCount() - 1){
441441
if (ui->containersTable->item(row, 0)) {
442-
SCP_string newString = ui->containersTable->item(row, 0)->text().toStdString();
442+
SCP_string newString = ui->containersTable->item(row, 0)->text().toUtf8().constData();
443443
if (!newString.empty() && newString != "Add Container ..."){
444444
_model->addContainer(newString);
445445
_currentContainer = newString;
@@ -454,7 +454,7 @@ void VariableDialog::onContainersTableUpdated()
454454

455455
// are they editing an existing container name?
456456
} else if (ui->containersTable->item(row, 0)){
457-
SCP_string newName = ui->containersTable->item(row,0)->text().toStdString();
457+
SCP_string newName = ui->containersTable->item(row,0)->text().toUtf8().constData();
458458

459459
// Restoring a deleted container?
460460
if (_currentContainer.empty()){
@@ -484,7 +484,7 @@ void VariableDialog::onContainersSelectionChanged()
484484
}
485485

486486
// guaranteed not to be null, since getCurrentContainerRow already checked.
487-
_currentContainer = ui->containersTable->item(row, 0)->text().toStdString();
487+
_currentContainer = ui->containersTable->item(row, 0)->text().toUtf8().constData();
488488
applyModel();
489489
}
490490

@@ -512,7 +512,7 @@ void VariableDialog::onContainerContentsTableUpdated()
512512
SCP_string newString;
513513

514514
if (ui->containerContentsTable->item(row, 0)) {
515-
newString = ui->containerContentsTable->item(row, 0)->text().toStdString();
515+
newString = ui->containerContentsTable->item(row, 0)->text().toUtf8().constData();
516516

517517
if (!newString.empty() && newString != "Add item ..."){
518518

@@ -532,7 +532,7 @@ void VariableDialog::onContainerContentsTableUpdated()
532532
}
533533

534534
if (ui->containerContentsTable->item(row, 1)) {
535-
newString = ui->containerContentsTable->item(row, 1)->text().toStdString();
535+
newString = ui->containerContentsTable->item(row, 1)->text().toUtf8().constData();
536536

537537
if (!newString.empty() && newString != "Add item ..."){
538538

@@ -554,7 +554,7 @@ void VariableDialog::onContainerContentsTableUpdated()
554554

555555
// are they editing an existing container item column 1?
556556
} else if (ui->containerContentsTable->item(row, 0)){
557-
SCP_string newText = ui->containerContentsTable->item(row, 0)->text().toStdString();
557+
SCP_string newText = ui->containerContentsTable->item(row, 0)->text().toUtf8().constData();
558558

559559
if (_model->getContainerListOrMap(containerRow)){
560560

@@ -581,7 +581,7 @@ void VariableDialog::onContainerContentsTableUpdated()
581581
// if we're here, nothing has changed so far. So let's attempt column 2
582582
if (ui->containerContentsTable->item(row, 1) && !_model->getContainerListOrMap(containerRow)){
583583

584-
SCP_string newText = ui->containerContentsTable->item(row, 1)->text().toStdString();
584+
SCP_string newText = ui->containerContentsTable->item(row, 1)->text().toUtf8().constData();
585585

586586
if(newText != _currentContainerItemCol2){
587587

@@ -624,9 +624,9 @@ void VariableDialog::onContainerContentsSelectionChanged()
624624
return;
625625
}
626626

627-
newContainerItemName = item->text().toStdString();
627+
newContainerItemName = item->text().toUtf8().constData();
628628
item = ui->containerContentsTable->item(row, 1);
629-
SCP_string newContainerDataText = (item) ? item->text().toStdString() : "";
629+
SCP_string newContainerDataText = (item) ? item->text().toUtf8().constData() : "";
630630

631631
if (newContainerItemName != _currentContainerItemCol1 || _currentContainerItemCol2 != newContainerDataText){
632632
_currentContainerItemCol1 = newContainerItemName;
@@ -676,7 +676,8 @@ void VariableDialog::onDeleteVariableButtonPressed()
676676
}
677677

678678
// Because of the text update we'll need, this needs an applyModel, whether it fails or not.
679-
if (ui->deleteVariableButton->text().toStdString() == "Restore") {
679+
SCP_string btn_text = ui->deleteVariableButton->text().toUtf8().constData();
680+
if (btn_text == "Restore") {
680681
_model->removeVariable(currentRow, false);
681682
applyModel();
682683
} else {
@@ -853,7 +854,8 @@ void VariableDialog::onDeleteContainerButtonPressed()
853854
}
854855

855856
// Because of the text update we'll need, this needs an applyModel, whether it fails or not.
856-
if (ui->deleteContainerButton->text().toStdString() == "Restore"){
857+
SCP_string btn_text = ui->deleteContainerButton->text().toUtf8().constData();
858+
if (btn_text == "Restore"){
857859
_model->removeContainer(row, false);
858860
} else {
859861
_model->removeContainer(row, true);
@@ -1248,12 +1250,13 @@ void VariableDialog::applyModel()
12481250
}
12491251

12501252
if (_currentVariable.empty() || selectedRow < 0){
1251-
if (ui->variablesTable->item(0, 0) && !ui->variablesTable->item(0, 0)->text().toStdString().empty()){
1252-
_currentVariable = ui->variablesTable->item(0, 0)->text().toStdString();
1253+
SCP_string text = ui->variablesTable->item(0, 0)->text().toUtf8().constData();
1254+
if (ui->variablesTable->item(0, 0) && !text.empty()) {
1255+
_currentVariable = text;
12531256
}
12541257

12551258
if (ui->variablesTable->item(0, 1)) {
1256-
_currentVariableData = ui->variablesTable->item(0, 1)->text().toStdString();
1259+
_currentVariableData = ui->variablesTable->item(0, 1)->text().toUtf8().constData();
12571260
}
12581261
}
12591262

@@ -1294,11 +1297,13 @@ void VariableDialog::applyModel()
12941297
}
12951298

12961299
// do we need to switch the delete button to a restore button?
1297-
if (selectedRow > -1 && ui->containersTable->item(selectedRow, 2) && ui->containersTable->item(selectedRow, 2)->text().toStdString() == "To Be Deleted") {
1300+
SCP_string var = selectedRow > -1 ? ui->containersTable->item(selectedRow, 2)->text().toUtf8().constData() : "";
1301+
if (selectedRow > -1 && ui->containersTable->item(selectedRow, 2) && var == "To Be Deleted") {
12981302
ui->deleteContainerButton->setText("Restore");
12991303

13001304
// We can't restore empty container names.
1301-
if (ui->containersTable->item(selectedRow, 0) && ui->containersTable->item(selectedRow, 0)->text().toStdString().empty()){
1305+
SCP_string text = ui->containersTable->item(selectedRow, 0)->text().toUtf8().constData();
1306+
if (ui->containersTable->item(selectedRow, 0) && text.empty()){
13021307
ui->deleteContainerButton->setEnabled(false);
13031308
} else {
13041309
ui->deleteContainerButton->setEnabled(true);
@@ -1338,7 +1343,7 @@ void VariableDialog::applyModel()
13381343

13391344
if (selectedRow < 0 && ui->containersTable->rowCount() > 1) {
13401345
if (ui->containersTable->item(0, 0)){
1341-
_currentContainer = ui->containersTable->item(0, 0)->text().toStdString();
1346+
_currentContainer = ui->containersTable->item(0, 0)->text().toUtf8().constData();
13421347
ui->containersTable->clearSelection();
13431348
ui->containersTable->item(0, 0)->setSelected(true);
13441349
}
@@ -1396,11 +1401,13 @@ void VariableDialog::updateVariableOptions(bool safeToAlter)
13961401
ui->setVariableAsNumberRadio->setChecked(!string);
13971402

13981403
// do we need to switch the delete button to a restore button?
1399-
if (ui->variablesTable->item(row, 2) && ui->variablesTable->item(row, 2)->text().toStdString() == "To Be Deleted"){
1404+
SCP_string var = ui->variablesTable->item(row, 2) ? ui->variablesTable->item(row, 2)->text().toUtf8().constData() : "";
1405+
if (ui->variablesTable->item(row, 2) && var == "To Be Deleted"){
14001406
ui->deleteVariableButton->setText("Restore");
14011407

14021408
// We can't restore empty variable names.
1403-
if (ui->variablesTable->item(row, 0) && ui->variablesTable->item(row, 0)->text().toStdString().empty()){
1409+
SCP_string text = ui->variablesTable->item(row, 0)->text().toUtf8().constData();
1410+
if (ui->variablesTable->item(row, 0) && text.empty()){
14041411
ui->deleteVariableButton->setEnabled(false);
14051412
} else {
14061413
ui->deleteVariableButton->setEnabled(true);
@@ -1823,7 +1830,8 @@ int VariableDialog::getCurrentVariableRow()
18231830

18241831
// yes, selected items returns a list, but we really should only have one item because multiselect will be off.
18251832
for (const auto& item : items) {
1826-
if (item && item->column() == 0 && item->text().toStdString() != "Add Variable ...") {
1833+
SCP_string var = item->text().toUtf8().constData();
1834+
if (item && item->column() == 0 && var != "Add Variable ...") {
18271835
return item->row();
18281836
}
18291837
}
@@ -1837,7 +1845,8 @@ int VariableDialog::getCurrentContainerRow()
18371845

18381846
// yes, selected items returns a list, but we really should only have one item because multiselect will be off.
18391847
for (const auto& item : items) {
1840-
if (item && item->column() == 0 && item->text().toStdString() != "Add Container ...") {
1848+
SCP_string var = item->text().toUtf8().constData();
1849+
if (item && item->column() == 0 && var != "Add Container ...") {
18411850
return item->row();
18421851
}
18431852
}
@@ -1851,7 +1860,8 @@ int VariableDialog::getCurrentContainerItemRow()
18511860

18521861
// yes, selected items returns a list, but we really should only have one item because multiselect will be off.
18531862
for (const auto& item : items) {
1854-
if (item && ((item->column() == 0 && item->text().toStdString() != "Add item ...") || (item->column() == 1 && item->text().toStdString() != "Add item ..."))) {
1863+
SCP_string var = item->text().toUtf8().constData();
1864+
if (item && ((item->column() == 0 && var != "Add item ...") || (item->column() == 1 && var != "Add item ..."))) {
18551865
return item->row();
18561866
}
18571867
}

0 commit comments

Comments
 (0)