从v8-0.2.5中提取的代码
/**
* Checks whether two handles are the same.
* Returns true if both are empty, or if the objects
* to which they refer are identical.
* The handles' references are not checked.
*/
template <class S> bool operator==(Handle<S> that) {
void** a = reinterpret_cast<void**>(**this);
void** b = reinterpret_cast<void**>(*that);
if (a == 0) return b == 0;
if (b == 0) return false;
return *a == *b;
}
Handle
重载运算符*,使得 **this
和 *that
返回类型为 T*
。
看起来是这样的:
void* a = reinterpret_cast<void*>(**this);
void* b = reinterpret_cast<void*>(*that);
return a == b;
也能正常工作吗?
Handle<T>
进行解引用会产生一个T*
,那么这段代码就存在严重的缺陷,因为比较会产生假阳性和假阴性:如果sizeof(T) < sizeof(void*)
,则会出现假阴性,因为比较中包含了不属于对象的内容;如果sizeof(T) > sizeof(void*)
,则会出现假阳性,因为只有对象的前四/八个字节被比较。 - cmaster - reinstate monica