[英]Overflow in C++ Unordered_set function
TBH, Im surprised Im asking this question but looking at this code TBH,我很惊讶我问了这个问题,但是看着这段代码
template <class _Tp, class _Hash, class _Equal, class _Alloc>
void
__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
{
if (__n == 1)
__n = 2;
else if (__n & (__n - 1)) // >>>>>>LINE IN QUESTION<<<<<<<<<<<
__n = __next_prime(__n);
size_type __bc = bucket_count();
if (__n > __bc)
__rehash(__n);
else if (__n < __bc)
{
__n = _VSTD::max<size_type>
(
__n,
__is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
__next_prime(size_t(ceil(float(size()) / max_load_factor())))
);
if (__n < __bc)
__rehash(__n);
}
}
On that line above, should __n
be allowed to be zero, that - 1
next to it leads to an integer overflow that a debugger (in my case XCode) catches and complains about. 在上面的那一行,应该将
__n
设为零,即- 1
旁边的- 1
会导致整数溢出,调试器(在我的情况下为XCode)会捕获并抱怨。 Now, my understanding here is that the &
operation on that line may make the line as a whole still relevant for the hashing process. 现在,我的理解是,该行上的
&
操作可能会使该行整体上仍与哈希过程相关。 But what have others done to work around this?? 但是其他人为解决这个问题做了什么? Silence the debugger?
使调试器静音? The function call that did this was another std library call:
完成此操作的函数调用是另一个标准库调用:
template <class _Value, class _Hash, class _Pred, class _Alloc>
unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set(const unordered_set& __u)
: __table_(__u.__table_){
#if _LIBCPP_DEBUG_LEVEL >= 2
__get_db()->__insert_c(this);
#endif
__table_.rehash(__u.bucket_count()); // >>FUNCTION CALL<<<<
insert(__u.begin(), __u.end());
}
Here the __u.bucket_count()
returns zero and thus the behaviour in question occurs. 在这里
__u.bucket_count()
返回零,因此发生了相关行为。 The error or sanitizer warning I received by the Undefined behaviour sanitizer in XCode was Unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned long'
我在XCode中由未定义的行为清除程序收到的错误或清除程序警告是
Unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned long'
0-1 Unsigned integer overflow: 0 - 1 cannot be represented in type 'unsigned long'
It seems a bug report was created about it and one of the developers answered it here https://bugs.llvm.org/show_bug.cgi?id=38606 It seems the operation performs a check to see if it's a power of 2. This this code is very intentional. 似乎已为此创建了一个错误报告,并且其中一个开发人员在这里回答了这个问题https://bugs.llvm.org/show_bug.cgi?id=38606似乎该操作正在执行检查以查看是否为2的幂。此代码非常有意。
According to this ticket here, https://bugs.llvm.org/show_bug.cgi?id=25706 , it seems they are going through and silencing all such interesting occurrences in the standard library. 根据这张票, https://bugs.llvm.org/show_bug.cgi?id=25706 ,看来他们正在经历并沉默标准库中所有这些有趣的事件。
So that's my answer I guess. 这就是我的答案。 Either wait for them to silence it or I can silence it myself if not ignore it.
要么等待他们将其静音,要么我自己将其静音(如果不忽略它的话)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.