[wasm-api] Return success from buffer_contents_realloc

This commit is contained in:
Behdad Esfahbod 2023-02-24 19:53:47 -07:00
parent 2568890d15
commit 16ecb96922
4 changed files with 32 additions and 17 deletions

View File

@ -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

View File

@ -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),

View File

@ -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));

View File

@ -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)