From 16ecb96922e2f1389cd634a2b908df0a72f8ac1f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 19:53:47 -0700 Subject: [PATCH] [wasm-api] Return success from buffer_contents_realloc --- src/hb-wasm-api-buffer.hh | 38 ++++++++++++++++++++++++++------------ src/hb-wasm-api-list.hh | 2 +- src/hb-wasm-api.h | 6 +++--- src/wasm-graphite/shape.cc | 3 ++- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index a89fc3968..78d44b572 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -35,35 +35,49 @@ namespace wasm { static_assert (sizeof (glyph_info_t) == sizeof (hb_glyph_info_t), ""); static_assert (sizeof (glyph_position_t) == sizeof (hb_glyph_position_t), ""); -HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents), - uint32_t size) +HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents), + uint32_t size) { HB_PTR_PARAM (buffer_contents_t, contents); if (unlikely (!contents)) - return; + return false; if (size <= contents->length) - return; + return true; unsigned bytes; if (hb_unsigned_mul_overflows (size, sizeof (glyph_info_t), &bytes)) - return; + return false; // TODO bounds check? uint32_t infoptr = contents->info; uint32_t posptr = contents->pos; + const char *info = (const char *) addr_app_to_native (infoptr); const char *pos = (const char *) addr_app_to_native (posptr); - contents->info = wasm_runtime_module_dup_data (module_inst, info, bytes); - contents->pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); + uint32_t new_info = wasm_runtime_module_dup_data (module_inst, info, bytes); + uint32_t new_pos = wasm_runtime_module_dup_data (module_inst, pos, bytes); - module_free (infoptr); - module_free (posptr); + if (likely (new_info)) + { + contents->info = new_info; + module_free (infoptr); + } + if (likely (new_pos)) + { + contents->pos = new_pos; + module_free (posptr); + } - // TODO Check success - contents->length = size; + if (likely (new_info && new_pos)) + { + contents->length = size; + return true; + } + + return false; } HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 8d05f2914..14ba384fa 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -58,7 +58,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = /* buffer */ NATIVE_SYMBOL ("(i)", buffer_contents_free), - NATIVE_SYMBOL ("(ii)", buffer_contents_realloc), + NATIVE_SYMBOL ("(ii)i", buffer_contents_realloc), NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index 515883566..ea3aa6190 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -140,9 +140,9 @@ typedef struct ptr_t(glyph_position_t) pos; } buffer_contents_t; -HB_WASM_API (void, buffer_contents_realloc) (HB_WASM_EXEC_ENV - ptr_d(buffer_contents_t, contents), - uint32_t size); +HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV + ptr_d(buffer_contents_t, contents), + uint32_t size); HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV ptr_d(buffer_contents_t, contents)); diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index 24b1b5fe1..80ef1c500 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -113,7 +113,8 @@ shape (void *shape_plan, }; length = glyph_count; - buffer_contents_realloc (&contents, length); + if (!buffer_contents_realloc (&contents, length)) + return false; cluster_t *clusters = (cluster_t *) malloc (length * sizeof (cluster_t)); uint32_t *gids = (uint32_t *) malloc (length * sizeof (uint32_t)); if (!clusters || !gids)