[vector][svg] Harden subset spans under failing-malloc

Validate cached SVG spans before use and make cache parsing
allocation-failure safe.

Fixes: https://oss-fuzz.com/testcase-detail/5448347908636672
Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod committed 2026-03-17 09:12:35 -06:00
1 parent 53cd1e5b08
commit ba53130d2c
3 files changed
+40 -28

No files matched your search

+22 -28
View File
@@ -432,8 +432,10 @@ parse_cache_entries_linear (const char *svg,
{
open_elem_t stack[MAX_DEPTH];
unsigned depth = 0;
defs_entries->alloc (256);
id_entries->alloc (256);
if (unlikely (!defs_entries->alloc (256) ||
!glyph_spans->alloc (256) ||
!id_entries->alloc (256)))
return false;
unsigned defs_depth = 0;
unsigned i = 0;
@@ -516,30 +518,24 @@ parse_cache_entries_linear (const char *svg,
if (e.id.len)
{
auto *id_slot = id_entries->push ();
if (unlikely (!id_slot))
if (unlikely (!id_entries->push_or_fail (id_entry_t {e.id, (uint32_t) e.start, (uint32_t) end})))
return false;
*id_slot = {e.id, (uint32_t) e.start, (uint32_t) end};
if (e.in_defs_content)
{
auto *slot = defs_entries->push ();
if (unlikely (!slot))
if (unlikely (!defs_entries->push_or_fail ()))
return false;
slot->id = e.id;
slot->start = e.start;
slot->end = end;
auto &slot = defs_entries->tail ();
slot.id = e.id;
slot.start = e.start;
slot.end = end;
}
hb_codepoint_t gid;
if (parse_glyph_id_span (e.id, &gid))
{
auto *span = glyph_spans->push ();
if (unlikely (!span))
if (unlikely (!glyph_spans->push_or_fail (glyph_entry_t {gid, (uint32_t) e.start, (uint32_t) end})))
return false;
span->glyph = gid;
span->start = (uint32_t) e.start;
span->end = (uint32_t) end;
}
}
@@ -564,30 +560,24 @@ parse_cache_entries_linear (const char *svg,
unsigned end = gt + 1;
if (e.id.len)
{
auto *id_slot = id_entries->push ();
if (unlikely (!id_slot))
if (unlikely (!id_entries->push_or_fail (id_entry_t {e.id, (uint32_t) e.start, (uint32_t) end})))
return false;
*id_slot = {e.id, (uint32_t) e.start, (uint32_t) end};
if (e.in_defs_content)
{
auto *slot = defs_entries->push ();
if (unlikely (!slot))
if (unlikely (!defs_entries->push_or_fail ()))
return false;
slot->id = e.id;
slot->start = e.start;
slot->end = end;
auto &slot = defs_entries->tail ();
slot.id = e.id;
slot.start = e.start;
slot.end = end;
}
hb_codepoint_t gid;
if (parse_glyph_id_span (e.id, &gid))
{
auto *span = glyph_spans->push ();
if (unlikely (!span))
if (unlikely (!glyph_spans->push_or_fail (glyph_entry_t {gid, (uint32_t) e.start, (uint32_t) end})))
return false;
span->glyph = gid;
span->start = (uint32_t) e.start;
span->end = (uint32_t) end;
}
}
}
@@ -788,6 +778,8 @@ SVG::accelerator_t::doc_cache_get_glyph_span (const svg_doc_cache_t *doc,
const auto &span = doc->glyph_spans.arrayZ[glyph - doc->start_glyph];
if (span.first == INVALID_SPAN)
return false;
if (unlikely (span.first > span.second || span.second > doc->len))
return false;
if (start) *start = span.first;
if (end) *end = span.second;
@@ -805,6 +797,8 @@ SVG::accelerator_t::doc_cache_find_id_span (const svg_doc_cache_t *doc,
hb_pair_t<uint32_t, uint32_t> *span = nullptr;
if (!doc->id_spans.has (id, &span))
return false;
if (unlikely (span->first > span->second || span->second > doc->len))
return false;
if (start) *start = span->first;
if (end) *end = span->second;
return true;
+18
View File
@@ -43,6 +43,14 @@
#ifndef HB_NO_SVG
#include "hb-vector-svg.hh"
static bool
hb_svg_range_is_valid (unsigned start,
unsigned end,
unsigned len)
{
return start <= end && end <= len;
}
static bool
hb_svg_append_with_prefix (hb_vector_t<char> *out,
const char *s,
@@ -330,6 +338,8 @@ hb_svg_subset_glyph_image (hb_face_t *face,
if (!face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end))
goto done;
if (unlikely (!hb_svg_range_is_valid (glyph_start, glyph_end, len)))
goto done;
defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache);
if (!hb_svg_find_root_open_tag (svg, len, &root_open_len, &root_missing_viewport))
@@ -358,6 +368,8 @@ hb_svg_subset_glyph_image (hb_face_t *face,
chosen_def_marks.arrayZ[i] = 1;
if (unlikely (!chosen_defs.push_or_fail (i)))
goto done;
if (unlikely (!hb_svg_range_is_valid (e.start, e.end, len)))
goto done;
if (!hb_svg_collect_refs (svg + e.start, e.end - e.start,
&needed_ids, &needed_ids_set))
goto done;
@@ -376,6 +388,8 @@ hb_svg_subset_glyph_image (hb_face_t *face,
for (unsigned i = 0; i < chosen_defs.length; i++)
{
const auto &e = defs_entries->arrayZ[chosen_defs.arrayZ[i]];
if (unlikely (!hb_svg_range_is_valid (e.start, e.end, len)))
goto done;
if (!hb_svg_append_with_prefix (defs_dst, svg + e.start, e.end - e.start, prefix, (unsigned) prefix_len))
goto done;
if (!hb_svg_append_c (defs_dst, '\n'))
@@ -384,6 +398,8 @@ hb_svg_subset_glyph_image (hb_face_t *face,
if (glyph_is_root_svg && root_missing_viewport && root_open_len > 1)
{
if (unlikely (root_open_len > glyph_end - glyph_start))
goto done;
unsigned upem = face->get_upem ();
if (!hb_svg_append_with_prefix (body_dst,
svg + glyph_start,
@@ -413,6 +429,8 @@ hb_svg_subset_glyph_image (hb_face_t *face,
}
else if (glyph_is_root_svg && root_open_len > 1)
{
if (unlikely (root_open_len > glyph_end - glyph_start))
goto done;
if (!hb_svg_append_with_prefix (body_dst,
svg + glyph_start,
root_open_len - 1,