From 4260de12c1bb271033d9cd3b0f47a4cf77190bf3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 25 Feb 2023 10:43:27 -0700 Subject: [PATCH] [wasm] Add HB_ARRAY_APP2NATIVE --- src/hb-wasm-api-buffer.hh | 7 +++---- src/hb-wasm-api-face.hh | 2 +- src/hb-wasm-api-font.hh | 4 ++-- src/hb-wasm-api.hh | 5 +++++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 1a1057373..fd0d1dd95 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -113,10 +113,8 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV if (length <= contents->length) { - unsigned bytes = length * sizeof (hb_glyph_info_t); - - glyph_info_t *info = (glyph_info_t *) (validate_app_addr (contents->info, bytes) ? addr_app_to_native (contents->info) : nullptr); - glyph_position_t *pos = (glyph_position_t *) (validate_app_addr (contents->pos, bytes) ? addr_app_to_native (contents->pos) : nullptr); + glyph_info_t *info = HB_ARRAY_APP2NATIVE (glyph_info_t, contents->info, length); + glyph_position_t *pos = HB_ARRAY_APP2NATIVE (glyph_position_t, contents->pos, length); if (unlikely (!info || !pos)) { @@ -124,6 +122,7 @@ HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV return false; } + unsigned bytes = length * sizeof (hb_glyph_info_t); memcpy (info, buffer->info, bytes); memcpy (pos, buffer->pos, bytes); diff --git a/src/hb-wasm-api-face.hh b/src/hb-wasm-api-face.hh index 9ef6476a7..e528e1f8c 100644 --- a/src/hb-wasm-api-face.hh +++ b/src/hb-wasm-api-face.hh @@ -48,7 +48,7 @@ HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV if (length <= blob->length) { - char *data = (char *) (validate_app_addr (blob->data, length) ? addr_app_to_native (blob->data) : nullptr); + char *data = HB_ARRAY_APP2NATIVE (char, blob->data, length); if (unlikely (!data)) { diff --git a/src/hb-wasm-api-font.hh b/src/hb-wasm-api-font.hh index 10f1a30c2..2a5425cd0 100644 --- a/src/hb-wasm-api-font.hh +++ b/src/hb-wasm-api-font.hh @@ -140,8 +140,8 @@ HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV if (hb_outline.points.length <= outline->n_points && hb_outline.contours.length <= outline->n_contours) { - glyph_outline_point_t *points = (glyph_outline_point_t *) (validate_app_addr (outline->points, hb_outline.points.get_size ()) ? addr_app_to_native (outline->points) : nullptr); - uint32_t *contours = (uint32_t *) (validate_app_addr (outline->contours, hb_outline.contours.get_size ()) ? addr_app_to_native (outline->contours) : nullptr); + glyph_outline_point_t *points = HB_ARRAY_APP2NATIVE (glyph_outline_point_t, outline->points, hb_outline.points.length); + uint32_t *contours = HB_ARRAY_APP2NATIVE (uint32_t, outline->contours, hb_outline.contours.length); if (unlikely (!points || !contours)) { diff --git a/src/hb-wasm-api.hh b/src/hb-wasm-api.hh index f7e5565be..4ead01c1f 100644 --- a/src/hb-wasm-api.hh +++ b/src/hb-wasm-api.hh @@ -108,5 +108,10 @@ HB_INTERNAL extern hb_user_data_key_t _hb_wasm_ref_type_key; name = (type *) wasm_runtime_addr_app_to_native (module_inst, name##ptr); \ } HB_STMT_END +#define HB_ARRAY_APP2NATIVE(type, name, length) \ + ((type *) (!hb_unsigned_mul_overflows (length, sizeof (type)) && \ + validate_app_addr (name, (length) * sizeof (type)) ? \ + addr_app_to_native (name) : nullptr)) + #endif /* HB_WASM_API_HH */