[HB] More cast cleanup

This commit is contained in:
Behdad Esfahbod 2009-08-04 11:38:50 -04:00
parent 196598bbcc
commit 2b5a59c277
4 changed files with 50 additions and 71 deletions

View File

@ -42,8 +42,13 @@
* Casts * Casts
*/ */
#define CONST_CHARP(X) (reinterpret_cast<const char *>(X)) #define CONST_CHARP(X) (reinterpret_cast<const char *>(X))
#define CHARP(X) ((char *)reinterpret_cast<const char *>(X)) #define DECONST_CHARP(X) ((char *)reinterpret_cast<const char *>(X))
#define CHARP(X) (reinterpret_cast<char *>(X))
#define CONST_CAST(T,X,Ofs) (*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + Ofs)))
#define DECONST_CAST(T,X,Ofs) (*(reinterpret_cast<T *>((char *)CONST_CHARP(&(X)) + Ofs)))
#define CAST(T,X,Ofs) (*(reinterpret_cast<T *>(CHARP(&(X)) + Ofs)))
/* /*
@ -65,7 +70,7 @@ struct _hb_sanitize_context_t
#define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG)) #define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
#define SANITIZE2(X,Y) SANITIZE (X) && SANITIZE (Y) #define SANITIZE2(X,Y) SANITIZE (X) && SANITIZE (Y)
#define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, reinterpret_cast<const char *>(this))) #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CONST_CHARP(this)))
#define SANITIZE_THIS2(X,Y) SANITIZE_THIS (X) && SANITIZE_THIS (Y) #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_THIS3(X,Y,Z) SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z)
@ -73,7 +78,7 @@ struct _hb_sanitize_context_t
#define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X)) #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
#define SANITIZE_GET_SIZE() SANITIZE_MEM (this, this->get_size ()) #define SANITIZE_GET_SIZE() SANITIZE_MEM (this, this->get_size ())
#define SANITIZE_MEM(B,L) HB_LIKELY (context->start <= reinterpret_cast<const char *>(B) && reinterpret_cast<const char *>(B) + (L) <= context->end) /* XXX overflow */ #define SANITIZE_MEM(B,L) HB_LIKELY (context->start <= CONST_CHARP(B) && CONST_CHARP(B) + (L) <= context->end) /* XXX overflow */
#define NEUTER(Var, Val) (false) #define NEUTER(Var, Val) (false)
@ -272,8 +277,8 @@ struct Tag : ULONG
inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; } inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; }
inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; } inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; }
/* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
inline operator const char* (void) const { return reinterpret_cast<const char *>(this); } inline operator const char* (void) const { return CONST_CHARP(this); }
inline operator char* (void) { return reinterpret_cast<char *>(this); } inline operator char* (void) { return CHARP(this); }
}; };
ASSERT_SIZE (Tag, 4); ASSERT_SIZE (Tag, 4);
#define _NULL_TAG_INIT {' ', ' ', ' ', ' '} #define _NULL_TAG_INIT {' ', ' ', ' ', ' '}
@ -337,14 +342,14 @@ struct GenericOffsetTo : OffsetType
{ {
unsigned int offset = *this; unsigned int offset = *this;
if (HB_UNLIKELY (!offset)) return Null(Type); if (HB_UNLIKELY (!offset)) return Null(Type);
return *reinterpret_cast<const Type*>(reinterpret_cast<const char *>(base) + offset); return CONST_CAST(Type, base, offset);
} }
inline bool sanitize (SANITIZE_ARG_DEF, const void *base) { inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
if (!SANITIZE_OBJ (*this)) return false; if (!SANITIZE_OBJ (*this)) return false;
unsigned int offset = *this; unsigned int offset = *this;
if (HB_UNLIKELY (!offset)) return true; if (HB_UNLIKELY (!offset)) return true;
return SANITIZE (*reinterpret_cast<Type*>((char *)(reinterpret_cast<const char *>(base) + offset))) || NEUTER (*this, 0); return SANITIZE (CAST(Type, base, offset)) || NEUTER (*this, 0);
} }
}; };
template <typename Base, typename OffsetType, typename Type> template <typename Base, typename OffsetType, typename Type>
@ -379,7 +384,7 @@ struct GenericArrayOf
return false; return false;
*/ */
} }
inline bool sanitize (SANITIZE_ARG_DEF, const char *base) { inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
if (!(SANITIZE (len) && SANITIZE_GET_SIZE())) return false; if (!(SANITIZE (len) && SANITIZE_GET_SIZE())) return false;
unsigned int count = len; unsigned int count = len;
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)

