Skip to content

Commit 342958b

Browse files
committed
fix qtfred memory corruption
Fix a qtFRED memory access bug when checking for autosaved files. Passing a temporary variable to a function as a reference, then returning it, led to a dangling reference access. Rewrite the `maybeUseAutosave` function to avoid this.
1 parent dbe0af1 commit 342958b

3 files changed

Lines changed: 7 additions & 8 deletions

File tree

qtfred/src/mission/Editor.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,18 @@ void Editor::update() {
130130
}
131131
}
132132

133-
std::string Editor::maybeUseAutosave(const std::string& filepath)
133+
void Editor::maybeUseAutosave(std::string& filepath)
134134
{
135135
// first, just grab the info of this mission
136136
if (!parse_main(filepath.c_str(), MPF_ONLY_MISSION_INFO))
137-
return filepath;
137+
return;
138138
SCP_string created = The_mission.created;
139139
CFileLocation res = cf_find_file_location(filepath.c_str(), CF_TYPE_ANY);
140140
time_t modified = res.m_time;
141141
if (!res.found)
142142
{
143143
UNREACHABLE("Couldn't find path '%s' even though parse_main() succeeded!", filepath.c_str());
144-
return filepath; // just load the actual specified file
144+
return;
145145
}
146146

147147
// now check all the autosaves
@@ -179,10 +179,8 @@ std::string Editor::maybeUseAutosave(const std::string& filepath)
179179
prompt.c_str(),
180180
{ DialogButton::Yes, DialogButton::No });
181181
if (z == DialogButton::Yes)
182-
return backup_res.full_name.c_str();
182+
filepath = backup_res.full_name; // replace the specified file with the autosave file
183183
}
184-
185-
return filepath;
186184
}
187185

188186
bool Editor::loadMission(const std::string& mission_name, int flags) {

qtfred/src/mission/Editor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Editor : public QObject {
3939

4040
void createNewMission();
4141

42-
std::string maybeUseAutosave(const std::string& filepath);
42+
void maybeUseAutosave(std::string& filepath);
4343

4444
/*! Load a mission. */
4545
bool loadMission(const std::string& filepath, int flags = 0);

qtfred/src/ui/FredView.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void FredView::loadMissionFile(const QString& pathName) {
143143
try {
144144
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
145145

146-
auto pathToLoad = fred->maybeUseAutosave(pathName.toStdString());
146+
auto pathToLoad = pathName.toStdString();
147+
fred->maybeUseAutosave(pathToLoad);
147148

148149
fred->loadMission(pathToLoad);
149150

0 commit comments

Comments
 (0)