Add different casts from pointer and ref to avoid bugs

This commit is contained in:
Behdad Esfahbod 2010-04-23 16:35:01 -04:00
parent efb324a46f
commit 187454c595
4 changed files with 28 additions and 20 deletions

View File

@ -49,14 +49,22 @@ template <typename Type>
inline char * CharP (Type* X)
{ return reinterpret_cast<char *>(X); }
/* Cast to struct T& */
/* Cast to struct T, reference to reference */
template<typename Type, typename TObject>
inline const Type& Cast(const TObject &X)
inline const Type& CastR(const TObject &X)
{ return reinterpret_cast<const Type&> (X); }
template<typename Type, typename TObject>
inline Type& Cast(TObject &X)
inline Type& CastR(TObject &X)
{ return reinterpret_cast<Type&> (X); }
/* Cast to struct T, pointer to pointer */
template<typename Type, typename TObject>
inline const Type* CastP(const TObject *X)
{ return reinterpret_cast<const Type*> (X); }
template<typename Type, typename TObject>
inline Type* CastP(TObject *X)
{ return reinterpret_cast<Type*> (X); }
/* StructAtOffset<T>(X,Ofs) returns the struct T& that is placed at memory
* location of X plus Ofs bytes. */
template<typename Type, typename TObject>
@ -91,7 +99,7 @@ static const void *_NullPool[32 / sizeof (void *)];
template <typename Type>
static inline const Type& Null () {
ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
return Cast<Type> (*_NullPool);
return *CastP<Type> (_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<Type> () { \
return Cast<Type> (*_Null##Type); \
return *CastP<Type> (_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<Type> (* (char *) CharP(context.start));
Type *t = CastP<Type> ((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<Type> (* (const char *) hb_blob_lock (blob));
return *CastP<Type> (hb_blob_lock (blob));
}
};

View File

@ -1445,7 +1445,7 @@ struct PosLookupSubTable
struct PosLookup : Lookup
{
inline const PosLookupSubTable& get_subtable (unsigned int i) const
{ return this+Cast<OffsetArrayOf<PosLookupSubTable> > (subTable)[i]; }
{ return this+CastR<OffsetArrayOf<PosLookupSubTable> > (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<PosLookupSubTable> &list = Cast<OffsetArrayOf<PosLookupSubTable> > (subTable);
OffsetArrayOf<PosLookupSubTable> &list = CastR<OffsetArrayOf<PosLookupSubTable> > (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<PosLookup> (GSUBGPOS::get_lookup (i)); }
{ return CastR<PosLookup> (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<PosLookupList> &list = Cast<OffsetTo<PosLookupList> > (lookupList);
OffsetTo<PosLookupList> &list = CastR<OffsetTo<PosLookupList> > (lookupList);
return SANITIZE_THIS (list);
}
};

View File

@ -743,7 +743,7 @@ struct SubstLookupSubTable
struct SubstLookup : Lookup
{
inline const SubstLookupSubTable& get_subtable (unsigned int i) const
{ return this+Cast<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
{ return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (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<ExtensionSubst> (get_subtable(0)).is_reverse ();
return CastR<ExtensionSubst> (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<SubstLookupSubTable> &list = Cast<OffsetArrayOf<SubstLookupSubTable> > (subTable);
OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (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<SubstLookup> (GSUBGPOS::get_lookup (i)); }
{ return CastR<SubstLookup> (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<SubstLookupList> &list = Cast<OffsetTo<SubstLookupList> > (lookupList);
OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (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<ExtensionSubst> (get_subtable()).is_reverse ();
return CastR<ExtensionSubst> (get_subtable()).is_reverse ();
return SubstLookup::lookup_type_is_reverse (type);
}

View File

@ -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<OpenTypeFontFile> (font_data);
const OpenTypeFontFile &ot = *CastP<OpenTypeFontFile> (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<GSUBGPOS> (font_data + table.offset);
const GSUBGPOS &g = *CastP<GSUBGPOS> (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<GDEF> (font_data + table.offset);
const GDEF &gdef = *CastP<GDEF> (font_data + table.offset);
printf (" Has %sglyph classes\n",
gdef.has_glyph_classes () ? "" : "no ");