View File

@ -46,7 +46,7 @@
template <typename Type> template <typename Type>
struct Record struct Record
{ {
inline bool sanitize (SANITIZE_ARG_DEF, const char *base) { inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
/* Note: Only accept ASCII-visible tags (mind DEL) /* Note: Only accept ASCII-visible tags (mind DEL)
* This is one of the few times (only time?) we check * This is one of the few times (only time?) we check
* for data integrity, as opposed o just boundary checks * for data integrity, as opposed o just boundary checks
@ -210,8 +210,7 @@ struct Lookup
unsigned int flag = lookupFlag; unsigned int flag = lookupFlag;
if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet)) if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
{ {
const USHORT &markFilteringSet = *(const USHORT*) const USHORT &markFilteringSet = CONST_CAST (USHORT, subTable, subTable.get_size ());
(CONST_CHARP(&subTable) + subTable.get_size ());
flag += (markFilteringSet << 16); flag += (markFilteringSet << 16);
} }
return flag; return flag;
@ -221,7 +220,7 @@ struct Lookup
if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false; if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet)) if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
{ {
USHORT &markFilteringSet = *(USHORT*) (CHARP(&subTable) + subTable.get_size ()); USHORT &markFilteringSet = CAST (USHORT, subTable, subTable.get_size ());
if (!SANITIZE (markFilteringSet)) return false; if (!SANITIZE (markFilteringSet)) return false;
} }
return true; return true;

View File

@ -543,9 +543,7 @@ struct ExtensionSubst : Extension
private: private:
inline const struct SubstLookupSubTable& get_subtable (void) const inline const struct SubstLookupSubTable& get_subtable (void) const
{ return (const struct SubstLookupSubTable&) Extension::get_subtable (); } { return CONST_CAST (SubstLookupSubTable, Extension::get_subtable (), 0); }
inline struct SubstLookupSubTable& get_subtable (void)
{ return (struct SubstLookupSubTable&) Extension::get_subtable (); }
inline bool apply (APPLY_ARG_DEF) const; inline bool apply (APPLY_ARG_DEF) const;
@ -568,17 +566,15 @@ struct ReverseChainSingleSubstFormat1
if (HB_LIKELY (index == NOT_COVERED)) if (HB_LIKELY (index == NOT_COVERED))
return false; return false;
const OffsetArrayOf<Coverage> &lookahead = *(const OffsetArrayOf<Coverage>*) const OffsetArrayOf<Coverage> &lookahead = CONST_CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
(CONST_CHARP(&backtrack) + backtrack.get_size ()); const ArrayOf<GlyphID> &substitute = CONST_CAST (ArrayOf<GlyphID>, lookahead, lookahead.get_size ());
const ArrayOf<GlyphID> &substitute = *(const ArrayOf<GlyphID>*)
(CONST_CHARP(&lookahead) + lookahead.get_size ());
if (match_backtrack (APPLY_ARG, if (match_backtrack (APPLY_ARG,
backtrack.len, (USHORT *) backtrack.array, backtrack.len, (USHORT *) backtrack.array,
match_coverage, CHARP(this)) && match_coverage, DECONST_CHARP(this)) &&
match_lookahead (APPLY_ARG, match_lookahead (APPLY_ARG,
lookahead.len, (USHORT *) lookahead.array, lookahead.len, (USHORT *) lookahead.array,
match_coverage, CHARP(this), match_coverage, DECONST_CHARP(this),
1)) 1))
{ {
IN_CURGLYPH () = substitute[index]; IN_CURGLYPH () = substitute[index];
@ -592,12 +588,10 @@ struct ReverseChainSingleSubstFormat1
inline bool sanitize (SANITIZE_ARG_DEF) { inline bool sanitize (SANITIZE_ARG_DEF) {
if (!SANITIZE_THIS2 (coverage, backtrack)) if (!SANITIZE_THIS2 (coverage, backtrack))
return false; return false;
OffsetArrayOf<Coverage> &lookahead = *(OffsetArrayOf<Coverage>*) OffsetArrayOf<Coverage> &lookahead = CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
(CONST_CHARP(&backtrack) + backtrack.get_size ());
if (!SANITIZE_THIS (lookahead)) if (!SANITIZE_THIS (lookahead))
return false; return false;
ArrayOf<GlyphID> &substitute = *(ArrayOf<GlyphID>*) ArrayOf<GlyphID> &substitute = CAST (ArrayOf<GlyphID>, lookahead, lookahead.get_size ());
(CONST_CHARP(&lookahead) + lookahead.get_size ());
if (!SANITIZE (substitute)) if (!SANITIZE (substitute))
return false; return false;
} }
@ -720,9 +714,7 @@ ASSERT_SIZE (SubstLookupSubTable, 2);
struct SubstLookup : Lookup struct SubstLookup : Lookup
{ {
inline const SubstLookupSubTable& get_subtable (unsigned int i) const inline const SubstLookupSubTable& get_subtable (unsigned int i) const
{ return (const SubstLookupSubTable&) Lookup::get_subtable (i); } { return CONST_CAST (SubstLookupSubTable, Lookup::get_subtable (i), 0); }
inline SubstLookupSubTable& get_subtable (unsigned int i)
{ return (SubstLookupSubTable&) Lookup::get_subtable (i); }
/* Like get_type(), but looks through extension lookups. /* Like get_type(), but looks through extension lookups.
* Never returns Extension */ * Never returns Extension */
@ -871,7 +863,8 @@ inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF) inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
{ {
return Extension::sanitize (SANITIZE_ARG) && get_subtable ().sanitize (SANITIZE_ARG); return Extension::sanitize (SANITIZE_ARG) &&
DECONST_CAST (SubstLookupSubTable, get_subtable (), 0).sanitize (SANITIZE_ARG);
} }
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index) static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)

