Skip to content

Commit 8a4cf6b

Browse files
MinyazevRiakov
andauthored
Fix crash when saving generated files for NXT (trikset#1886)
When trying to save the source file generated for NXT, TRIKStudio crashes with use heap after free. This solution is a workaround, as one needs to pass ownership of NxtOsekCMasterGenerator to the NxtOsekCGeneratorPlugin object, but this requires to rewrite RobotsGeneratorPluginBase (that leads to changes in behaviour in others kits) * Fix crash bug when saving source files for NXT * Prefer qobject_cast instead of dynamic_cast whenever possible --------- Co-authored-by: iakov <iakov@users.noreply.github.com>
1 parent 6f9404f commit 8a4cf6b

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

plugins/robots/generators/nxt/nxtOsekCGenerator/nxtOsekCGeneratorPlugin.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ NxtOsekCGeneratorPlugin::NxtOsekCGeneratorPlugin()
3434
, mFlashRobotAction(new QAction(this))
3535
, mUploadProgramAction(new QAction(this))
3636
, mNxtToolsPresent(false)
37-
, mMasterGenerator(nullptr)
3837
, mCommunicator(utils::Singleton<communication::UsbRobotCommunicationThread>::instance())
3938
{
4039
initActions();
@@ -179,21 +178,24 @@ void NxtOsekCGeneratorPlugin::onUploadingComplete(bool success)
179178

180179
generatorBase::MasterGeneratorBase *NxtOsekCGeneratorPlugin::masterGenerator()
181180
{
182-
mMasterGenerator = new NxtOsekCMasterGenerator(*mRepo
181+
return new NxtOsekCMasterGenerator(*mRepo
183182
, *mMainWindowInterface->errorReporter()
184183
, *mParserErrorReporter
185184
, *mRobotModelManager
186185
, *mTextLanguage
187186
, mMainWindowInterface->activeDiagram()
188187
, generatorName());
189-
return mMasterGenerator;
190188
}
191189

192190
void NxtOsekCGeneratorPlugin::regenerateExtraFiles(const QFileInfo &newFileInfo)
193191
{
194-
mMasterGenerator->initialize();
195-
mMasterGenerator->setProjectDir(newFileInfo);
196-
mMasterGenerator->generateOilAndMakeFiles();
192+
// Static cast is possible and correct, but dynamic will be more flexible.
193+
if (auto nxtGenerator = qobject_cast<NxtOsekCMasterGenerator*>(masterGenerator())) {
194+
QScopedPointer<NxtOsekCMasterGenerator> generator(nxtGenerator);
195+
generator->initialize();
196+
generator->setProjectDir(newFileInfo);
197+
generator->generateOilAndMakeFiles();
198+
}
197199
}
198200

199201
void NxtOsekCGeneratorPlugin::flashRobot()

plugins/robots/generators/nxt/nxtOsekCGenerator/nxtOsekCGeneratorPlugin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ private slots:
8989
bool mNxtToolsPresent { false };
9090
/// Flasher object
9191
QScopedPointer<NxtFlashTool> mFlashTool;
92-
93-
NxtOsekCMasterGenerator *mMasterGenerator;
9492
const QSharedPointer<communication::UsbRobotCommunicationThread> mCommunicator;
9593
};
9694

qrtranslations/fr/plugins/robots/nxtOsekCGenerator_fr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@
165165
<translation>Génération (NXT OSEK C)</translation>
166166
</message>
167167
<message>
168-
<location line="+52"/>
169-
<location line="+129"/>
168+
<location line="+51"/>
169+
<location line="+132"/>
170170
<source>NXT tools package is not installed</source>
171171
<translation type="unfinished"></translation>
172172
</message>
173173
<message>
174-
<location line="-82"/>
174+
<location line="-85"/>
175175
<source>Generate code</source>
176176
<translation>Générer le code</translation>
177177
</message>
@@ -196,7 +196,7 @@
196196
<translation>Téléverser le programme sur le dispositif NXT</translation>
197197
</message>
198198
<message>
199-
<location line="+50"/>
199+
<location line="+53"/>
200200
<source>flash.sh not found. Make sure it is present in QReal installation directory</source>
201201
<translation>flash.sh n&apos;est pas trouvé. Assurez-vous qu&apos;il soit présent dans le repertoire de QReal</translation>
202202
</message>

qrtranslations/ru/plugins/robots/nxtOsekCGenerator_ru.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@
169169
<translation>Генерация (C)</translation>
170170
</message>
171171
<message>
172-
<location line="+52"/>
173-
<location line="+129"/>
172+
<location line="+51"/>
173+
<location line="+132"/>
174174
<source>NXT tools package is not installed</source>
175175
<translation>Пакет &quot;Инструменты NXT&quot; не установлен</translation>
176176
</message>
177177
<message>
178-
<location line="-82"/>
178+
<location line="-85"/>
179179
<source>Generate code</source>
180180
<translation>Генерировать код</translation>
181181
</message>
@@ -200,7 +200,7 @@
200200
<translation>Загрузить программу на устройство NXT</translation>
201201
</message>
202202
<message>
203-
<location line="+50"/>
203+
<location line="+53"/>
204204
<source>flash.sh not found. Make sure it is present in QReal installation directory</source>
205205
<translation>Не найден скрипт flash.sh. Убедитесь, что пакет nxt-tools установлен корректно</translation>
206206
</message>

0 commit comments

Comments
 (0)