diff --git a/src/hb-algs.hh b/src/hb-algs.hh index f0af8ed19..d35a723f7 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1673,4 +1673,51 @@ double solve_itp (func_t f, } +/* + * Scope guard: runs a callable at scope exit (RAII cleanup for + * non-HB-type resources — raw malloc'd buffers, paired init/end + * calls like inflateInit/inflateEnd, FT_Done_* handles, etc.). + * + * Prefer hb_unique_ptr_t etc. for HB types; this is + * for the long tail of cleanup that those wrappers don't cover. + * + * Usage: + * void *buf = hb_malloc (len); + * if (!buf) return false; + * HB_SCOPE_GUARD (hb_free (buf)); + * ... multiple fallible operations ... + * return true; // buf freed automatically on any return path + */ +template +struct hb_scope_guard_t +{ + explicit hb_scope_guard_t (F &&f) : f (std::move (f)), active (true) {} + hb_scope_guard_t (hb_scope_guard_t &&o) noexcept + : f (std::move (o.f)), active (o.active) { o.active = false; } + hb_scope_guard_t (const hb_scope_guard_t &) = delete; + hb_scope_guard_t &operator= (const hb_scope_guard_t &) = delete; + hb_scope_guard_t &operator= (hb_scope_guard_t &&) = delete; + ~hb_scope_guard_t () { if (active) f (); } + + /* Release: dismiss the guard so the cleanup does NOT run. Use + * when transferring ownership out of the scope. */ + void release () { active = false; } + + private: + F f; + bool active; +}; + +template +static inline hb_scope_guard_t hb_make_scope_guard (F &&f) +{ return hb_scope_guard_t (std::forward (f)); } + +#define HB_SCOPE_GUARD_NAME_(line) hb_scope_guard_##line +#define HB_SCOPE_GUARD_NAME(line) HB_SCOPE_GUARD_NAME_(line) +#define HB_SCOPE_GUARD(stmt) \ + auto HB_SCOPE_GUARD_NAME(__LINE__) = \ + hb_make_scope_guard ([&]() { stmt; }); \ + (void) HB_SCOPE_GUARD_NAME(__LINE__) + + #endif /* HB_ALGS_HH */ diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 6509b3bd8..850739604 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -627,16 +627,19 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, stops[i].offset = 1 - stops[i].offset; } + auto angles_guard = hb_make_scope_guard ([&]() { + if (angles != angles_) hb_free (angles); + }); + auto colors_guard = hb_make_scope_guard ([&]() { + if (colors != colors_) hb_free (colors); + }); + if (n_stops > PREALLOCATED_COLOR_STOPS) { angles = (float *) hb_malloc (sizeof (float) * n_stops); colors = (hb_cairo_color_t *) hb_malloc (sizeof (hb_cairo_color_t) * n_stops); if (unlikely (!angles || !colors)) - { - hb_free (angles); - hb_free (colors); return; - } } for (unsigned i = 0; i < n_stops; i++) @@ -673,7 +676,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, 0., &color0, HB_2_PI, &color0, pattern); - goto done; + return; } _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, @@ -710,7 +713,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, angles[n_stops - 1], &color0, HB_2_PI, &color0, pattern); - goto done; + return; } } else @@ -720,7 +723,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, span = angles[n_stops - 1] - angles[0]; if (!span) - goto done; + return; k = 0; if (angles[0] >= 0) @@ -804,7 +807,7 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, a0, c0, HB_2_PI, &color, pattern); - goto done; + return; } else { @@ -817,12 +820,6 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, } } -done: - - if (angles != angles_) - hb_free (angles); - if (colors != colors_) - hb_free (colors); } void diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc index 94b058828..7466c5bc7 100644 --- a/src/hb-coretext.cc +++ b/src/hb-coretext.cc @@ -84,26 +84,27 @@ _hb_cg_get_table_tags (const hb_face_t *face HB_UNUSED, CTFontRef ct_font = create_ct_font (cg_font, (CGFloat) HB_CORETEXT_DEFAULT_FONT_SIZE); auto arr = CTFontCopyAvailableTables (ct_font, kCTFontTableOptionNoOptions); + HB_SCOPE_GUARD (CFRelease (arr)); + HB_SCOPE_GUARD (CFRelease (ct_font)); unsigned population = (unsigned) CFArrayGetCount (arr); - unsigned end_offset; if (!table_count) - goto done; + return population; if (unlikely (start_offset >= population)) { *table_count = 0; - goto done; + return population; } - end_offset = start_offset + *table_count; + unsigned end_offset = start_offset + *table_count; if (unlikely (end_offset < start_offset)) { *table_count = 0; - goto done; + return population; } - end_offset= hb_min (end_offset, (unsigned) population); + end_offset = hb_min (end_offset, (unsigned) population); *table_count = end_offset - start_offset; for (unsigned i = start_offset; i < end_offset; i++) @@ -112,9 +113,6 @@ _hb_cg_get_table_tags (const hb_face_t *face HB_UNUSED, table_tags[i - start_offset] = tag; } -done: - CFRelease (arr); - CFRelease (ct_font); return population; } @@ -525,30 +523,29 @@ hb_coretext_font_create (CTFontRef ct_font) CFDictionaryRef variations = CTFontCopyVariation (ct_font); if (variations) { + HB_SCOPE_GUARD (CFRelease (variations)); + hb_vector_t vars; hb_vector_t keys; hb_vector_t values; CFIndex count = CFDictionaryGetCount (variations); - if (unlikely (!vars.alloc_exact (count) || !keys.resize_exact (count) || !values.resize_exact (count))) - goto done; - - // Fetch them one by one and collect in a vector of our own. - CFDictionaryGetKeysAndValues (variations, keys.arrayZ, values.arrayZ); - for (CFIndex i = 0; i < count; i++) + if (likely (vars.alloc_exact (count) && keys.resize_exact (count) && values.resize_exact (count))) { - int tag; - float value; - CFNumberGetValue ((CFNumberRef) keys.arrayZ[i], kCFNumberIntType, &tag); - CFNumberGetValue ((CFNumberRef) values.arrayZ[i], kCFNumberFloatType, &value); + // Fetch them one by one and collect in a vector of our own. + CFDictionaryGetKeysAndValues (variations, keys.arrayZ, values.arrayZ); + for (CFIndex i = 0; i < count; i++) + { + int tag; + float value; + CFNumberGetValue ((CFNumberRef) keys.arrayZ[i], kCFNumberIntType, &tag); + CFNumberGetValue ((CFNumberRef) values.arrayZ[i], kCFNumberFloatType, &value); - hb_variation_t var = {tag, value}; - vars.push (var); + hb_variation_t var = {tag, value}; + vars.push (var); + } + hb_font_set_variations (font, vars.arrayZ, vars.length); } - hb_font_set_variations (font, vars.arrayZ, vars.length); - -done: - CFRelease (variations); } /* Let there be dragons here... */ diff --git a/src/hb-cplusplus.hh b/src/hb-cplusplus.hh index 14944eeea..e94bab009 100644 --- a/src/hb-cplusplus.hh +++ b/src/hb-cplusplus.hh @@ -185,6 +185,13 @@ HB_DEFINE_VTABLE (vector_paint, nullptr); #endif + +#ifdef HB_GPU_H + +HB_DEFINE_VTABLE (gpu_draw, nullptr); + +#endif + #undef HB_DEFINE_VTABLE diff --git a/src/hb-draw.cc b/src/hb-draw.cc index 30ab86d33..044c39c0c 100644 --- a/src/hb-draw.cc +++ b/src/hb-draw.cc @@ -116,25 +116,25 @@ _hb_draw_funcs_set_middle (hb_draw_funcs_t *dfuncs, void *user_data, hb_destroy_func_t destroy) { + auto destroy_guard = hb_make_scope_guard ([&]() { + if (destroy) destroy (user_data); + }); + if (user_data && !dfuncs->user_data) { dfuncs->user_data = (decltype (dfuncs->user_data)) hb_calloc (1, sizeof (*dfuncs->user_data)); if (unlikely (!dfuncs->user_data)) - goto fail; + return false; } if (destroy && !dfuncs->destroy) { dfuncs->destroy = (decltype (dfuncs->destroy)) hb_calloc (1, sizeof (*dfuncs->destroy)); if (unlikely (!dfuncs->destroy)) - goto fail; + return false; } + destroy_guard.release (); return true; - -fail: - if (destroy) - (destroy) (user_data); - return false; } #define HB_DRAW_FUNC_IMPLEMENT(name) \ diff --git a/src/hb-font.cc b/src/hb-font.cc index fa8d329d3..e7c3980bb 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1003,25 +1003,25 @@ _hb_font_funcs_set_middle (hb_font_funcs_t *ffuncs, void *user_data, hb_destroy_func_t destroy) { + auto destroy_guard = hb_make_scope_guard ([&]() { + if (destroy) destroy (user_data); + }); + if (user_data && !ffuncs->user_data) { ffuncs->user_data = (decltype (ffuncs->user_data)) hb_calloc (1, sizeof (*ffuncs->user_data)); if (unlikely (!ffuncs->user_data)) - goto fail; + return false; } if (destroy && !ffuncs->destroy) { ffuncs->destroy = (decltype (ffuncs->destroy)) hb_calloc (1, sizeof (*ffuncs->destroy)); if (unlikely (!ffuncs->destroy)) - goto fail; + return false; } + destroy_guard.release (); return true; - -fail: - if (destroy) - (destroy) (user_data); - return false; } #define HB_FONT_FUNC_IMPLEMENT(get_,name) \ diff --git a/src/hb-ft.cc b/src/hb-ft.cc index a6a8cf2c5..5fef168c8 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -1117,14 +1117,13 @@ _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data buffer = (FT_Byte *) hb_malloc (length); if (!buffer) return nullptr; + auto buffer_guard = hb_make_scope_guard ([&]() { hb_free (buffer); }); error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length); if (error) - { - hb_free (buffer); return nullptr; - } + buffer_guard.release (); return hb_blob_create ((const char *) buffer, length, HB_MEMORY_MODE_WRITABLE, buffer, hb_free); diff --git a/src/hb-gdi.cc b/src/hb-gdi.cc index 8e7589bea..e3b611cfb 100644 --- a/src/hb-gdi.cc +++ b/src/hb-gdi.cc @@ -45,25 +45,25 @@ _hb_gdi_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_dat DWORD length = 0; HDC hdc = GetDC (nullptr); - if (unlikely (!SelectObject (hdc, (HFONT) user_data))) goto fail; + if (unlikely (!SelectObject (hdc, (HFONT) user_data))) + return hb_blob_get_empty (); + HB_SCOPE_GUARD (ReleaseDC (nullptr, hdc)); length = GetFontData (hdc, hb_uint32_swap (tag), 0, buffer, length); - if (unlikely (length == GDI_ERROR)) goto fail_with_releasedc; + if (unlikely (length == GDI_ERROR)) + return hb_blob_get_empty (); buffer = (char *) hb_malloc (length); - if (unlikely (!buffer)) goto fail_with_releasedc; + if (unlikely (!buffer)) + return hb_blob_get_empty (); + auto buffer_guard = hb_make_scope_guard ([&]() { hb_free (buffer); }); + length = GetFontData (hdc, hb_uint32_swap (tag), 0, buffer, length); - if (unlikely (length == GDI_ERROR)) goto fail_with_releasedc_and_free; - ReleaseDC (nullptr, hdc); + if (unlikely (length == GDI_ERROR)) + return hb_blob_get_empty (); + buffer_guard.release (); return hb_blob_create ((const char *) buffer, length, HB_MEMORY_MODE_WRITABLE, buffer, hb_free); - -fail_with_releasedc_and_free: - hb_free (buffer); -fail_with_releasedc: - ReleaseDC (nullptr, hdc); -fail: - return hb_blob_get_empty (); } /** diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index d5948e22e..f42f679df 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -897,7 +897,6 @@ hb_ot_draw_glyph_or_fail (hb_font_t *font, { const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; hb_draw_session_t draw_session {draw_funcs, draw_data}; - bool ret = false; OT::hb_scalar_cache_t *gvar_cache = nullptr; #ifndef HB_NO_VAR @@ -907,23 +906,20 @@ hb_ot_draw_glyph_or_fail (hb_font_t *font, gvar_cache = ot_font->draw.acquire_gvar_cache (*ot_font->ot_face->gvar); } #endif + HB_SCOPE_GUARD (ot_font->draw.release_gvar_cache (gvar_cache)); #ifndef HB_NO_VAR_COMPOSITES - if (font->face->table.VARC->get_path (font, glyph, draw_session)) { ret = true; goto done; } + if (font->face->table.VARC->get_path (font, glyph, draw_session)) return true; #endif // Keep the following in synch with VARC::get_path_at() - if (font->face->table.glyf->get_path (font, glyph, draw_session, gvar_cache)) { ret = true; goto done; } + if (font->face->table.glyf->get_path (font, glyph, draw_session, gvar_cache)) return true; #ifndef HB_NO_CFF - if (font->face->table.cff2->get_path (font, glyph, draw_session)) { ret = true; goto done; } - if (font->face->table.cff1->get_path (font, glyph, draw_session)) { ret = true; goto done; } + if (font->face->table.cff2->get_path (font, glyph, draw_session)) return true; + if (font->face->table.cff1->get_path (font, glyph, draw_session)) return true; #endif -done: - - ot_font->draw.release_gvar_cache (gvar_cache); - - return ret; + return false; } #endif diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 68fc0cade..4d65828cb 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -166,25 +166,25 @@ _hb_paint_funcs_set_middle (hb_paint_funcs_t *funcs, void *user_data, hb_destroy_func_t destroy) { + auto destroy_guard = hb_make_scope_guard ([&]() { + if (destroy) destroy (user_data); + }); + if (user_data && !funcs->user_data) { funcs->user_data = (decltype (funcs->user_data)) hb_calloc (1, sizeof (*funcs->user_data)); if (unlikely (!funcs->user_data)) - goto fail; + return false; } if (destroy && !funcs->destroy) { funcs->destroy = (decltype (funcs->destroy)) hb_calloc (1, sizeof (*funcs->destroy)); if (unlikely (!funcs->destroy)) - goto fail; + return false; } + destroy_guard.release (); return true; - -fail: - if (destroy) - (destroy) (user_data); - return false; } #define HB_PAINT_FUNC_IMPLEMENT(name) \ diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 70420e78d..4fbf58117 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -1282,6 +1282,13 @@ hb_raster_draw_render (hb_raster_draw_t *draw) ext.stride = (ext.width + 3u) & ~3u; /* ── 3. Allocate or reuse image ─────────────────────────────────── */ + /* Reset one-shot state on every exit path. */ + HB_SCOPE_GUARD ({ + draw->edges.clear (); + draw->has_extents = false; + draw->fixed_extents = {}; + }); + hb_raster_image_t *image; if (draw->recycled_image) { @@ -1291,15 +1298,12 @@ hb_raster_draw_render (hb_raster_draw_t *draw) else { image = hb_raster_image_create_or_fail (); - if (unlikely (!image)) goto fail; + if (unlikely (!image)) return nullptr; } + auto image_guard = hb_make_scope_guard ([&]() { hb_raster_image_destroy (image); }); if (unlikely (!image->configure (HB_RASTER_FORMAT_A8, ext))) - { - hb_raster_image_destroy (image); - image = nullptr; - goto fail; - } + return nullptr; image->clear (); /* ── 4. Bucket edges by starting row and rasterize scanlines ──── */ @@ -1307,7 +1311,7 @@ hb_raster_draw_render (hb_raster_draw_t *draw) { if (unlikely (!draw->row_area.resize_dirty (ext.width) || !draw->row_cover.resize_dirty (ext.width))) - goto fail; + return nullptr; hb_memset (draw->row_area.arrayZ, 0, ext.width * sizeof (int32_t)); hb_memset (draw->row_cover.arrayZ, 0, ext.width * sizeof (int16_t)); @@ -1317,7 +1321,7 @@ hb_raster_draw_render (hb_raster_draw_t *draw) if (ext.height > old_buckets) { if (unlikely (!draw->edge_buckets.resize (ext.height))) - goto fail; + return nullptr; } for (unsigned i = 0; i < hb_min (ext.height, old_buckets); i++) draw->edge_buckets.arrayZ[i].clear (); @@ -1380,16 +1384,6 @@ hb_raster_draw_render (hb_raster_draw_t *draw) } } -done: - /* ── 6. Reset one-shot state ────────────────────────────────────── */ - draw->edges.clear (); - draw->has_extents = false; - draw->fixed_extents = {}; - + image_guard.release (); return image; - -fail: - hb_raster_image_destroy (image); - image = nullptr; - goto done; } diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 021766540..467955b66 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -1113,7 +1113,7 @@ hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, /* Inverse transform: pixel → glyph space */ hb_transform_t<> t = c->current_effective_transform (); float det = t.xx * t.yy - t.xy * t.yx; - if (fabsf (det) < 1e-10f) goto done; + if (fabsf (det) < 1e-10f) return; { float inv_det = 1.f / det; @@ -1127,7 +1127,7 @@ hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, /* Gradient direction vector and denominator for projection */ float dx = gx1 - gx0, dy = gy1 - gy0; float denom = dx * dx + dy * dy; - if (denom < 1e-10f) goto done; + if (denom < 1e-10f) return; float inv_denom = 1.f / denom; unsigned stride = surf->extents.stride; @@ -1215,8 +1215,6 @@ hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, } } -done: - (void) stops_; } static void @@ -1263,7 +1261,7 @@ hb_raster_paint_radial_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, /* Inverse transform */ hb_transform_t<> t = c->current_effective_transform (); float det = t.xx * t.yy - t.xy * t.yx; - if (fabsf (det) < 1e-10f) goto done; + if (fabsf (det) < 1e-10f) return; { float inv_det = 1.f / det; @@ -1494,8 +1492,6 @@ hb_raster_paint_radial_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, } } -done: - (void) stops_; } static void @@ -1540,7 +1536,7 @@ hb_raster_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, /* Inverse transform */ hb_transform_t<> t = c->current_effective_transform (); float det = t.xx * t.yy - t.xy * t.yx; - if (fabsf (det) < 1e-10f || fabsf (angle_range) < 1e-10f) goto done; + if (fabsf (det) < 1e-10f || fabsf (angle_range) < 1e-10f) return; { float inv_det = 1.f / det; @@ -1646,8 +1642,6 @@ hb_raster_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, } } -done: - (void) stops_; } static hb_bool_t @@ -2207,11 +2201,23 @@ hb_raster_paint_glyph (hb_raster_paint_t *paint, hb_raster_image_t * hb_raster_paint_render (hb_raster_paint_t *paint) { - hb_raster_image_t *result = nullptr; + /* Common per-exit state reset. Runs on every return path, + * including the early-return failure cases below. */ + HB_SCOPE_GUARD ({ + paint->transform_stack.clear (); + paint->release_all_clips (); + for (auto *s : paint->surface_stack) + paint->release_surface (s); + paint->surface_stack.clear (); + hb_raster_draw_reset (paint->clip_rdr); + paint->has_extents = false; + paint->fixed_extents = {}; + }); if (unlikely (!paint->has_extents)) - goto fail; + return nullptr; + hb_raster_image_t *result; if (paint->surface_stack.length) { result = paint->surface_stack[0]; @@ -2225,28 +2231,10 @@ hb_raster_paint_render (hb_raster_paint_t *paint) { result = paint->acquire_surface (); if (unlikely (!result)) - goto fail; + return nullptr; } - /* Clean up stacks and reset auto-extents for next glyph. */ - paint->transform_stack.clear (); - paint->release_all_clips (); - hb_raster_draw_reset (paint->clip_rdr); - paint->has_extents = false; - paint->fixed_extents = {}; - return result; - -fail: - paint->transform_stack.clear (); - paint->release_all_clips (); - for (auto *s : paint->surface_stack) - paint->release_surface (s); - paint->surface_stack.clear (); - hb_raster_draw_reset (paint->clip_rdr); - paint->has_extents = false; - paint->fixed_extents = {}; - return nullptr; } /** diff --git a/src/hb-unicode.cc b/src/hb-unicode.cc index aa2735bed..a157052fd 100644 --- a/src/hb-unicode.cc +++ b/src/hb-unicode.cc @@ -376,8 +376,12 @@ hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t *ufuncs, \ void *user_data, \ hb_destroy_func_t destroy) \ { \ + auto destroy_guard = hb_make_scope_guard ([&]() { \ + if (destroy) destroy (user_data); \ + }); \ + \ if (hb_object_is_immutable (ufuncs)) \ - goto fail; \ + return; \ \ if (!func) \ { \ @@ -396,11 +400,7 @@ hb_unicode_funcs_set_##name##_func (hb_unicode_funcs_t *ufuncs, \ ufuncs->func.name = ufuncs->parent->func.name; \ ufuncs->user_data.name = user_data; \ ufuncs->destroy.name = destroy; \ - return; \ - \ -fail: \ - if (destroy) \ - destroy (user_data); \ + destroy_guard.release (); \ } HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS diff --git a/src/hb-uniscribe.cc b/src/hb-uniscribe.cc index 1b8ac367e..057b5236b 100644 --- a/src/hb-uniscribe.cc +++ b/src/hb-uniscribe.cc @@ -333,6 +333,7 @@ _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) */ blob = hb_sanitize_context_t ().sanitize_blob (blob); + HB_SCOPE_GUARD (hb_blob_destroy (blob)); unsigned int length, new_length, name_str_len; const char *orig_sfnt_data = hb_blob_get_data (blob, &length); @@ -350,10 +351,8 @@ _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) new_length = name_table_offset + padded_name_table_length; void *new_sfnt_data = hb_calloc (1, new_length); if (!new_sfnt_data) - { - hb_blob_destroy (blob); return nullptr; - } + auto sfnt_guard = hb_make_scope_guard ([&]() { hb_free (new_sfnt_data); }); hb_memcpy(new_sfnt_data, orig_sfnt_data, length); @@ -397,18 +396,14 @@ _hb_rename_font (hb_blob_t *blob, wchar_t *new_name) record.length = name_table_length; } else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */ - { - hb_free (new_sfnt_data); - hb_blob_destroy (blob); return nullptr; - } } /* The checkSumAdjustment field in the 'head' table is now wrong, * but that doesn't actually seem to cause any problems so we don't * bother. */ - hb_blob_destroy (blob); + sfnt_guard.release (); return hb_blob_create ((const char *) new_sfnt_data, new_length, HB_MEMORY_MODE_WRITABLE, new_sfnt_data, hb_free); } diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index d6236c914..8c2e8cdb8 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -481,6 +481,7 @@ hb_pdf_build_indexed_smask (hb_vector_t *out, unsigned raw_len = (width + 1) * height; /* 1 filter byte per row + width bytes */ uint8_t *raw = (uint8_t *) hb_malloc (raw_len); if (!raw) return false; + HB_SCOPE_GUARD (hb_free (raw)); z_stream stream = {}; stream.next_in = (Bytef *) idat_data; @@ -489,38 +490,25 @@ hb_pdf_build_indexed_smask (hb_vector_t *out, stream.avail_out = raw_len; if (inflateInit (&stream) != Z_OK) - { - hb_free (raw); return false; - } int status = inflate (&stream, Z_FINISH); inflateEnd (&stream); if (status != Z_STREAM_END) - { - hb_free (raw); return false; - } /* Un-filter and map to alpha. */ if (!out->resize (width * height)) - { - hb_free (raw); return false; - } uint8_t *unfiltered = (uint8_t *) hb_malloc (width); if (!unfiltered) - { - hb_free (raw); return false; - } + HB_SCOPE_GUARD (hb_free (unfiltered)); + uint8_t *prev_unfiltered = (uint8_t *) hb_calloc (width, 1); if (!prev_unfiltered) - { - hb_free (unfiltered); - hb_free (raw); return false; - } + HB_SCOPE_GUARD (hb_free (prev_unfiltered)); for (unsigned y = 0; y < height; y++) { @@ -565,9 +553,6 @@ hb_pdf_build_indexed_smask (hb_vector_t *out, unfiltered = tmp; } - hb_free (unfiltered); - hb_free (prev_unfiltered); - hb_free (raw); return true; #endif } diff --git a/src/hb-wasm-shape.cc b/src/hb-wasm-shape.cc index f83329e1c..923b4cd68 100644 --- a/src/hb-wasm-shape.cc +++ b/src/hb-wasm-shape.cc @@ -169,40 +169,37 @@ _hb_wasm_shaper_face_data_create (hb_face_t *face) { char error[128]; hb_wasm_face_data_t *data = nullptr; - hb_blob_t *wasm_blob = nullptr; wasm_module_t wasm_module = nullptr; - wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); + hb_blob_t *wasm_blob = hb_face_reference_table (face, HB_WASM_TAG_WASM); + auto blob_guard = hb_make_scope_guard ([&]() { hb_blob_destroy (wasm_blob); }); + unsigned length = hb_blob_get_length (wasm_blob); if (!length) - goto fail; + return nullptr; if (!_hb_wasm_init ()) - goto fail; + return nullptr; wasm_module = wasm_runtime_load ((uint8_t *) hb_blob_get_data_writable (wasm_blob, nullptr), length, error, sizeof (error)); if (unlikely (!wasm_module)) { DEBUG_MSG (WASM, nullptr, "Load wasm module failed: %s", error); - goto fail; + return nullptr; } + auto module_guard = hb_make_scope_guard ([&]() { wasm_runtime_unload (wasm_module); }); data = (hb_wasm_face_data_t *) hb_calloc (1, sizeof (hb_wasm_face_data_t)); if (unlikely (!data)) - goto fail; + return nullptr; data->wasm_blob = wasm_blob; data->wasm_module = wasm_module; + blob_guard.release (); + module_guard.release (); return data; - -fail: - if (wasm_module) - wasm_runtime_unload (wasm_module); - hb_blob_destroy (wasm_blob); - hb_free (data); - return nullptr; } static hb_wasm_shape_plan_t * @@ -217,30 +214,27 @@ acquire_shape_plan (hb_face_t *face, return plan; plan = (hb_wasm_shape_plan_t *) hb_calloc (1, sizeof (hb_wasm_shape_plan_t)); - - wasm_module_inst_t module_inst = nullptr; - wasm_exec_env_t exec_env = nullptr; - wasm_function_inst_t func = nullptr; + auto plan_guard = hb_make_scope_guard ([&]() { hb_free (plan); }); constexpr uint32_t stack_size = 32 * 1024, heap_size = 2 * 1024 * 1024; - module_inst = plan->module_inst = wasm_runtime_instantiate (face_data->wasm_module, - stack_size, heap_size, - error, sizeof (error)); + wasm_module_inst_t module_inst = plan->module_inst = wasm_runtime_instantiate ( + face_data->wasm_module, stack_size, heap_size, error, sizeof (error)); if (unlikely (!module_inst)) { DEBUG_MSG (WASM, face_data, "Create wasm module instance failed: %s", error); - goto fail; + return nullptr; } + auto module_guard = hb_make_scope_guard ([&]() { wasm_runtime_deinstantiate (module_inst); }); - exec_env = plan->exec_env = wasm_runtime_create_exec_env (module_inst, - stack_size); + wasm_exec_env_t exec_env = plan->exec_env = wasm_runtime_create_exec_env (module_inst, stack_size); if (unlikely (!exec_env)) { DEBUG_MSG (WASM, face_data, "Create wasm execution environment failed."); - goto fail; + return nullptr; } + auto exec_guard = hb_make_scope_guard ([&]() { wasm_runtime_destroy_exec_env (exec_env); }); - func = wasm_runtime_lookup_function (module_inst, "shape_plan_create"); + wasm_function_inst_t func = wasm_runtime_lookup_function (module_inst, "shape_plan_create"); if (func) { wasm_val_t results[1]; @@ -250,7 +244,7 @@ acquire_shape_plan (hb_face_t *face, if (unlikely (!faceref)) { DEBUG_MSG (WASM, face_data, "Failed to register face object."); - goto fail; + return nullptr; } results[0].kind = WASM_I32; @@ -264,21 +258,15 @@ acquire_shape_plan (hb_face_t *face, { DEBUG_MSG (WASM, module_inst, "Calling shape_plan_create() failed: %s", wasm_runtime_get_exception (module_inst)); - goto fail; + return nullptr; } plan->wasm_shape_planptr = results[0].of.i32; } + plan_guard.release (); + module_guard.release (); + exec_guard.release (); return plan; - -fail: - - if (exec_env) - wasm_runtime_destroy_exec_env (exec_env); - if (module_inst) - wasm_runtime_deinstantiate (module_inst); - hb_free (plan); - return nullptr; } static void diff --git a/src/hb-zlib.cc b/src/hb-zlib.cc index 5c8eff0f1..f17c5d6dd 100644 --- a/src/hb-zlib.cc +++ b/src/hb-zlib.cc @@ -28,6 +28,7 @@ hb_blob_decompress_gzip (hb_blob_t *blob, if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) return nullptr; + HB_SCOPE_GUARD (inflateEnd (&stream)); uint32_t expected_size = 0; hb_gzip_get_uncompressed_size ((const char *) compressed, @@ -38,27 +39,25 @@ hb_blob_decompress_gzip (hb_blob_t *blob, (size_t) max_output_len); char *output = (char *) hb_malloc (allocated); if (!output) - { - inflateEnd (&stream); return nullptr; - } + auto output_guard = hb_make_scope_guard ([&]() { hb_free (output); }); int status = Z_OK; while (true) { size_t produced = (size_t) stream.total_out; if (unlikely (produced >= (size_t) max_output_len)) - goto fail; + return nullptr; if (produced == allocated) { size_t new_allocated = hb_min (allocated * 2, (size_t) max_output_len); if (unlikely (new_allocated <= allocated)) - goto fail; + return nullptr; char *new_output = (char *) hb_realloc (output, new_allocated); if (unlikely (!new_output)) - goto fail; + return nullptr; output = new_output; allocated = new_allocated; @@ -74,25 +73,18 @@ hb_blob_decompress_gzip (hb_blob_t *blob, if ((status == Z_OK || status == Z_BUF_ERROR) && stream.avail_out == 0) continue; - goto fail; + return nullptr; } - inflateEnd (&stream); - if (unlikely ((size_t) stream.total_out > (size_t) max_output_len)) - goto fail_free; + return nullptr; + output_guard.release (); return hb_blob_create_or_fail (output, (unsigned) stream.total_out, HB_MEMORY_MODE_WRITABLE, output, hb_free); - -fail: - inflateEnd (&stream); -fail_free: - hb_free (output); - return nullptr; #endif } diff --git a/src/hb.hh b/src/hb.hh index 4a21ca90b..7e2837f99 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -571,6 +571,13 @@ extern "C" void hb_free_impl(void *ptr); #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector +/* Library-internal alias for hb::unique_ptr defined in hb-cplusplus.hh. + * Unique-ownership RAII wrapper around an HB object; destroys on scope exit + * via the type's hb_*_destroy() function. Usage: hb_unique_ptr_t. + * Matches the hb_*_t naming used elsewhere in internal code. */ +template using hb_unique_ptr_t = hb::unique_ptr; + + /* Our src/test-*.cc use hb_assert(), such that it's not compiled out under NDEBUG. * https://github.com/harfbuzz/harfbuzz/issues/5418 */ #define hb_always_assert(x) \