diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 857e18373..5b9954d98 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -108,5 +108,13 @@ #define HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH 64 #endif +#ifndef HB_SVG_MAX_PATH_SEGMENTS +#define HB_SVG_MAX_PATH_SEGMENTS 262144 +#endif + +#ifndef HB_RASTER_MAX_BUFFER_SIZE +#define HB_RASTER_MAX_BUFFER_SIZE ((size_t) 1 << 24) +#endif + #endif /* HB_LIMITS_HH */ diff --git a/src/hb-raster-image.cc b/src/hb-raster-image.cc index f339c00b7..ea89aa61e 100644 --- a/src/hb-raster-image.cc +++ b/src/hb-raster-image.cc @@ -395,8 +395,6 @@ composite_pixel (uint32_t src, uint32_t dst, /* hb_raster_image_t */ -#define HB_RASTER_IMAGE_MAX_BUFFER_SIZE ((size_t) 1 << 30) - unsigned hb_raster_image_t::bytes_per_pixel (hb_raster_format_t format) { @@ -423,7 +421,7 @@ hb_raster_image_t::configure (hb_raster_format_t format, return false; size_t buf_size = (size_t) extents.stride * extents.height; - if (buf_size > HB_RASTER_IMAGE_MAX_BUFFER_SIZE) + if (buf_size > HB_RASTER_MAX_BUFFER_SIZE) return false; if (unlikely (!buffer.resize_dirty (buf_size))) return false; diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index a88e0f044..84657cdd6 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -213,7 +213,9 @@ hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c, } /* Allocate alpha buffer and intersect with previous clip */ - if (unlikely (!new_clip.alpha.resize (new_clip.stride * h))) + size_t clip_size = (size_t) new_clip.stride * h; + if (unlikely (clip_size > HB_RASTER_MAX_BUFFER_SIZE || + !new_clip.alpha.resize ((unsigned) clip_size))) { hb_raster_draw_recycle_image (rdr, mask_img); hb_raster_paint_push_empty_clip (c, w, h); @@ -434,12 +436,14 @@ hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED, { /* General case: rasterize transformed quad as alpha mask */ new_clip.is_rect = false; - if (unlikely (!new_clip.alpha.resize (new_clip.stride * h))) + size_t clip_size = (size_t) new_clip.stride * h; + if (unlikely (clip_size > HB_RASTER_MAX_BUFFER_SIZE || + !new_clip.alpha.resize ((unsigned) clip_size))) { hb_raster_paint_push_empty_clip (c, w, h); return; } - hb_memset (new_clip.alpha.arrayZ, 0, new_clip.stride * h); + hb_memset (new_clip.alpha.arrayZ, 0, (unsigned) clip_size); /* Convert quad corners to pixel-relative coords */ float qx[4], qy[4]; diff --git a/src/hb-raster-svg-parse.cc b/src/hb-raster-svg-parse.cc index ecf689641..0bdb47106 100644 --- a/src/hb-raster-svg-parse.cc +++ b/src/hb-raster-svg-parse.cc @@ -246,9 +246,12 @@ hb_raster_svg_parse_path_data (hb_svg_str_t d, hb_draw_funcs_t *dfuncs, void *dr float last_cx = 0, last_cy = 0; char last_cmd = 0; char cmd = 0; + unsigned segments = 0; while (fp.p < fp.end) { + if (unlikely (segments++ >= HB_SVG_MAX_PATH_SEGMENTS)) + break; fp.skip_ws_comma (); if (fp.p >= fp.end) break; diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index 99379e33f..dd80f7464 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -217,7 +217,9 @@ hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, else { hb_svg_blob_meta_set_buffer (meta, data, allocated); - blob = hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, meta, hb_svg_blob_meta_destroy); + blob = hb_blob_create_or_fail (data, len, HB_MEMORY_MODE_WRITABLE, meta, hb_svg_blob_meta_destroy); + if (unlikely (!blob)) + return nullptr; } if (unlikely (blob == hb_blob_get_empty ())) diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index 0ab69be72..31e61bcaa 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -203,7 +203,9 @@ hb_svg_blob_from_buffer (hb_blob_t **recycled_blob, else { hb_svg_blob_meta_set_buffer (meta, data, allocated); - blob = hb_blob_create (data, len, HB_MEMORY_MODE_WRITABLE, meta, hb_svg_blob_meta_destroy); + blob = hb_blob_create_or_fail (data, len, HB_MEMORY_MODE_WRITABLE, meta, hb_svg_blob_meta_destroy); + if (unlikely (!blob)) + return nullptr; } if (unlikely (blob == hb_blob_get_empty ())) @@ -1011,14 +1013,15 @@ hb_vector_paint_funcs_get () return static_vector_paint_funcs.get_unconst (); } -static void +static hb_bool_t hb_vector_paint_ensure_initialized (hb_vector_paint_t *paint) { if (paint->group_stack.length) - return; - auto *root = paint->group_stack.push (); - if (likely (root)) - root->alloc (4096); + return true; + if (unlikely (!paint->group_stack.push_or_fail ())) + return false; + paint->group_stack.tail ().alloc (4096); + return !paint->group_stack.in_error (); } static void @@ -1030,7 +1033,8 @@ hb_vector_paint_push_transform (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; if (unlikely (paint->transform_group_overflow_depth)) { @@ -1076,7 +1080,8 @@ hb_vector_paint_pop_transform (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; if (unlikely (paint->transform_group_overflow_depth)) { paint->transform_group_overflow_depth--; @@ -1099,7 +1104,8 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; if (!hb_set_has (paint->defined_outlines, glyph)) { @@ -1137,7 +1143,8 @@ hb_vector_paint_push_clip_rectangle (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; unsigned clip_id = paint->clip_rect_counter++; hb_svg_append_str (&paint->defs, "current_body (), "\n"); } @@ -1175,7 +1183,8 @@ hb_vector_paint_color (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; hb_color_t c = color; if (is_foreground) @@ -1202,7 +1211,8 @@ hb_vector_paint_image (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return false; auto &body = paint->current_body (); if (format == HB_TAG ('s','v','g',' ')) @@ -1287,7 +1297,8 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; hb_vector_t &stops = paint->color_stops_scratch; if (!hb_svg_get_color_stops (paint, color_line, &stops) || !stops.length) @@ -1328,7 +1339,8 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; hb_vector_t &stops = paint->color_stops_scratch; if (!hb_svg_get_color_stops (paint, color_line, &stops) || !stops.length) @@ -1376,7 +1388,8 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; hb_vector_t &stops = paint->color_stops_scratch; if (!hb_svg_get_color_stops (paint, color_line, &stops) || !stops.length) @@ -1400,8 +1413,9 @@ hb_vector_paint_push_group (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); - paint->group_stack.push (hb_vector_t {}); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; + paint->group_stack.push_or_fail (hb_vector_t {}); } static void @@ -1411,7 +1425,8 @@ hb_vector_paint_pop_group (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return; if (paint->group_stack.length < 2) return; @@ -1439,7 +1454,8 @@ hb_vector_paint_color_glyph (hb_paint_funcs_t *, void *) { auto *paint = (hb_vector_paint_t *) paint_data; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return false; hb_codepoint_t old_gid = paint->current_svg_image_glyph; hb_face_t *old_face = paint->current_face; paint->current_svg_image_glyph = glyph; @@ -1898,7 +1914,8 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, } } - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return false; bool can_cache = !paint->flat; hb_svg_color_glyph_cache_key_t cache_key = hb_svg_color_glyph_cache_key (glyph, @@ -1933,7 +1950,8 @@ hb_vector_paint_glyph (hb_vector_paint_t *paint, bool has_svg_image = false; if (can_cache) { - paint->group_stack.push (hb_vector_t {}); + if (unlikely (!paint->group_stack.push_or_fail (hb_vector_t {}))) + return false; paint->current_color_glyph_has_svg_image = false; } @@ -2090,7 +2108,8 @@ hb_vector_paint_render (hb_vector_paint_t *paint) if (!paint->has_extents) return nullptr; - hb_vector_paint_ensure_initialized (paint); + if (unlikely (!hb_vector_paint_ensure_initialized (paint))) + return nullptr; hb_vector_t out; hb_svg_recover_recycled_buffer (paint->recycled_blob, &out); diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index d0b35c60c..3cc06a916 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -178,7 +178,8 @@ hb_svg_add_unique_id (hb_vector_t *v, if (unlikely (!seen_ids->set (key, true))) return false; auto *slot = v->push (); - if (!slot) return false; + if (unlikely (v->in_error ())) + return false; *slot = key; return true; } diff --git a/test/fuzzing/hb-raster-fuzzer.cc b/test/fuzzing/hb-raster-fuzzer.cc index 713d5d69b..277f820c2 100644 --- a/test/fuzzing/hb-raster-fuzzer.cc +++ b/test/fuzzing/hb-raster-fuzzer.cc @@ -56,6 +56,13 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_raster_draw_t *draw = hb_raster_draw_create_or_fail (); hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); + if (!draw || !paint) + { + hb_raster_draw_destroy (draw); + hb_raster_paint_destroy (paint); + hb_buffer_destroy (buffer); + return 0; + } hb_raster_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); draw_shaped_text (input.font, buffer, draw, paint); @@ -101,4 +108,3 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_buffer_destroy (buffer); return counter ? 0 : 0; } - diff --git a/test/fuzzing/hb-vector-fuzzer.cc b/test/fuzzing/hb-vector-fuzzer.cc index 1fdb38970..a1c19b6ba 100644 --- a/test/fuzzing/hb-vector-fuzzer.cc +++ b/test/fuzzing/hb-vector-fuzzer.cc @@ -56,6 +56,13 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (HB_VECTOR_FORMAT_SVG); hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG); + if (!draw || !paint) + { + hb_vector_draw_destroy (draw); + hb_vector_paint_destroy (paint); + hb_buffer_destroy (buffer); + return 0; + } hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); unsigned precision = size ? data[size - 1] % 5 : 0; @@ -105,4 +112,3 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) hb_buffer_destroy (buffer); return counter ? 0 : 0; } -