Skip to content

Commit

Permalink
Fix segmentation fault when .conf file is missing
Browse files Browse the repository at this point in the history
Whenever ghostwriter.conf is missing (or the last used exporter within
is empty), the app will crash with a segmentation fault due to accessing
an uninitialed pointer to the current HTML exporter in appsettings.cpp.
This commit ensures that the pointer is initialized to null before being
checked for whether obtaining an exporter was successful.
BUG: 465799

(cherry picked from commit f82070a)
Signed-off-by: Andreas Sturmlechner <[email protected]>
  • Loading branch information
Megan Conkle authored and a17r committed Nov 6, 2024
1 parent 9c04a52 commit bfca43e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [24.08.3]

### Fixed

* Issue #465799: Segmentation fault no longer occurs when .conf file is missing on first launch.

## [24.08.0]

### Added

* Added more icons to menu actions.
Expand Down
24 changes: 12 additions & 12 deletions src/preview/htmlpreview.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2014-2023 Megan Conkle <[email protected]>
/*
* SPDX-FileCopyrightText: 2014-2024 Megan Conkle <[email protected]>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
Expand Down Expand Up @@ -38,7 +38,7 @@ class HtmlPreviewPrivate
HtmlPreviewPrivate(HtmlPreview *q_ptr)
: q_ptr(q_ptr)
{
;
proxy = new PreviewProxy(q_ptr);
}

~HtmlPreviewPrivate()
Expand All @@ -51,7 +51,7 @@ class HtmlPreviewPrivate
MarkdownDocument *document;
bool updateInProgress;
bool updateAgain;
PreviewProxy proxy;
PreviewProxy *proxy;
QString baseUrl;
QRegularExpression headingTagExp;
Exporter *exporter;
Expand Down Expand Up @@ -91,7 +91,7 @@ HtmlPreview::HtmlPreview
d->updateInProgress = false;
d->updateAgain = false;
d->exporter = exporter;
d->proxy.setMathEnabled(d->exporter->supportsMath());
d->proxy->setMathEnabled(d->exporter->supportsMath());

d->baseUrl = "";

Expand Down Expand Up @@ -153,7 +153,7 @@ HtmlPreview::HtmlPreview
this->setZoomFactor((horizontalDpi / 96.0));

QWebChannel *channel = new QWebChannel(this);
channel->registerObject(QStringLiteral("previewProxy"), &d->proxy);
channel->registerObject(QStringLiteral("previewProxy"), d->proxy);
this->page()->setWebChannel(channel);

QFile wrapperHtmlFile(":/resources/preview.html");
Expand Down Expand Up @@ -240,22 +240,22 @@ void HtmlPreview::setHtmlExporter(Exporter *exporter)

d->exporter = exporter;
d->setHtmlContent("");
d->proxy.setMathEnabled(d->exporter->supportsMath());
d->proxy->setMathEnabled(d->exporter->supportsMath());
updatePreview();
}

void HtmlPreview::setStyleSheet(const QString &css)
{
Q_D(HtmlPreview);
d->proxy.setStyleSheet(css);

d->proxy->setStyleSheet(css);
}

void HtmlPreview::setMathEnabled(bool enabled)
{
Q_D(HtmlPreview);
d->proxy.setMathEnabled(enabled);

d->proxy->setMathEnabled(enabled);
}

void HtmlPreviewPrivate::onHtmlReady()
Expand Down Expand Up @@ -312,7 +312,7 @@ void HtmlPreview::closeEvent(QCloseEvent *event)

void HtmlPreviewPrivate::setHtmlContent(const QString &html)
{
this->proxy.setHtmlContent(html);
this->proxy->setHtmlContent(html);
}

QString HtmlPreviewPrivate::exportToHtml
Expand Down
18 changes: 12 additions & 6 deletions src/settings/appsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AppSettingsPrivate
static AppSettings *instance;

AppSettingsPrivate()
: currentHtmlExporter(nullptr)
{
;
}
Expand Down Expand Up @@ -677,6 +678,7 @@ void AppSettings::setShowUnbreakableSpaceEnabled(bool enabled)
d->showUnbreakableSpaceEnabled = enabled;
emit showUnbreakableSpaceEnabledChanged(d->showUnbreakableSpaceEnabled);
}

AppSettings::AppSettings()
: d_ptr(new AppSettingsPrivate())
{
Expand Down Expand Up @@ -843,17 +845,21 @@ AppSettings::AppSettings()

QString exporterName = appSettings.value(constants::GW_LAST_USED_EXPORTER_KEY).toString();

d->currentHtmlExporter = nullptr;

if (!exporterName.isEmpty()) {
d->currentHtmlExporter = ExporterFactory::instance()->exporterByName(exporterName);
}

if (d->currentHtmlExporter) {
auto lastExportOptions = appSettings.value(constants::GW_LAST_USED_EXPORTER_PARAMS_KEY).toString();
if (d->currentHtmlExporter) {
auto lastExportOptions = appSettings.value(constants::GW_LAST_USED_EXPORTER_PARAMS_KEY).toString();

if (!lastExportOptions.isEmpty()) {
d->currentHtmlExporter->setOptions(lastExportOptions);
if (!lastExportOptions.isEmpty()) {
d->currentHtmlExporter->setOptions(lastExportOptions);
}
}
} else {
}

if (!d->currentHtmlExporter) {
d->currentHtmlExporter = ExporterFactory::instance()->htmlExporters().first();
}
}
Expand Down

0 comments on commit bfca43e

Please sign in to comment.