[serialize] Streamline error propagation
diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 0ac88c0..8a3170c 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh
@@ -137,6 +137,8 @@ /* Methods */ + bool in_error () const { return !successful; } + void allocate_var (unsigned int start, unsigned int count) { #ifndef HB_NDEBUG
diff --git a/src/hb-machinery.hh b/src/hb-machinery.hh index a3bd3be..c02032c 100644 --- a/src/hb-machinery.hh +++ b/src/hb-machinery.hh
@@ -508,14 +508,31 @@ reset (); } + bool in_error () const { return !this->successful; } + void reset () { - this->ran_out_of_room = false; + this->successful = true; this->head = this->start; this->debug_depth = 0; } - bool err (bool e) { return this->ran_out_of_room = this->ran_out_of_room || e; } + bool propagate_error (bool e) + { return this->successful = this->successful && e; } + template <typename T> bool propagate_error (const T &obj) + { return this->successful = this->successful && !obj.in_error (); } + template <typename T> bool propagate_error (const T *obj) + { return this->successful = this->successful && !obj->in_error (); } + template <typename T1, typename T2> bool propagate_error (T1 &o1, T2 &o2) + { return propagate_error (o1) && propagate_error (o2); } + template <typename T1, typename T2> bool propagate_error (T1 *o1, T2 *o2) + { return propagate_error (o1) && propagate_error (o2); } + template <typename T1, typename T2, typename T3> + bool propagate_error (T1 &o1, T2 &o2, T3 &o3) + { return propagate_error (o1) && propagate_error (o2, o3); } + template <typename T1, typename T2, typename T3> + bool propagate_error (T1 *o1, T2 *o2, T3 *o3) + { return propagate_error (o1) && propagate_error (o2, o3); } /* To be called around main operation. */ template <typename Type> @@ -534,7 +551,7 @@ "end [%p..%p] serialized %d bytes; %s", this->start, this->end, (int) (this->head - this->start), - this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room"); + this->successful ? "successful" : "UNSUCCESSFUL"); } unsigned int length () const { return this->head - this->start; } @@ -556,8 +573,8 @@ template <typename Type> Type *allocate_size (unsigned int size) { - if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { - this->ran_out_of_room = true; + if (unlikely (!this->successful || this->end - this->head < ptrdiff_t (size))) { + this->successful = false; return nullptr; } memset (this->head, 0, size); @@ -602,7 +619,7 @@ template <typename Type> Type *copy () const { - assert (!this->ran_out_of_room); + assert (this->successful); unsigned int len = this->head - this->start; void *p = malloc (len); if (p) @@ -611,7 +628,7 @@ } hb_bytes_t copy_bytes () const { - assert (!this->ran_out_of_room); + assert (this->successful); unsigned int len = this->head - this->start; void *p = malloc (len); if (p) @@ -622,7 +639,7 @@ } hb_blob_t *copy_blob () const { - assert (!this->ran_out_of_room); + assert (this->successful); return hb_blob_create (this->start, this->head - this->start, HB_MEMORY_MODE_DUPLICATE, @@ -632,7 +649,7 @@ public: unsigned int debug_depth; char *start, *end, *head; - bool ran_out_of_room; + bool successful; };
diff --git a/src/hb-map.hh b/src/hb-map.hh index ec8de2a..3b119da 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh
@@ -90,6 +90,8 @@ fini_shallow (); } + bool in_error () const { return !successful; } + bool resize () { if (unlikely (!successful)) return false;
diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index d609918..ac73920 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh
@@ -1254,7 +1254,7 @@ glyphs.push()->set (glyph_map[g]); klasses.push()->set (value); } - c->serializer->err (glyphs.in_error () || klasses.in_error ()); + c->serializer->propagate_error (glyphs, klasses); hb_supplier_t<GlyphID> glyphs_supplier (glyphs); hb_supplier_t<HBUINT16> klasses_supplier (klasses); @@ -1413,7 +1413,7 @@ klasses.push ()->set (value); } } - c->serializer->err (glyphs.in_error () || klasses.in_error ()); + c->serializer->propagate_error (glyphs, klasses); hb_supplier_t<GlyphID> glyphs_supplier (glyphs); hb_supplier_t<HBUINT16> klasses_supplier (klasses);
diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index d6e9e9d..fb59921 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh
@@ -120,7 +120,7 @@ from.push ()->set (glyph_map[iter.get_glyph ()]); to.push ()->set (glyph_map[(iter.get_glyph () + delta) & 0xFFFF]); } - c->serializer->err (from.in_error () || to.in_error ()); + c->serializer->propagate_error (from, to); hb_supplier_t<GlyphID> from_supplier (from); hb_supplier_t<GlyphID> to_supplier (to); @@ -225,7 +225,7 @@ from.push ()->set (glyph_map[iter.get_glyph ()]); to.push ()->set (glyph_map[substitute[iter.get_coverage ()]]); } - c->serializer->err (from.in_error () || to.in_error ()); + c->serializer->propagate_error (from, to); hb_supplier_t<GlyphID> from_supplier (from); hb_supplier_t<GlyphID> to_supplier (to);
diff --git a/src/hb-set.hh b/src/hb-set.hh index eb325a6..1959688 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh
@@ -213,6 +213,8 @@ fini_shallow (); } + bool in_error () const { return !successful; } + bool resize (unsigned int count) { if (unlikely (!successful)) return false;
diff --git a/src/hb-subset.cc b/src/hb-subset.cc index 9ce0a72..37e7cec 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc
@@ -83,7 +83,7 @@ hb_serialize_context_t serializer ((void *) buf, buf_size); hb_subset_context_t c (plan, &serializer); result = table->subset (&c); - if (serializer.ran_out_of_room) + if (serializer.in_error ()) { buf_size += (buf_size >> 1) + 32; DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);