Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Fix crash on invalid SDF dereference.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLoer committed Nov 21, 2016
1 parent 3669027 commit 2aa65d0
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/mbgl/text/glyph_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ std::set<int32_t> GlyphSet::determineLineBreaks(const std::u16string& logicalInp
float totalWidth = 0;

for (char16_t chr : logicalInput) {
totalWidth += sdfs.find(chr)->second.metrics.advance + spacing;
auto it = sdfs.find(chr);
if (it != sdfs.end())
totalWidth += it->second.metrics.advance + spacing;
}

uint32_t estimatedLineCount = std::fmax(1, std::ceil(totalWidth / maxWidth));
Expand All @@ -113,8 +115,11 @@ std::set<int32_t> GlyphSet::determineLineBreaks(const std::u16string& logicalInp
float lastSafeBreakX = 0;

for (uint32_t i = 0; i < logicalInput.size(); i++) {
char16_t chr = logicalInput[i];
const SDFGlyph& glyph = sdfs.find(chr)->second;
auto it = sdfs.find(logicalInput[i]);
if (it==sdfs.end())
continue;

const SDFGlyph& glyph = it->second;
currentX += glyph.metrics.advance + spacing;
if ( currentX > maxWidth && lastSafeBreak > 0 )
{
Expand Down Expand Up @@ -148,12 +153,6 @@ void GlyphSet::shapeLines(Shaping& shaping, const std::vector<std::u16string>& l

for (std::u16string line : lines)
{
// Filter out any missing glyphs ahead of time, after this assume we have all glyphs
line.erase(std::remove_if(line.begin(),
line.end(),
[ & ] (const char16_t& chr) -> bool { auto it = sdfs.find(chr); return it == sdfs.end(); }),
line.end());

// Collapse whitespace so it doesn't throw off justification
boost::algorithm::trim_if(line, boost::algorithm::is_any_of(u" \t\n\v\f\r"));

Expand All @@ -163,11 +162,18 @@ void GlyphSet::shapeLines(Shaping& shaping, const std::vector<std::u16string>& l
uint32_t lineStartIndex = shaping.positionedGlyphs.size();
for (char16_t chr : line)
{
const SDFGlyph& glyph = sdfs.find(chr)->second;
auto it = sdfs.find(chr);
if (it == sdfs.end())
continue;

const SDFGlyph& glyph = it->second;
shaping.positionedGlyphs.emplace_back(chr, x, y);
x += glyph.metrics.advance + spacing;
}

if (shaping.positionedGlyphs.size() == lineStartIndex)
continue;

maxLineLength = util::max(x,maxLineLength);

justifyLine(shaping.positionedGlyphs, sdfs, lineStartIndex, shaping.positionedGlyphs.size() - 1, justify);
Expand Down

0 comments on commit 2aa65d0

Please sign in to comment.