From 64fe92bf2d43a0ea31743d774e073f202021dbd1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 21 Jan 2017 15:36:35 -0800 Subject: [PATCH] [var] Remove use of variable-length arrays --- src/hb-font.cc | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/hb-font.cc b/src/hb-font.cc index 1b4ed7e24..39d176914 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1540,6 +1540,17 @@ hb_font_get_ppem (hb_font_t *font, * Variations */ +static void +_hb_font_adopt_var_coords_normalized (hb_font_t *font, + int *coords, /* 2.14 normalized */ + unsigned int coords_length) +{ + free (font->coords); + + font->coords = coords; + font->num_coords = coords_length; +} + void hb_font_set_var_coords (hb_font_t *font, const hb_var_coord_t *coords, @@ -1557,10 +1568,12 @@ hb_font_set_var_coords (hb_font_t *font, hb_face_t *face = font->face; unsigned int length = hb_ot_var_get_axis_count (face); - int normalized[length]; // XXX Remove variable-length array use... - memset (normalized, 0, length * sizeof (normalized[0])); + int *normalized = length ? (int *) calloc (length, sizeof (int)) : NULL; + if (unlikely (length && !normalized)) + return; + /* normalized is filled with zero already. */ for (unsigned int i = 0; i < coords_length; i++) { unsigned int axis_index; @@ -1568,7 +1581,7 @@ hb_font_set_var_coords (hb_font_t *font, normalized[axis_index] = hb_ot_var_normalize_axis_value (face, axis_index, coords[i].value); } - hb_font_set_var_coords_normalized (font, normalized, coords_length); + _hb_font_adopt_var_coords_normalized (font, normalized, coords_length); } void @@ -1579,13 +1592,15 @@ hb_font_set_var_coords_design (hb_font_t *font, if (font->immutable) return; - int normalized[coords_length]; // XXX Remove variable-length array use... + int *normalized = coords_length ? (int *) calloc (coords_length, sizeof (int)) : NULL; + if (unlikely (coords_length && !normalized)) + return; hb_face_t *face = font->face; for (unsigned int i = 0; i < coords_length; i++) normalized[i] = hb_ot_var_normalize_axis_value (face, i, coords[i]); - hb_font_set_var_coords_normalized (font, normalized, coords_length); + _hb_font_adopt_var_coords_normalized (font, normalized, coords_length); } void @@ -1603,10 +1618,7 @@ hb_font_set_var_coords_normalized (hb_font_t *font, if (coords_length) memcpy (copy, coords, coords_length * sizeof (coords[0])); - free (font->coords); - - font->coords = copy; - font->num_coords = coords_length; + _hb_font_adopt_var_coords_normalized (font, copy, coords_length); } int *