From 187454c595559ce48d072fee6bccb51f3de283d4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 23 Apr 2010 16:35:01 -0400 Subject: Add different casts from pointer and ref to avoid bugs --- src/hb-open-type-private.hh | 22 +++++++++++++++------- src/hb-ot-layout-gpos-private.hh | 8 ++++---- src/hb-ot-layout-gsub-private.hh | 12 ++++++------ src/main.cc | 6 +++--- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/hb-open-type-private.hh b/src/hb-open-type-private.hh index 232a61e..daf0c40 100644 --- a/src/hb-open-type-private.hh +++ b/src/hb-open-type-private.hh @@ -49,14 +49,22 @@ template inline char * CharP (Type* X) { return reinterpret_cast(X); } -/* Cast to struct T& */ +/* Cast to struct T, reference to reference */ template -inline const Type& Cast(const TObject &X) +inline const Type& CastR(const TObject &X) { return reinterpret_cast (X); } template -inline Type& Cast(TObject &X) +inline Type& CastR(TObject &X) { return reinterpret_cast (X); } +/* Cast to struct T, pointer to pointer */ +template +inline const Type* CastP(const TObject *X) +{ return reinterpret_cast (X); } +template +inline Type* CastP(TObject *X) +{ return reinterpret_cast (X); } + /* StructAtOffset(X,Ofs) returns the struct T& that is placed at memory * location of X plus Ofs bytes. */ template @@ -91,7 +99,7 @@ static const void *_NullPool[32 / sizeof (void *)]; template static inline const Type& Null () { ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool)); - return Cast (*_NullPool); + return *CastP (_NullPool); } /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ @@ -99,7 +107,7 @@ static inline const Type& Null () { static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \ template <> \ inline const Type& Null () { \ - return Cast (*_Null##Type); \ + return *CastP (_Null##Type); \ } /* The following line really exists such that we end in a place needing semicolon */ \ ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type)) @@ -281,7 +289,7 @@ struct Sanitizer _hb_sanitize_init (&context, blob); /* Note: We drop const here */ - Type *t = &Cast (* (char *) CharP(context.start)); + Type *t = CastP ((void *) context.start); sane = t->sanitize (SANITIZE_ARG_INIT); if (sane) { @@ -326,7 +334,7 @@ struct Sanitizer } static const Type& lock_instance (hb_blob_t *blob) { - return Cast (* (const char *) hb_blob_lock (blob)); + return *CastP (hb_blob_lock (blob)); } }; diff --git a/src/hb-ot-layout-gpos-private.hh b/src/hb-ot-layout-gpos-private.hh index 73ac56d..777874b 100644 --- a/src/hb-ot-layout-gpos-private.hh +++ b/src/hb-ot-layout-gpos-private.hh @@ -1445,7 +1445,7 @@ struct PosLookupSubTable struct PosLookup : Lookup { inline const PosLookupSubTable& get_subtable (unsigned int i) const - { return this+Cast > (subTable)[i]; } + { return this+CastR > (subTable)[i]; } inline bool apply_once (hb_ot_layout_context_t *context, hb_buffer_t *buffer, @@ -1504,7 +1504,7 @@ struct PosLookup : Lookup inline bool sanitize (SANITIZE_ARG_DEF) { TRACE_SANITIZE (); if (HB_UNLIKELY (!Lookup::sanitize (SANITIZE_ARG))) return false; - OffsetArrayOf &list = Cast > (subTable); + OffsetArrayOf &list = CastR > (subTable); return SANITIZE_THIS (list); } }; @@ -1521,7 +1521,7 @@ struct GPOS : GSUBGPOS static const hb_tag_t Tag = HB_OT_TAG_GPOS; inline const PosLookup& get_lookup (unsigned int i) const - { return Cast (GSUBGPOS::get_lookup (i)); } + { return CastR (GSUBGPOS::get_lookup (i)); } inline bool position_lookup (hb_ot_layout_context_t *context, hb_buffer_t *buffer, @@ -1532,7 +1532,7 @@ struct GPOS : GSUBGPOS inline bool sanitize (SANITIZE_ARG_DEF) { TRACE_SANITIZE (); if (HB_UNLIKELY (!GSUBGPOS::sanitize (SANITIZE_ARG))) return false; - OffsetTo &list = Cast > (lookupList); + OffsetTo &list = CastR > (lookupList); return SANITIZE_THIS (list); } }; diff --git a/src/hb-ot-layout-gsub-private.hh b/src/hb-ot-layout-gsub-private.hh index fc3bb29..77c27a8 100644 --- a/src/hb-ot-layout-gsub-private.hh +++ b/src/hb-ot-layout-gsub-private.hh @@ -743,7 +743,7 @@ struct SubstLookupSubTable struct SubstLookup : Lookup { inline const SubstLookupSubTable& get_subtable (unsigned int i) const - { return this+Cast > (subTable)[i]; } + { return this+CastR > (subTable)[i]; } inline static bool lookup_type_is_reverse (unsigned int lookup_type) { return lookup_type == SubstLookupSubTable::ReverseChainSingle; } @@ -752,7 +752,7 @@ struct SubstLookup : Lookup { unsigned int type = get_type (); if (HB_UNLIKELY (type == SubstLookupSubTable::Extension)) - return Cast (get_subtable(0)).is_reverse (); + return CastR (get_subtable(0)).is_reverse (); return lookup_type_is_reverse (type); } @@ -839,7 +839,7 @@ struct SubstLookup : Lookup inline bool sanitize (SANITIZE_ARG_DEF) { TRACE_SANITIZE (); if (HB_UNLIKELY (!Lookup::sanitize (SANITIZE_ARG))) return false; - OffsetArrayOf &list = Cast > (subTable); + OffsetArrayOf &list = CastR > (subTable); return SANITIZE_THIS (list); } }; @@ -856,7 +856,7 @@ struct GSUB : GSUBGPOS static const hb_tag_t Tag = HB_OT_TAG_GSUB; inline const SubstLookup& get_lookup (unsigned int i) const - { return Cast (GSUBGPOS::get_lookup (i)); } + { return CastR (GSUBGPOS::get_lookup (i)); } inline bool substitute_lookup (hb_ot_layout_context_t *context, hb_buffer_t *buffer, @@ -868,7 +868,7 @@ struct GSUB : GSUBGPOS inline bool sanitize (SANITIZE_ARG_DEF) { TRACE_SANITIZE (); if (HB_UNLIKELY (!GSUBGPOS::sanitize (SANITIZE_ARG))) return false; - OffsetTo &list = Cast > (lookupList); + OffsetTo &list = CastR > (lookupList); return SANITIZE_THIS (list); } }; @@ -896,7 +896,7 @@ inline bool ExtensionSubst::is_reverse (void) const { unsigned int type = get_type (); if (HB_UNLIKELY (type == SubstLookupSubTable::Extension)) - return Cast (get_subtable()).is_reverse (); + return CastR (get_subtable()).is_reverse (); return SubstLookup::lookup_type_is_reverse (type); } diff --git a/src/main.cc b/src/main.cc index badf91b..2e0927a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -61,7 +61,7 @@ main (int argc, char **argv) printf ("Opened font file %s: %d bytes long\n", argv[1], len); - const OpenTypeFontFile &ot = Cast (font_data); + const OpenTypeFontFile &ot = *CastP (font_data); switch (ot.get_tag ()) { case OpenTypeFontFile::TrueTypeTag: @@ -99,7 +99,7 @@ main (int argc, char **argv) case GSUBGPOS::GPOSTag: { - const GSUBGPOS &g = Cast (font_data + table.offset); + const GSUBGPOS &g = *CastP (font_data + table.offset); int num_scripts = g.get_script_count (); printf (" %d script(s) found in table\n", num_scripts); @@ -162,7 +162,7 @@ main (int argc, char **argv) case GDEF::Tag: { - const GDEF &gdef = Cast (font_data + table.offset); + const GDEF &gdef = *CastP (font_data + table.offset); printf (" Has %sglyph classes\n", gdef.has_glyph_classes () ? "" : "no "); -- cgit v1.2.3