Skip to content

Commit

Permalink
added checks to mitigate floating point crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
fourtf committed Apr 19, 2020
1 parent 6b512d1 commit 6052846
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 40 deletions.
17 changes: 15 additions & 2 deletions src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ namespace detail {
this->items_.begin(), this->items_.end(), 0UL,
[](auto init, auto &&frame) { return init + frame.duration; });

this->durationOffset_ = std::min<int>(
int(getApp()->emotes->gifTimer.position() % totalLength), 60000);
if (totalLength == 0)
{
this->durationOffset_ = 0;
}
else
{
this->durationOffset_ = std::min<int>(
int(getApp()->emotes->gifTimer.position() % totalLength),
60000);
}
this->processOffset();
}

Expand All @@ -72,6 +80,11 @@ namespace detail {

void Frames::processOffset()
{
if (this->items_.isEmpty())
{
return;
}

while (true)
{
this->index_ %= this->items_.size();
Expand Down
5 changes: 4 additions & 1 deletion src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ static float relativeSimilarity(const QString &str1, const QString &str2)
}
}

return z == 0 ? 0.f : float(z) / std::max(str1.size(), str2.size());
// ensure that no div by 0
return z == 0 ? 0.f
: float(z) /
std::max<int>(1, std::max(str1.size(), str2.size()));
};

float IrcMessageHandler::similarity(
Expand Down
1 change: 1 addition & 0 deletions src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ QColor getRandomColor(const QVariant &userId)
colorSeed = std::rand();
}

assert(twitchUsernameColors.size() != 0);
const auto colorIndex = colorSeed % twitchUsernameColors.size();
return twitchUsernameColors[colorIndex];
}
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/BaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <QIcon>
#include <QLayout>
#include <QtGlobal>
#include <algorithm>

namespace chatterino {

Expand Down Expand Up @@ -113,7 +114,8 @@ float BaseWidget::qtFontScale() const
{
if (auto window = dynamic_cast<BaseWindow *>(this->window()))
{
return this->scale() / window->nativeScale_;
// ensure no div by 0
return this->scale() / std::max<float>(0.01f, window->nativeScale_);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ QRect BaseWindow::getBounds()

float BaseWindow::scale() const
{
return this->overrideScale().value_or(this->scale_);
return std::max<float>(0.01f, this->overrideScale().value_or(this->scale_));
}

float BaseWindow::qtFontScale() const
{
return this->scale() / this->nativeScale_;
return this->scale() / std::max<float>(0.01, this->nativeScale_);
}

void BaseWindow::init()
Expand Down
6 changes: 4 additions & 2 deletions src/widgets/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ void Label::paintEvent(QPaintEvent *)

QFontMetrics metrics = getFonts()->getFontMetrics(
this->getFontStyle(),
this->scale() * 96.f / this->logicalDpiX() * deviceDpi);
this->scale() * 96.f /
std::max<float>(0.01, this->logicalDpiX() * deviceDpi));
painter.setFont(getFonts()->getFont(
this->getFontStyle(),
this->scale() * 96.f / this->logicalDpiX() * deviceDpi));
this->scale() * 96.f /
std::max<float>(0.02, this->logicalDpiX() * deviceDpi)));

int offset = this->getOffset();

Expand Down
19 changes: 10 additions & 9 deletions src/widgets/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ void Scrollbar::paintEvent(QPaintEvent *)

int w = this->width();
float y = 0;
float dY = float(this->height()) / float(snapshotLength);
float dY =
float(this->height()) / std::max<float>(1.0f, float(snapshotLength));
int highlightHeight =
int(std::ceil(std::max<float>(this->scale() * 2, dY)));

Expand Down Expand Up @@ -356,8 +357,10 @@ void Scrollbar::mouseMoveEvent(QMouseEvent *event)
{
int delta = event->pos().y() - this->lastMousePosition_.y();

setDesiredValue(this->desiredValue_ +
qreal(delta) / this->trackHeight_ * this->maximum_);
setDesiredValue(
this->desiredValue_ +
qreal(delta) /
std::max<qreal>(0.02, this->trackHeight_ * this->maximum_));
}

this->lastMousePosition_ = event->pos();
Expand Down Expand Up @@ -442,13 +445,11 @@ void Scrollbar::updateScroll()
this->trackHeight_ = this->height() - this->buttonHeight_ -
this->buttonHeight_ - MIN_THUMB_HEIGHT - 1;

auto div = std::max<qreal>(0.01, this->maximum_ * this->trackHeight_);

this->thumbRect_ =
QRect(0,
int(this->currentValue_ / this->maximum_ * this->trackHeight_) +
1 + this->buttonHeight_,
this->width(),
int(this->largeChange_ / this->maximum_ * this->trackHeight_) +
MIN_THUMB_HEIGHT);
QRect(0, int(this->currentValue_ / div) + 1 + this->buttonHeight_,
this->width(), int(this->largeChange_ / div) + MIN_THUMB_HEIGHT);

this->update();
}
Expand Down
1 change: 1 addition & 0 deletions src/widgets/dialogs/ColorPickerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ void ColorPickerDialog::initRecentColors(LayoutCreator<QWidget> &creator)
this->ui_.recent.colors.push_back(new ColorButton(*it, this));
auto *button = this->ui_.recent.colors[ind];

static_assert(RECENT_COLORS_PER_ROW != 0);
const int rowInd = (ind / RECENT_COLORS_PER_ROW) + 1;
const int columnInd = ind % RECENT_COLORS_PER_ROW;

