Font selection: harden reading of variable font data

- Convert OpenType axis tags to Python as four Latin-1 characters instead
  of UTF-8, so that fonts with axis tags that are not valid UTF-8 no longer
  cause an exception. On macOS this made font selection fail entirely if
  the selected variable font had such a tag. Convert axis_map keys back to
  tags losslessly, validating them, instead of reading four bytes from the
  UTF-8 encoding, which could read past the end of short keys.
- variable_data_from_font_file() now raises ValueError for files that are
  not valid fonts, instead of an OSError with a stale or zero errno.
- Avoid quadratic work when reading a crafted font with many axes, named
  instances and name records: de-duplicate axis tags once rather than per
  instance, and binary search name records by name id. A 64MB crafted font
  that took over a minute to parse now takes about a second.
This commit is contained in:
Kovid Goyal 2026-11-19 13:22:20 +05:30
parent b7b43f85c0
commit 3fb8949168
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 179 additions and 53 deletions

View file

@ -144,19 +144,31 @@ dealloc(CTFace *self) {
Py_TYPE(self)->tp_free((PyObject *)self);
}
static const char *
tag_to_string(uint32_t tag, uint8_t bytes[5]) {
bytes[0] = (tag >> 24) & 0xff;
bytes[1] = (tag >> 16) & 0xff;
bytes[2] = (tag >> 8) & 0xff;
bytes[3] = (tag) & 0xff;
bytes[4] = 0;
return (const char *)bytes;
// OpenType tags are supposed to be printable ASCII but fonts in the wild can
// contain arbitrary bytes, which are not valid UTF-8. Use Latin-1 so that any
// tag converts to a four character string and back losslessly.
static PyObject *
tag_to_python(uint32_t tag) {
const char b[4] = {(char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)tag};
return PyUnicode_DecodeLatin1(b, sizeof(b), NULL);
}
static uint32_t
string_to_tag(const uint8_t *bytes) {
return (((uint32_t)bytes[0]) << 24) | (((uint32_t)bytes[1]) << 16) | (((uint32_t)bytes[2]) << 8) | bytes[3];
static bool
tag_from_python(PyObject *s, uint32_t *tag) {
if (!PyUnicode_Check(s) || PyUnicode_GET_LENGTH(s) != 4) {
PyErr_Format(PyExc_ValueError, "%R is not a valid OpenType tag", s);
return false;
}
*tag = 0;
for (Py_ssize_t i = 0; i < 4; i++) {
const Py_UCS4 ch = PyUnicode_READ_CHAR(s, i);
if (ch > 0xff) {
PyErr_Format(PyExc_ValueError, "%R is not a valid OpenType tag", s);
return false;
}
*tag = (*tag << 8) | ch;
}
return true;
}
FontFeatures *
@ -170,12 +182,12 @@ add_variation_pair(const void *key_, const void *value_, void *ctx) {
CFNumberRef key = key_, value = value_;
uint32_t tag;
double val;
if (PyErr_Occurred()) return;
if (!CFNumberGetValue(key, kCFNumberSInt32Type, &tag)) return;
if (!CFNumberGetValue(value, kCFNumberDoubleType, &val)) return;
uint8_t tag_string[5];
tag_to_string(tag, tag_string);
RAII_PyObject(pykey, tag_to_python(tag));
RAII_PyObject(pyval, PyFloat_FromDouble(val));
if (pyval) PyDict_SetItemString(ans, (const char *)tag_string, pyval);
if (pykey && pyval) PyDict_SetItem(ans, pykey, pyval);
}
static PyObject *
@ -290,8 +302,10 @@ font_descriptor_from_python(PyObject *src) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(t, &pos, &key, &value)) {
double val = PyFloat_AS_DOUBLE(value);
uint32_t tag = string_to_tag((const uint8_t *)PyUnicode_AsUTF8(key));
const double val = PyFloat_AsDouble(value);
if (val == -1 && PyErr_Occurred()) return NULL;
uint32_t tag;
if (!tag_from_python(key, &tag)) return NULL;
RAII_CoreFoundation(CFNumberRef, cf_tag, CFNumberCreate(NULL, kCFNumberSInt32Type, &tag));
RAII_CoreFoundation(CFNumberRef, cf_val, CFNumberCreate(NULL, kCFNumberDoubleType, &val));
CFDictionaryAddValue(axis_map, cf_tag, cf_val);
@ -1775,9 +1789,9 @@ fs_backend_descriptor(const FSFont *f) {
RAII_PyObject(axis_map, PyDict_New());
if (!axis_map) return NULL;
for (size_t i = 0; i < f->num_axes; i++) {
uint8_t tag[5];
RAII_PyObject(tag, tag_to_python(f->axes[i].tag));
RAII_PyObject(val, PyFloat_FromDouble(f->axes[i].value));
if (!val || PyDict_SetItemString(axis_map, tag_to_string(f->axes[i].tag, tag), val) != 0) return NULL;
if (!tag || !val || PyDict_SetItem(axis_map, tag, val) != 0) return NULL;
}
if (PyDict_SetItemString(ans, "axis_map", axis_map) != 0) return NULL;
}

View file

@ -219,10 +219,13 @@ pread_all(int fd, uint8_t *buf, size_t sz, off_t offset) {
while (done < sz) {
ssize_t n = pread(fd, buf + done, sz - done, offset + (off_t)done);
if (n < 0) {
if (errno == EINTR) continue;
if (errno == EINTR) {
errno = 0;
continue;
}
return false;
}
if (n == 0) return false;
if (n == 0) return false; // truncated file, errno is left untouched
done += (size_t)n;
}
return true;
@ -524,11 +527,14 @@ end:
}
#endif
// On failure errno is non-zero if the failure was caused by an OS error and
// zero if the file is not a valid font or does not contain the specified face.
bool
fs_read_font_tables_from_file(const char *path, int index, FSFontTables *t) {
memset(t, 0, sizeof(*t));
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return false;
errno = 0;
bool ok = false;
uint8_t header[12];
uint8_t *records = NULL;
@ -580,13 +586,16 @@ fs_read_font_tables_from_file(const char *path, int index, FSFontTables *t) {
ok = true;
end:
free(records);
const int saved_errno = errno;
close(fd);
if (!ok) fs_free_font_tables(t);
errno = saved_errno;
return ok;
}
typedef struct NameRecord {
uint16_t platform_id, encoding_id, language_id, name_id, length;
uint32_t order; // position in the name table
const uint8_t *data;
} NameRecord;
@ -595,6 +604,13 @@ typedef struct NameTable {
size_t count, capacity;
} NameTable;
static int
cmp_name_records(const void *a_, const void *b_) {
const NameRecord *a = a_, *b = b_;
if (a->name_id != b->name_id) return a->name_id < b->name_id ? -1 : 1;
return a->order < b->order ? -1 : (a->order > b->order ? 1 : 0);
}
static void
parse_name_table(const uint8_t *table, size_t table_len, NameTable *nt) {
memset(nt, 0, sizeof(*nt));
@ -608,9 +624,15 @@ parse_name_table(const uint8_t *table, size_t table_len, NameTable *nt) {
const uint8_t *s = storage + be16(p + 10);
if (s + r.length > limit) continue;
r.data = s;
r.order = i;
grow(nt->records, nt->count, nt->capacity);
nt->records[nt->count++] = r;
}
// Sort by name id, preserving table order within a name id, so that
// get_best_name() can find the records for a name id with a binary search
// rather than a linear scan, which would make reading a crafted font with
// many axes, instances and name records quadratic.
if (nt->count > 1) qsort(nt->records, nt->count, sizeof(nt->records[0]), cmp_name_records);
}
static size_t
@ -677,27 +699,40 @@ decode_name_record(const NameRecord *r) {
static char *
get_best_name(const NameTable *nt, unsigned name_id) {
const NameRecord *only = NULL;
size_t count = 0;
for (size_t i = 0; i < nt->count; i++) {
if (nt->records[i].name_id == name_id) {
if (!count) only = nt->records + i;
count++;
}
// nt->records is sorted by name_id, find the first record with name_id
size_t lo = 0, hi = nt->count;
while (lo < hi) {
const size_t mid = lo + (hi - lo) / 2;
if (nt->records[mid].name_id < name_id) lo = mid + 1;
else hi = mid;
}
size_t end = lo;
while (end < nt->count && nt->records[end].name_id == name_id) end++;
const size_t count = end - lo;
if (!count) return xstrdup("");
if (count == 1) return decode_name_record(only);
if (count == 1) return decode_name_record(nt->records + lo);
static const uint16_t priorities[][3] = {{3, 1, 1033}, {1, 0, 0}, {0, 6, 0}, {0, 4, 0}, {0, 3, 0}, {0, 2, 0}, {0, 1, 0}};
for (size_t p = 0; p < arraysz(priorities); p++) {
for (size_t i = 0; i < nt->count; i++) {
for (size_t i = lo; i < end; i++) {
const NameRecord *r = nt->records + i;
if (r->name_id == name_id && r->platform_id == priorities[p][0] && r->encoding_id == priorities[p][1] && r->language_id == priorities[p][2])
return decode_name_record(r);
if (r->platform_id == priorities[p][0] && r->encoding_id == priorities[p][1] && r->language_id == priorities[p][2]) return decode_name_record(r);
}
}
return xstrdup("");
}
typedef struct TagIndex {
uint32_t tag;
size_t index;
} TagIndex;
static int
cmp_tag_index(const void *a_, const void *b_) {
const TagIndex *a = a_, *b = b_;
if (a->tag != b->tag) return a->tag < b->tag ? -1 : 1;
return a->index < b->index ? -1 : (a->index > b->index ? 1 : 0);
}
static void
read_fvar(const uint8_t *table, size_t table_len, const NameTable *nt, FSVariableData *vd) {
if (!table || table_len < 28) return;
@ -717,23 +752,31 @@ read_fvar(const uint8_t *table, size_t table_len, const NameTable *nt, FSVariabl
a->hidden = be16(pos + 16) & 1;
a->strid = get_best_name(nt, be16(pos + 18));
}
// A named style uses each tag at most once, like a dict: in the order the
// tag first occurs, with the value of its last occurrence. Compute the
// slot for each axis once, so that this is not quadratic in the number of
// axes for every instance, which a crafted font could exploit.
RAII_ALLOC(size_t, slots, xcalloc(vd->num_axes ? vd->num_axes : 1, sizeof(size_t)));
size_t num_slots = 0;
if (vd->num_axes) {
RAII_ALLOC(TagIndex, sorted, xcalloc(vd->num_axes, sizeof(TagIndex)));
for (size_t i = 0; i < vd->num_axes; i++) sorted[i] = (TagIndex){.tag = vd->axes[i].tag, .index = i};
qsort(sorted, vd->num_axes, sizeof(sorted[0]), cmp_tag_index);
// slots[i] temporarily holds the index of the first axis with the same tag as axis i
for (size_t i = 0, first = 0; i < vd->num_axes; i++) {
if (!i || sorted[i].tag != sorted[i - 1].tag) first = sorted[i].index;
slots[sorted[i].index] = first;
}
for (size_t i = 0; i < vd->num_axes; i++) slots[i] = slots[i] == i ? num_slots++ : slots[slots[i]];
}
vd->named_styles = xcalloc(num_instances, sizeof(vd->named_styles[0]));
for (const uint8_t *pos = table + offset_to_axes + (size_t)num_axes * axis_size; pos + instance_size <= limit && vd->num_named_styles < num_instances;
pos += instance_size) {
FSNamedStyle *ns = vd->named_styles + vd->num_named_styles++;
ns->name = get_best_name(nt, be16(pos));
ns->values = xcalloc(vd->num_axes, sizeof(ns->values[0]));
for (size_t i = 0; i < vd->num_axes; i++) {
// use the same tag at most once, like a dict
bool found = false;
for (size_t j = 0; j < ns->num_values; j++) {
if (ns->values[j].tag == vd->axes[i].tag) {
ns->values[j].value = fixed(pos + 4 + 4 * i);
found = true;
}
}
if (!found) ns->values[ns->num_values++] = (FSAxisValue){.tag = vd->axes[i].tag, .value = fixed(pos + 4 + 4 * i)};
}
ns->values = xcalloc(num_slots ? num_slots : 1, sizeof(ns->values[0]));
ns->num_values = num_slots;
for (size_t i = 0; i < vd->num_axes; i++) ns->values[slots[i]] = (FSAxisValue){.tag = vd->axes[i].tag, .value = fixed(pos + 4 + 4 * i)};
const uint16_t psname_id = has_postscript_name ? be16(pos + 4 + size_of_coordinates) : 0xffff;
ns->psname = (psname_id != 0xffff && psname_id) ? get_best_name(nt, psname_id) : xstrdup("");
}
@ -2182,15 +2225,23 @@ finish_font_selection(PyObject *self UNUSED, PyObject *args UNUSED) {
return ans;
}
// OpenType tags are supposed to be printable ASCII but fonts in the wild can
// contain arbitrary bytes, which are not valid UTF-8. Decode as Latin-1 so that
// any tag converts to a four character string and can be converted back losslessly.
static PyObject *
tag_to_python(uint32_t tag) {
const char b[4] = {(char)(tag >> 24), (char)(tag >> 16), (char)(tag >> 8), (char)tag};
return PyUnicode_DecodeLatin1(b, sizeof(b), NULL);
}
static PyObject *
axis_values_to_python(const FSAxisValue *values, size_t count) {
RAII_PyObject(ans, PyDict_New());
if (!ans) return NULL;
for (size_t i = 0; i < count; i++) {
char tag[5];
tag_to_str(values[i].tag, tag);
RAII_PyObject(tag, tag_to_python(values[i].tag));
RAII_PyObject(v, PyFloat_FromDouble(values[i].value));
if (!v || PyDict_SetItemString(ans, tag, v) != 0) return NULL;
if (!tag || !v || PyDict_SetItem(ans, tag, v) != 0) return NULL;
}
Py_INCREF(ans);
return ans;
@ -2202,12 +2253,12 @@ variable_data_to_python(const FSVariableData *vd) {
RAII_PyObject(named_styles, PyTuple_New(vd->num_named_styles));
RAII_PyObject(design_axes, PyTuple_New(vd->num_design_axes));
if (!axes || !named_styles || !design_axes) return NULL;
char tag[5];
for (size_t i = 0; i < vd->num_axes; i++) {
const FSVarAxis *a = vd->axes + i;
tag_to_str(a->tag, tag);
PyObject *tag = tag_to_python(a->tag);
if (!tag) return NULL;
PyObject *x = Py_BuildValue(
"{sd sd sd sO ss ss}",
"{sd sd sd sO sN ss}",
"minimum",
a->minimum,
"maximum",
@ -2225,7 +2276,9 @@ variable_data_to_python(const FSVariableData *vd) {
}
for (size_t i = 0; i < vd->num_named_styles; i++) {
const FSNamedStyle *ns = vd->named_styles + i;
PyObject *x = Py_BuildValue("{sN ss ss}", "axis_values", axis_values_to_python(ns->values, ns->num_values), "name", ns->name, "psname", ns->psname);
PyObject *axis_values = axis_values_to_python(ns->values, ns->num_values);
if (!axis_values) return NULL;
PyObject *x = Py_BuildValue("{sN ss ss}", "axis_values", axis_values, "name", ns->name, "psname", ns->psname);
if (!x) return NULL;
PyTuple_SET_ITEM(named_styles, i, x);
}
@ -2262,8 +2315,9 @@ variable_data_to_python(const FSVariableData *vd) {
if (!x) return NULL;
PyList_SET_ITEM(values, j, x);
}
tag_to_str(da->tag, tag);
PyObject *x = Py_BuildValue("{ss ss sI sO}", "tag", tag, "name", da->name, "ordering", da->ordering, "values", values);
PyObject *tag = tag_to_python(da->tag);
if (!tag) return NULL;
PyObject *x = Py_BuildValue("{sN ss sI sO}", "tag", tag, "name", da->name, "ordering", da->ordering, "values", values);
if (!x) return NULL;
PyTuple_SET_ITEM(design_axes, i, x);
}
@ -2288,7 +2342,8 @@ variable_data_from_font_file(PyObject *self UNUSED, PyObject *args) {
if (!PyArg_ParseTuple(args, "s|i", &path, &index)) return NULL;
FSFontTables t = {0};
if (!fs_read_font_tables_from_file(path, index, &t)) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
if (errno) PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
else PyErr_Format(PyExc_ValueError, "%s is not a valid font file or does not contain a face with index %d", path, index);
return NULL;
}
FSVariableData vd;

View file

@ -292,6 +292,63 @@ class Selection(BaseTest):
if self.is_ci and not count:
raise AssertionError('No variable fonts found')
def test_variable_data_from_crafted_font(self):
import struct
from kitty.fast_data_types import variable_data_from_font_file
def name_table(names: dict[int, str]) -> bytes:
records, storage = b'', b''
for name_id, text in names.items():
data = text.encode('utf-16-be')
records += struct.pack('>6H', 3, 1, 1033, name_id, len(data), len(storage))
storage += data
return struct.pack('>3H', 0, len(names), 6 + len(records)) + records + storage
def fvar_table(axes: list[tuple[bytes, int]], instances: list[tuple[int, list[float], int]]) -> bytes:
instance_size = 4 + 4 * len(axes) + 2
ans = struct.pack('>8H', 1, 0, 16, 2, len(axes), 20, len(instances), instance_size)
for tag, name_id in axes:
ans += tag + struct.pack('>3iHH', 0, 0, 1000 << 16, 0, name_id)
for name_id, coords, psname_id in instances:
ans += struct.pack('>HH', name_id, 0) + b''.join(struct.pack('>i', int(c * 65536)) for c in coords) + struct.pack('>H', psname_id)
return ans
def sfnt(tables: dict[bytes, bytes]) -> bytes:
offset = 12 + 16 * len(tables)
header, data = struct.pack('>IHHHH', 0x00010000, len(tables), 0, 0, 0), b''
for tag, table in tables.items():
header += tag + struct.pack('>III', 0, offset + len(data), len(table))
data += table + b'\0' * (-len(table) % 4)
return header + data
names = {256: 'Weight', 257: 'Odd', 258: 'Bold', 259: 'Test-Bold', 1: 'Test', 25: 'TestVar'}
# An axis tag that is not valid UTF-8 and a duplicated axis tag
odd_tag = b'\xff\xfe\x80a'
fvar = fvar_table([(b'wght', 256), (odd_tag, 257), (b'wght', 256)], [(258, [700, 1, 800], 259)])
with tempfile.TemporaryDirectory() as tdir:
path = os.path.join(tdir, 'test.ttf')
with open(path, 'wb') as f:
f.write(sfnt({b'fvar': fvar, b'name': name_table(names)}))
vd = variable_data_from_font_file(path)
odd = odd_tag.decode('latin-1')
self.ae([a['tag'] for a in vd['axes']], ['wght', odd, 'wght'])
self.ae([a['strid'] for a in vd['axes']], ['Weight', 'Odd', 'Weight'])
self.ae(vd['variations_postscript_name_prefix'], 'TestVar')
self.ae(len(vd['named_styles']), 1)
ns = vd['named_styles'][0]
self.ae((ns['name'], ns['psname']), ('Bold', 'Test-Bold'))
# each tag used once, in order of first occurrence, with the value of the last occurrence
self.ae(list(ns['axis_values'].items()), [('wght', 800.0), (odd, 1.0)])
# A file that is not a valid font is a ValueError, not an OSError with a meaningless errno
with open(path, 'wb') as f:
f.write(b'\0\1\0\0\0')
with self.assertRaises(ValueError):
variable_data_from_font_file(path)
with self.assertRaises(FileNotFoundError):
variable_data_from_font_file(os.path.join(tdir, 'does-not-exist.ttf'))
def test_invalid_font_feature(self):
with self.assertRaises(ValueError) as cm:
ParsedFontFeature('!!bad!!')