- glyphlen = gr_seg_n_slots (seg);
- clusters = (hb_graphite2_cluster_t *) calloc (charlen, sizeof (hb_graphite2_cluster_t));
- if (!glyphlen || !clusters) goto dieout;
+ if (unlikely (!seg)) {
+ if (feats) gr_featureval_destroy (feats);
+ return false;
+ }
+
+ unsigned int glyph_count = gr_seg_n_slots (seg);
+ if (unlikely (!glyph_count)) {
+ if (feats) gr_featureval_destroy (feats);
+ gr_seg_destroy (seg);
+ return false;
+ }
+
+ scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
+ while ((sizeof (hb_graphite2_cluster_t) * buffer->len +
+ sizeof (hb_codepoint_t) * glyph_count) > scratch_size)
+ {
+ buffer->ensure (buffer->allocated * 2);
+ if (unlikely (buffer->in_error)) {
+ if (feats) gr_featureval_destroy (feats);
+ gr_seg_destroy (seg);
+ return false;
+ }
+ scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
+ }
+
+ ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len);
+ ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count);