Expand Down
17 changes: 10 additions & 7 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ void ChannelView::scaleChangedEvent(float scale)
{
auto factor = this->qtFontScale();
#ifdef Q_OS_MACOS
factor = scale * 80.f / this->logicalDpiX() * this->devicePixelRatioF();
factor = scale * 80.f /
std::max<float>(
0.01, this->logicalDpiX() * this->devicePixelRatioF());
#endif
this->goToBottom_->getLabel().setFont(
getFonts()->getFont(FontStyle::UiMedium, factor));
Expand Down Expand Up @@ -426,8 +428,9 @@ void ChannelView::updateScrollbar(

if (h < 0) // break condition
{
this->scrollBar_->setLargeChange((messages.size() - i) +
qreal(h) / message->getHeight());
this->scrollBar_->setLargeChange(
(messages.size() - i) +
qreal(h) / std::max<int>(1, message->getHeight()));

showScrollbar = true;
break;
Expand Down Expand Up @@ -964,8 +967,8 @@ void ChannelView::wheelEvent(QWheelEvent *event)
if (delta > 0)
{
qreal scrollFactor = fmod(desired, 1);
qreal currentScrollLeft =
int(scrollFactor * snapshot[i]->getHeight());
qreal currentScrollLeft = std::max<qreal>(
0.01, int(scrollFactor * snapshot[i]->getHeight()));

for (; i >= 0; i--)
{
Expand Down Expand Up @@ -997,8 +1000,8 @@ void ChannelView::wheelEvent(QWheelEvent *event)
{
delta = -delta;
qreal scrollFactor = 1 - fmod(desired, 1);
qreal currentScrollLeft =
int(scrollFactor * snapshot[i]->getHeight());
qreal currentScrollLeft = std::max<qreal>(
0.01, int(scrollFactor * snapshot[i]->getHeight()));

for (; i < snapshotLength; i++)
{
Expand Down
11 changes: 5 additions & 6 deletions src/widgets/helper/NotebookTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,11 @@ void NotebookTab::paintEvent(QPaintEvent *)
QPainter painter(this);
float scale = this->scale();

painter.setFont(getApp()->fonts->getFont(
FontStyle::UiTabs,
scale * 96.f / this->logicalDpiX() * deviceDpi(this)));
QFontMetrics metrics = app->fonts->getFontMetrics(
FontStyle::UiTabs,
scale * 96.f / this->logicalDpiX() * deviceDpi(this));
auto div = std::max<float>(0.01f, this->logicalDpiX() * deviceDpi(this));
painter.setFont(
getApp()->fonts->getFont(FontStyle::UiTabs, scale * 96.f / div));
QFontMetrics metrics =
app->fonts->getFontMetrics(FontStyle::UiTabs, scale * 96.f / div);

int height = int(scale * NOTEBOOK_TAB_HEIGHT);

Expand Down
7 changes: 5 additions & 2 deletions src/widgets/settingspages/AboutPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ AboutPage::AboutPage()

auto logo = layout.emplace<QLabel>().assign(&this->logo_);
logo->setPixmap(pixmap);
logo->setFixedSize(PIXMAP_WIDTH,
PIXMAP_WIDTH * pixmap.height() / pixmap.width());
if (pixmap.width() != 0)
{
logo->setFixedSize(PIXMAP_WIDTH,
PIXMAP_WIDTH * pixmap.height() / pixmap.width());
}
logo->setScaledContents(true);

// this does nothing
Expand Down
22 changes: 14 additions & 8 deletions src/widgets/splits/SplitContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,10 @@ void SplitContainer::Node::insertNextToThis(Split *_split, Direction _direction)
{
auto &siblings = this->parent_->children_;

qreal width = this->parent_->geometry_.width() / siblings.size();
qreal height = this->parent_->geometry_.height() / siblings.size();
qreal width = this->parent_->geometry_.width() /
std::max<qreal>(0.0001, siblings.size());
qreal height = this->parent_->geometry_.height() /
std::max<qreal>(0.0001, siblings.size());

if (siblings.size() == 1)
{
Expand Down Expand Up @@ -1117,16 +1119,20 @@ void SplitContainer::Node::layout(bool addSpacing, float _scale,
// vars
qreal minSize = qreal(48 * _scale);

qreal totalFlex = this->getChildrensTotalFlex(isVertical);
qreal totalFlex = std::max<qreal>(
0.0001, this->getChildrensTotalFlex(isVertical));
qreal totalSize = std::accumulate(
this->children_.begin(), this->children_.end(), qreal(0),
[=](int val, std::unique_ptr<Node> &node) {
return val + std::max<qreal>(this->getSize(isVertical) /
totalFlex *
node->getFlex(isVertical),
minSize);
return val + std::max<qreal>(
this->getSize(isVertical) /
std::max<qreal>(0.0001, totalFlex) *
node->getFlex(isVertical),
minSize);
});

totalSize = std::max<qreal>(0.0001, totalSize);

qreal sizeMultiplier = this->getSize(isVertical) / totalSize;
QRectF childRect = this->geometry_;

Expand Down Expand Up @@ -1242,7 +1248,7 @@ void SplitContainer::Node::layout(bool addSpacing, float _scale,
}
}
break;
};
}
}

SplitContainer::Node::Type SplitContainer::Node::toContainerType(Direction _dir)
Expand Down

0 comments on commit 6052846

Please sign in to comment.