View File

@ -64,7 +64,7 @@ static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, ch
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data) static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data)
{ {
const ClassDef &class_def = *(const ClassDef *)data; const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
return class_def.get_class (glyph_id) == value; return class_def.get_class (glyph_id) == value;
} }
@ -256,9 +256,7 @@ struct Rule
private: private:
inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
{ {
const LookupRecord *lookupRecord = (const LookupRecord *) const LookupRecord *lookupRecord = &CONST_CAST (LookupRecord, input, sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
(CONST_CHARP(input) +
sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
return context_lookup (APPLY_ARG, return context_lookup (APPLY_ARG,
inputCount, input, inputCount, input,
lookupCount, lookupRecord, lookupCount, lookupRecord,
@ -364,7 +362,7 @@ struct ContextFormat2
*/ */
struct ContextLookupContext lookup_context = { struct ContextLookupContext lookup_context = {
{match_class, apply_func}, {match_class, apply_func},
CHARP(&class_def) DECONST_CHARP(&class_def)
}; };
return rule_set.apply (APPLY_ARG, lookup_context); return rule_set.apply (APPLY_ARG, lookup_context);
} }
@ -399,12 +397,10 @@ struct ContextFormat3
if (HB_LIKELY (index == NOT_COVERED)) if (HB_LIKELY (index == NOT_COVERED))
return false; return false;
const LookupRecord *lookupRecord = (const LookupRecord *) const LookupRecord *lookupRecord = &CONST_CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
(CONST_CHARP(coverage) +
sizeof (coverage[0]) * glyphCount);
struct ContextLookupContext lookup_context = { struct ContextLookupContext lookup_context = {
{match_coverage, apply_func}, {match_coverage, apply_func},
CHARP(this) DECONST_CHARP(this)
}; };
return context_lookup (APPLY_ARG, return context_lookup (APPLY_ARG,
glyphCount, (const USHORT *) (coverage + 1), glyphCount, (const USHORT *) (coverage + 1),
@ -417,9 +413,7 @@ struct ContextFormat3
unsigned int count = glyphCount; unsigned int count = glyphCount;
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
if (!SANITIZE_THIS (coverage[i])) return false; if (!SANITIZE_THIS (coverage[i])) return false;
LookupRecord *lookupRecord = (LookupRecord *) LookupRecord *lookupRecord = &CAST(LookupRecord, coverage, sizeof (coverage[0]) * glyphCount);
(CHARP(coverage) +
sizeof (coverage[0]) * glyphCount);
return SANITIZE_MEM (lookupRecord, sizeof (lookupRecord[0]) * lookupCount); return SANITIZE_MEM (lookupRecord, sizeof (lookupRecord[0]) * lookupCount);
} }
@ -521,12 +515,9 @@ struct ChainRule
private: private:
inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
{ {
const HeadlessArrayOf<USHORT> &input = *(const HeadlessArrayOf<USHORT>*) const HeadlessArrayOf<USHORT> &input = CONST_CAST (HeadlessArrayOf<USHORT>, backtrack, backtrack.get_size ());
(CONST_CHARP(&backtrack) + backtrack.get_size ()); const ArrayOf<USHORT> &lookahead = CONST_CAST (ArrayOf<USHORT>, input, input.get_size ());
const ArrayOf<USHORT> &lookahead = *(const ArrayOf<USHORT>*) const ArrayOf<LookupRecord> &lookup = CONST_CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
(CONST_CHARP(&input) + input.get_size ());
const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
(CONST_CHARP(&lookahead) + lookahead.get_size ());
return chain_context_lookup (APPLY_ARG, return chain_context_lookup (APPLY_ARG,
backtrack.len, backtrack.array, backtrack.len, backtrack.array,
input.len, input.array + 1, input.len, input.array + 1,
@ -539,14 +530,11 @@ struct ChainRule
public: public:
inline bool sanitize (SANITIZE_ARG_DEF) { inline bool sanitize (SANITIZE_ARG_DEF) {
if (!SANITIZE (backtrack)) return false; if (!SANITIZE (backtrack)) return false;
HeadlessArrayOf<USHORT> &input = *(HeadlessArrayOf<USHORT>*) HeadlessArrayOf<USHORT> &input = CAST (HeadlessArrayOf<USHORT>, backtrack, backtrack.get_size ());
(CHARP(&backtrack) + backtrack.get_size ());
if (!SANITIZE (input)) return false; if (!SANITIZE (input)) return false;
ArrayOf<USHORT> &lookahead = *(ArrayOf<USHORT>*) ArrayOf<USHORT> &lookahead = CAST (ArrayOf<USHORT>, input, input.get_size ());
(CHARP(&input) + input.get_size ());
if (!SANITIZE (lookahead)) return false; if (!SANITIZE (lookahead)) return false;
ArrayOf<LookupRecord> &lookup = *(ArrayOf<LookupRecord>*) ArrayOf<LookupRecord> &lookup = CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
(CHARP(&lookahead) + lookahead.get_size ());
return SANITIZE (lookup); return SANITIZE (lookup);
} }
@ -648,9 +636,9 @@ struct ChainContextFormat2
*/ */
struct ChainContextLookupContext lookup_context = { struct ChainContextLookupContext lookup_context = {
{match_class, apply_func}, {match_class, apply_func},
{CHARP(&backtrack_class_def), {DECONST_CHARP(&backtrack_class_def),
CHARP(&input_class_def), DECONST_CHARP(&input_class_def),
CHARP(&lookahead_class_def)} DECONST_CHARP(&lookahead_class_def)}
}; };
return rule_set.apply (APPLY_ARG, lookup_context); return rule_set.apply (APPLY_ARG, lookup_context);
} }
@ -692,20 +680,17 @@ struct ChainContextFormat3
inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
{ {
const OffsetArrayOf<Coverage> &input = *(const OffsetArrayOf<Coverage>*) const OffsetArrayOf<Coverage> &input = CONST_CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
(CONST_CHARP(&backtrack) + backtrack.get_size ());
unsigned int index = (this+input[0]) (IN_CURGLYPH ()); unsigned int index = (this+input[0]) (IN_CURGLYPH ());
if (HB_LIKELY (index == NOT_COVERED)) if (HB_LIKELY (index == NOT_COVERED))
return false; return false;
const OffsetArrayOf<Coverage> &lookahead = *(const OffsetArrayOf<Coverage>*) const OffsetArrayOf<Coverage> &lookahead = CONST_CAST (OffsetArrayOf<Coverage>, input, input.get_size ());
(CONST_CHARP(&input) + input.get_size ()); const ArrayOf<LookupRecord> &lookup = CONST_CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
(CONST_CHARP(&lookahead) + lookahead.get_size ());
struct ChainContextLookupContext lookup_context = { struct ChainContextLookupContext lookup_context = {
{match_coverage, apply_func}, {match_coverage, apply_func},
{CHARP(this), CHARP(this), CHARP(this)} {DECONST_CHARP(this), DECONST_CHARP(this), DECONST_CHARP(this)}
}; };
return chain_context_lookup (APPLY_ARG, return chain_context_lookup (APPLY_ARG,
backtrack.len, (USHORT *) backtrack.array, backtrack.len, (USHORT *) backtrack.array,
@ -718,14 +703,11 @@ struct ChainContextFormat3
inline bool sanitize (SANITIZE_ARG_DEF) { inline bool sanitize (SANITIZE_ARG_DEF) {
if (!SANITIZE_THIS (backtrack)) return false; if (!SANITIZE_THIS (backtrack)) return false;
OffsetArrayOf<Coverage> &input = *(OffsetArrayOf<Coverage>*) OffsetArrayOf<Coverage> &input = CAST (OffsetArrayOf<Coverage>, backtrack, backtrack.get_size ());
(CHARP(&backtrack) + backtrack.get_size ());
if (!SANITIZE_THIS (input)) return false; if (!SANITIZE_THIS (input)) return false;
OffsetArrayOf<Coverage> &lookahead = *(OffsetArrayOf<Coverage>*) OffsetArrayOf<Coverage> &lookahead = CAST (OffsetArrayOf<Coverage>, input, input.get_size ());
(CHARP(&input) + input.get_size ());
if (!SANITIZE_THIS (lookahead)) return false; if (!SANITIZE_THIS (lookahead)) return false;
ArrayOf<LookupRecord> &lookup = *(ArrayOf<LookupRecord>*) ArrayOf<LookupRecord> &lookup = CAST (ArrayOf<LookupRecord>, lookahead, lookahead.get_size ());
(CHARP(&lookahead) + lookahead.get_size ());
return SANITIZE (lookup); return SANITIZE (lookup);
} }
@ -794,7 +776,7 @@ struct ExtensionFormat1
{ {
unsigned int offset = get_offset (); unsigned int offset = get_offset ();
if (HB_UNLIKELY (!offset)) return Null(LookupSubTable); if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
return *(LookupSubTable*)(CHARP(this) + offset); return CONST_CAST (LookupSubTable, *this, offset);
} }
inline bool sanitize (SANITIZE_ARG_DEF) { inline bool sanitize (SANITIZE_ARG_DEF) {