簡體   English   中英

C ++ Unordered_set函數中的溢出

[英]Overflow in C++ Unordered_set function

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);
    }
}

在上面的那一行,應該將__n設為零,即- 1旁邊的- 1會導致整數溢出,調試器(在我的情況下為XCode)會捕獲並抱怨。 現在,我的理解是,該行上的&操作可能會使該行整體上仍與哈希過程相關。 但是其他人為解決這個問題做了什么? 使調試器靜音? 完成此操作的函數調用是另一個標准庫調用:

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());
      }

在這里__u.bucket_count()返回零,因此發生了相關行為。 我在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'

似乎已為此創建了一個錯誤報告,並且其中一個開發人員在這里回答了這個問題https://bugs.llvm.org/show_bug.cgi?id=38606似乎該操作正在執行檢查以查看是否為2的冪。此代碼非常有意。

根據這張票, https://bugs.llvm.org/show_bug.cgi?id=25706 ,看來他們正在經歷並沉默標准庫中所有這些有趣的事件。

這就是我的答案。 要么等待他們將其靜音,要么我自己將其靜音(如果不忽略它的話)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM