From f786c9d501099f3c989fb6e34469381f4be8681f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 4 Aug 2009 02:09:34 -0400 Subject: [HB] More sanitize() --- pango/opentype/hb-open-file-private.hh | 2 +- pango/opentype/hb-open-types-private.hh | 12 +++-- pango/opentype/hb-ot-layout-common-private.hh | 64 +++++++++++++++++++++---- pango/opentype/hb-ot-layout-gdef-private.hh | 2 +- pango/opentype/hb-ot-layout-gsubgpos-private.hh | 15 +++++- 5 files changed, 79 insertions(+), 16 deletions(-) diff --git a/pango/opentype/hb-open-file-private.hh b/pango/opentype/hb-open-file-private.hh index c06bb887..50b9bc20 100644 --- a/pango/opentype/hb-open-file-private.hh +++ b/pango/opentype/hb-open-file-private.hh @@ -85,7 +85,7 @@ struct TTCHeader { friend struct OpenTypeFontFile; - STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 2); + STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 1, 2); private: Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ diff --git a/pango/opentype/hb-open-types-private.hh b/pango/opentype/hb-open-types-private.hh index a8549e2b..3d57122a 100644 --- a/pango/opentype/hb-open-types-private.hh +++ b/pango/opentype/hb-open-types-private.hh @@ -60,6 +60,7 @@ struct _hb_sanitize_context_t #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, (const char *) this)) #define SANITIZE_THIS2(X,Y) SANITIZE_THIS (X) && SANITIZE_THIS (Y) +#define SANITIZE_THIS3(X,Y,Z) SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z) #define SANITIZE_SELF() SANITIZE_OBJ (*this) #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X)) @@ -205,12 +206,12 @@ struct Null \ return *(const Type*)data; \ } /* Like get_for_data(), but checks major version first. */ -#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, Major) \ +#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \ static inline const Type& get_for_data (const char *data) \ { \ if (HB_UNLIKELY (data == NULL)) return Null(Type); \ const Type& t = *(const Type*)data; \ - if (HB_UNLIKELY (!t.version.major || t.version.major > Major)) return Null(Type); \ + if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \ return t; \ } @@ -348,6 +349,10 @@ struct FixedVersion { inline operator uint32_t (void) const { return (major << 16) + minor; } + inline bool sanitize (SANITIZE_ARG_DEF) { + return SANITIZE_SELF (); + } + USHORT major; USHORT minor; }; @@ -371,12 +376,11 @@ struct ArrayOf inline bool sanitize (SANITIZE_ARG_DEF) { if (!(SANITIZE (len) && SANITIZE_GET_SIZE())) return false; - /* For non-offset types, this shouldn't be needed + /* XXX For non-recursive types, this is too much overhead */ unsigned int count = len; for (unsigned int i = 0; i < count; i++) if (!SANITIZE (array[i])) return false; - */ } USHORT len; diff --git a/pango/opentype/hb-ot-layout-common-private.hh b/pango/opentype/hb-ot-layout-common-private.hh index 3eb0a559..439d7e87 100644 --- a/pango/opentype/hb-ot-layout-common-private.hh +++ b/pango/opentype/hb-ot-layout-common-private.hh @@ -46,6 +46,12 @@ template struct Record { + inline bool sanitize (SANITIZE_ARG_DEF, const char *base) { + /* Note: Doesn't sanitize referenced object */ + /* Only accept ASCII-visible tags (mind DEL) */ + return (tag & 0x80808080) == 0 && offset.sanitize (SANITIZE_ARG, base); + } + Tag tag; /* 4-byte Tag identifier */ OffsetTo offset; /* Offset from beginning of object holding @@ -53,7 +59,19 @@ struct Record }; template -struct RecordListOf : ArrayOf > +struct RecordArrayOf : ArrayOf > +{ + inline bool sanitize (SANITIZE_ARG_DEF, const char *base) { + if (!(SANITIZE (this->len) && SANITIZE_GET_SIZE())) return false; + unsigned int count = this->len; + for (unsigned int i = 0; i < count; i++) + if (!SANITIZE_THIS (this->array[i])) + return false; + } +}; + +template +struct RecordListOf : RecordArrayOf { inline const Type& operator [] (unsigned int i) const { @@ -65,18 +83,16 @@ struct RecordListOf : ArrayOf > if (HB_UNLIKELY (i >= this->len)) return Null(Tag); return this->array[i].tag; } + + inline bool sanitize (SANITIZE_ARG_DEF) { + return RecordArrayOf::sanitize (SANITIZE_ARG, (const char *) this); + } }; struct Script; -typedef Record