Simplify sanitize->check_array()
authorBehdad Esfahbod <behdad@behdad.org>
Mon, 10 Sep 2018 21:18:07 +0000 (23:18 +0200)
committerBehdad Esfahbod <behdad@behdad.org>
Mon, 10 Sep 2018 21:18:23 +0000 (23:18 +0200)
Fix a bug in CBDT sanitize, and redundant check in avar.

15 files changed:
src/hb-aat-layout-common.hh
src/hb-aat-layout-morx-table.hh
src/hb-machinery.hh
src/hb-open-type.hh
src/hb-ot-color-cbdt-table.hh
src/hb-ot-kern-table.hh
src/hb-ot-layout-common.hh
src/hb-ot-layout-gpos-table.hh
src/hb-ot-layout-gsubgpos.hh
src/hb-ot-math-table.hh
src/hb-ot-name-table.hh
src/hb-ot-var-avar-table.hh
src/hb-ot-var-fvar-table.hh
src/hb-ot-var-hvar-table.hh
src/hb-ot-var-mvar-table.hh

index 25f4fe1..85c40a7 100644 (file)
@@ -129,7 +129,7 @@ struct BinSearchArrayOf
     TRACE_SANITIZE (this);
     return_trace (header.sanitize (c) &&
                  Type::static_size >= header.unitSize &&
-                 c->check_array (bytesZ, header.unitSize, header.nUnits));
+                 c->check_array (bytesZ, header.nUnits, header.unitSize));
   }
 
   protected:
@@ -480,8 +480,8 @@ struct StateTable
     while (state < num_states)
     {
       if (unlikely (!c->check_array (states,
-                                    states[0].static_size * nClasses,
-                                    num_states)))
+                                    num_states,
+                                    states[0].static_size * nClasses)))
        return_trace (false);
       { /* Sweep new states. */
        const HBUINT16 *stop = &states[num_states * nClasses];
@@ -490,9 +490,7 @@ struct StateTable
        state = num_states;
       }
 
-      if (unlikely (!c->check_array (entries,
-                                    entries[0].static_size,
-                                    num_entries)))
+      if (unlikely (!c->check_array (entries, num_entries)))
        return_trace (false);
       { /* Sweep new entries. */
        const Entry<Extra> *stop = &entries[num_entries];
index ef452e2..6f7bf77 100644 (file)
@@ -648,7 +648,7 @@ struct Chain
        !c->check_range (this, length))
       return_trace (false);
 
-    if (!c->check_array (featureZ, featureZ[0].static_size, featureCount))
+    if (!c->check_array (featureZ, featureCount))
       return_trace (false);
 
     const ChainSubtable *subtable = &StructAtOffset<ChainSubtable> (featureZ, featureZ[0].static_size * featureCount);
index 62f4907..f80cfdb 100644 (file)
@@ -296,7 +296,8 @@ struct hb_sanitize_context_t :
     return likely (ok);
   }
 
-  inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
+  template <typename T>
+  inline bool check_array (const T *base, unsigned int len, unsigned int record_size = T::static_size) const
   {
     const char *p = (const char *) base;
     bool overflows = hb_unsigned_mul_overflows (len, record_size);
index ef18163..49e52c6 100644 (file)
@@ -345,7 +345,7 @@ struct UnsizedArrayOf
   inline bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const
   {
     TRACE_SANITIZE (this);
-    return_trace (c->check_array (arrayZ, arrayZ[0].static_size, count));
+    return_trace (c->check_array (arrayZ, count));
   }
 
   public:
@@ -487,7 +487,7 @@ struct ArrayOf
   inline bool sanitize_shallow (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
-    return_trace (len.sanitize (c) && c->check_array (arrayZ, Type::static_size, len));
+    return_trace (len.sanitize (c) && c->check_array (arrayZ, len));
   }
 
   public:
@@ -596,7 +596,7 @@ struct HeadlessArrayOf
   {
     TRACE_SANITIZE (this);
     return_trace (len.sanitize (c) &&
-                 (!len || c->check_array (arrayZ, Type::static_size, len - 1)));
+                 (!len || c->check_array (arrayZ, len - 1)));
   }
 
   public:
index 8bedf22..a563697 100644 (file)
@@ -128,7 +128,7 @@ struct IndexSubtableFormat1Or3
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this) &&
-                 c->check_array (offsetArrayZ, offsetArrayZ[0].static_size, glyph_count + 1));
+                 c->check_array (offsetArrayZ, glyph_count + 1));
   }
 
   bool get_image_data (unsigned int idx,
@@ -240,7 +240,7 @@ struct IndexSubtableArray
   inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
   {
     TRACE_SANITIZE (this);
-    if (unlikely (!c->check_array (&indexSubtablesZ, indexSubtablesZ[0].static_size, count)))
+    if (unlikely (!c->check_array (indexSubtablesZ, count)))
       return_trace (false);
     for (unsigned int i = 0; i < count; i++)
       if (unlikely (!indexSubtablesZ[i].sanitize (c, this)))
index 40f94be..e9f109c 100644 (file)
@@ -208,7 +208,7 @@ struct KernSubTableWrapper
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (thiz()) &&
                  thiz()->length >= T::min_size &&
-                 c->check_array (thiz(), 1, thiz()->length) &&
+                 c->check_array (thiz(), thiz()->length, 1) &&
                  thiz()->subtable.sanitize (c, thiz()->format));
   }
 };
index dd65f33..e469ff8 100644 (file)
@@ -1542,7 +1542,7 @@ struct VarData
                  regionIndices.sanitize(c) &&
                  shortCount <= regionIndices.len &&
                  c->check_array (&StructAfter<HBUINT8> (regionIndices),
-                                 get_row_size (), itemCount));
+                                 itemCount, get_row_size ()));
   }
 
   protected:
index 5847306..b3651f9 100644 (file)
@@ -199,7 +199,7 @@ struct ValueFormat : HBUINT16
     TRACE_SANITIZE (this);
     unsigned int len = get_len ();
 
-    if (!c->check_array (values, get_size (), count)) return_trace (false);
+    if (!c->check_array (values, count, get_size ())) return_trace (false);
 
     if (!has_device ()) return_trace (true);
 
@@ -376,7 +376,7 @@ struct AnchorMatrix
     if (!c->check_struct (this)) return_trace (false);
     if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
     unsigned int count = rows * cols;
-    if (!c->check_array (matrixZ, matrixZ[0].static_size, count)) return_trace (false);
+    if (!c->check_array (matrixZ, count)) return_trace (false);
     for (unsigned int i = 0; i < count; i++)
       if (!matrixZ[i].sanitize (c, this)) return_trace (false);
     return_trace (true);
@@ -698,7 +698,7 @@ struct PairSet
   {
     TRACE_SANITIZE (this);
     if (!(c->check_struct (this)
-       && c->check_array (arrayZ, HBUINT16::static_size * closure->stride, len))) return_trace (false);
+       && c->check_array (arrayZ, len, HBUINT16::static_size * closure->stride))) return_trace (false);
 
     unsigned int count = len;
     const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
@@ -869,7 +869,7 @@ struct PairPosFormat2
     unsigned int stride = len1 + len2;
     unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
     unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
-    return_trace (c->check_array (values, record_size, count) &&
+    return_trace (c->check_array (values, count, record_size) &&
                  valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
                  valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
   }
index d475565..719a505 100644 (file)
@@ -1677,11 +1677,11 @@ struct ContextFormat3
     if (!c->check_struct (this)) return_trace (false);
     unsigned int count = glyphCount;
     if (!count) return_trace (false); /* We want to access coverageZ[0] freely. */
-    if (!c->check_array (coverageZ.arrayZ, coverageZ[0].static_size, count)) return_trace (false);
+    if (!c->check_array (coverageZ.arrayZ, count)) return_trace (false);
     for (unsigned int i = 0; i < count; i++)
       if (!coverageZ[i].sanitize (c, this)) return_trace (false);
     const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverageZ.arrayZ, coverageZ[0].static_size * count);
-    return_trace (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
+    return_trace (c->check_array (lookupRecord, lookupCount));
   }
 
   protected:
index b7ca167..1b2d550 100644 (file)
@@ -242,9 +242,7 @@ struct MathKern
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this) &&
-                 c->check_array (mathValueRecords,
-                                 mathValueRecords[0].static_size,
-                                 2 * heightCount + 1) &&
+                 c->check_array (mathValueRecords, 2 * heightCount + 1) &&
                  sanitize_math_value_records (c));
   }
 
@@ -598,9 +596,7 @@ struct MathVariants
     return_trace (c->check_struct (this) &&
                  vertGlyphCoverage.sanitize (c, this) &&
                  horizGlyphCoverage.sanitize (c, this) &&
-                 c->check_array (glyphConstruction,
-                                 glyphConstruction[0].static_size,
-                                 vertGlyphCount + horizGlyphCount) &&
+                 c->check_array (glyphConstruction, vertGlyphCount + horizGlyphCount) &&
                  sanitize_offsets (c));
   }
 
index a1de119..c1e7adb 100644 (file)
@@ -118,7 +118,7 @@ struct name
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this) &&
                  likely (format == 0 || format == 1) &&
-                 c->check_array (nameRecord, nameRecord[0].static_size, count) &&
+                 c->check_array (nameRecord, count) &&
                  sanitize_records (c));
   }
 
index 5428c97..d100ca2 100644 (file)
@@ -93,6 +93,7 @@ struct SegmentMaps : ArrayOf<AxisValueMap>
            (value - arrayZ[i-1].fromCoord) + denom/2) / denom;
   }
 
+  public:
   DEFINE_SIZE_ARRAY (2, arrayZ);
 };
 
@@ -105,8 +106,7 @@ struct avar
     TRACE_SANITIZE (this);
     if (unlikely (!(version.sanitize (c) &&
                    version.major == 1 &&
-                   c->check_struct (this),
-                   c->check_array(axisSegmentMapsZ.arrayZ, sizeof (axisSegmentMapsZ[0]), axisCount))))
+                   c->check_struct (this))))
       return_trace (false);
 
     const SegmentMaps *map = axisSegmentMapsZ.arrayZ;
index 1a85b6e..1d17a97 100644 (file)
@@ -46,7 +46,7 @@ struct InstanceRecord
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this) &&
-                 c->check_array (coordinatesZ.arrayZ, coordinatesZ[0].static_size, axis_count));
+                 c->check_array (coordinatesZ.arrayZ, axis_count));
   }
 
   protected:
index 97300f4..d87285b 100644 (file)
@@ -39,7 +39,7 @@ struct DeltaSetIndexMap
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this) &&
-                 c->check_array (mapDataZ.arrayZ, get_width (), mapCount));
+                 c->check_array (mapDataZ.arrayZ, mapCount, get_width ()));
   }
 
   unsigned int map (unsigned int v) const /* Returns 16.16 outer.inner. */
index d10c3e9..d60c6b9 100644 (file)
@@ -68,7 +68,7 @@ struct MVAR
                  c->check_struct (this) &&
                  valueRecordSize >= VariationValueRecord::static_size &&
                  varStore.sanitize (c, this) &&
-                 c->check_array (valuesZ.arrayZ, valueRecordSize, valueRecordCount));
+                 c->check_array (valuesZ.arrayZ, valueRecordCount, valueRecordSize));
   }
 
   inline float get_var (hb_tag_t tag,