Skip to content

Commit

Permalink
Merge pull request #141 from SixLabors/js/fix-140
Browse files Browse the repository at this point in the history
Use bit masking to limit the glyph id to prevent an off by on error
  • Loading branch information
JimBobSquarePants authored Jun 17, 2020
2 parents 007b5e7 + c2ac75b commit 56e1088
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Tables/General/CMap/Format4SubTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override bool TryGetGlyphId(int codePoint, out ushort glyphId)
{
if (seg.Offset == 0)
{
glyphId = (ushort)((charAsInt + seg.Delta) % ushort.MaxValue); // TODO: bitmask instead?
glyphId = (ushort)((charAsInt + seg.Delta) & ushort.MaxValue);
return true;
}
else
Expand Down
29 changes: 29 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_104.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using SixLabors.Fonts.Tables.General.CMap;
using Xunit;
using static SixLabors.Fonts.Tables.General.CMap.Format4SubTable;

namespace SixLabors.Fonts.Tests.Issues
{
public class Issues_104
{
[Fact]
public void Format4SubTableWithSegmentsHasOffByOneWhenOverflowing()
{
var tbl = new Format4SubTable(0, WellKnownIds.PlatformIDs.Windows, 0, new[] {
new Segment(0,
ushort.MaxValue, // end
ushort.MinValue, // start of range
(short)(short.MaxValue), //delta
0 // zero to force correctly tested codepath
)
}, null);

const int delta = short.MaxValue + 2; // extra 2 to handle the difference between ushort and short when offsettings

const int codePoint = delta + 5;
Assert.True(tbl.TryGetGlyphId(codePoint, out ushort gid));

Assert.Equal(5, gid);
}
}
}

0 comments on commit 56e1088

Please sign in to comment.