Modify

Ticket #7739 (closed Bugs: fixed)

Opened 17 months ago

Last modified 11 months ago

Divide by zero error in hash_map.hpp

Reported by: Gaurav Gupta <g.gupta@…> Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc: yogen.saini@…

Description

In the code snippet, while doing calculate_hash_value(iter->first) % num_buckets_ If value of num_buckets_ is 0, then it leads to Divide By zero error.

void rehash(std::size_t num_buckets)

{

if (num_buckets == num_buckets_)

return;

num_buckets_ = num_buckets;

iterator end_iter = values_.end();

----- SOME CODE ------------

Put all values back into the hash. iterator iter = values_.begin(); while (iter != end_iter) {

std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_;

if (buckets_[bucket].last == end_iter)

{

buckets_[bucket].first = buckets_[bucket].last = iter++;

}

------- SOME CODE -----------

Attached patch is the fix for it.

Attachments

hash_map.hpp_patch Download (673 bytes) - added by Gaurav Gupta <g.gupta@…> 17 months ago.
Fix for the Divide by zero error in hash_map.hpp

Change History

Changed 17 months ago by Gaurav Gupta <g.gupta@…>

Fix for the Divide by zero error in hash_map.hpp

comment:1 follow-up: ↓ 2 Changed 17 months ago by steven_watanabe

This is not a bug, as num_buckets cannot be zero in the first place.

comment:2 in reply to: ↑ 1 Changed 17 months ago by Gaurav Gupta <g.gupta@…>

Replying to steven_watanabe:

This is not a bug, as num_buckets cannot be zero in the first place.

Its True, num_buckets canot be zero, but this check is safe here, as many tools (like Prevent )report this as DIVIDE By Zero error.

Also In my patch , I have provided fix at two places, other is in following code snippet.

void erase(iterator it)
  {
    BOOST_ASSERT(it != values_.end());

    '''size_t bucket = calculate_hash_value(it->first) % num_buckets_;'''
    bool is_first = (it == buckets_[bucket].first);
    bool is_last = (it == buckets_[bucket].last);
    
    ----- SOME CODE ------

    values_erase(it);
    --size_;
  }

comment:3 Changed 17 months ago by marshall

I agree with Steven on this.

If num_buckets cannot be zero, and "tools like Prevent" report a "divide by Zero" error, then that tool should be fixed.

Not the Boost code (which is not in error).

comment:4 follow-up: ↓ 5 Changed 17 months ago by Gaurav Gupta <g.gupta@…>

What about code block inside void erase(iterator it) method?

In this num_buckets_ can be zero?

comment:5 in reply to: ↑ 4 Changed 17 months ago by steven_watanabe

Replying to Gaurav Gupta <g.gupta@…>:

What about code block inside void erase(iterator it) method?

In this num_buckets_ can be zero?

Nope.

Pre: it != end() Therefore: !empty() Therefore: num_buckets_ != 0

comment:6 Changed 16 months ago by chris_kohlhoff

I am happy to add an additional assert to those two locations, to confirm that num_buckets != 0, if that will silence the tool. Does it?

comment:7 Changed 16 months ago by Gaurav Gupta <g.gupta@…>

Yes, Please add assert. thank you.

comment:8 Changed 11 months ago by chris_kohlhoff

(In [84485]) Add assertions that num_buckets_ is non-zero. Refs #7739

comment:9 Changed 11 months ago by chris_kohlhoff

  • Status changed from new to closed
  • Resolution set to fixed

(In [84530]) Merge from trunk. Fixes #8421, #8602, #7739, #8613, #7939.


r84482 | chris_kohlhoff | 2013-05-25 21:35:10 +1000 (Sat, 25 May 2013) | 1 line

Fix potential deadlock in signal_set implementation.


r84483 | chris_kohlhoff | 2013-05-25 21:38:01 +1000 (Sat, 25 May 2013) | 1 line

Fix error in acceptor example. Refs #8421


r84484 | chris_kohlhoff | 2013-05-25 21:41:19 +1000 (Sat, 25 May 2013) | 1 line

Fix waitable timer documentation. Refs #8602


r84485 | chris_kohlhoff | 2013-05-25 21:46:20 +1000 (Sat, 25 May 2013) | 1 line

Add assertions that num_buckets_ is non-zero. Refs #7739


r84486 | chris_kohlhoff | 2013-05-25 21:50:52 +1000 (Sat, 25 May 2013) | 8 lines

Automatically disable SSL compression.

To mitigate the risk of certain attacks, SSL compression is now disabled by default. To enable, you can use the new ssl::context::clear_options() function like so:

my_context.clear_options(asio::ssl::context::no_compression);


r84487 | chris_kohlhoff | 2013-05-25 21:52:54 +1000 (Sat, 25 May 2013) | 3 lines

Fix bug on Windows where certain operations might generate an error_code with an invalid (i.e. NULL) error_category. Refs #8613


r84488 | chris_kohlhoff | 2013-05-25 21:57:36 +1000 (Sat, 25 May 2013) | 1 line

Fix problem in #warning directive. Refs #7939


r84492 | chris_kohlhoff | 2013-05-25 22:35:43 +1000 (Sat, 25 May 2013) | 2 lines

Fix potential data race due to reading the reactor pointer outside the lock.


r84494 | chris_kohlhoff | 2013-05-25 23:03:48 +1000 (Sat, 25 May 2013) | 1 line

Regenerate documentation.


r84529 | chris_kohlhoff | 2013-05-27 22:17:19 +1000 (Mon, 27 May 2013) | 1 line

Add documentation for new features.


View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.