Skip to content

Commit bab3475

Browse files
authored
Merge pull request matplotlib#31859 from arshsmith/ft2font-set-charmap-bounds
FIX: reject out-of-range charmap index in FT2Font.set_charmap
2 parents 2d53520 + 2d32ccf commit bab3475

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/matplotlib/tests/test_ft2font.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ def enc(name):
346346
# Though the encoding is different, the glyph should be the same.
347347
assert unic[u] == armn[m]
348348

349+
# Out-of-range charmap indices must be rejected rather than indexing out of
350+
# bounds. A negative index previously passed the upper-bound-only check and
351+
# read face->charmaps[i] out of bounds.
352+
with pytest.raises(RuntimeError, match='exceeds the available number'):
353+
font.set_charmap(-1)
354+
with pytest.raises(RuntimeError, match='exceeds the available number'):
355+
font.set_charmap(font.num_charmaps)
356+
349357

350358
_expected_sfnt_names = {
351359
'DejaVu Sans': {

src/ft2font.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void FT2Font::_set_transform(
269269

270270
void FT2Font::set_charmap(int i)
271271
{
272-
if (i >= face->num_charmaps) {
272+
if (i < 0 || i >= face->num_charmaps) {
273273
throw std::runtime_error("i exceeds the available number of char maps");
274274
}
275275
FT_CHECK(FT_Set_Charmap, face, face->charmaps[i]);

0 commit comments

Comments
 (0)