Opened 10 years ago

Closed 9 years ago

#7739 closed Bugs (fixed)

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 (1)

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

Download all attachments as: .zip

Change History (10)

by Gaurav Gupta <g.gupta@…>, 10 years ago

Attachment: hash_map.hpp_patch added

Fix for the Divide by zero error in hash_map.hpp

comment:1 by Steven Watanabe, 10 years ago

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

in reply to:  1 comment:2 by Gaurav Gupta <g.gupta@…>, 10 years ago

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 by Marshall Clow, 10 years ago

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 by Gaurav Gupta <g.gupta@…>, 10 years ago

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

In this num_buckets_ can be zero?

in reply to:  4 comment:5 by Steven Watanabe, 10 years ago

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 by chris_kohlhoff, 10 years ago

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 by Gaurav Gupta <g.gupta@…>, 10 years ago

Yes, Please add assert. thank you.

comment:8 by chris_kohlhoff, 9 years ago

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

comment:9 by chris_kohlhoff, 9 years ago

Resolution: fixed
Status: newclosed

(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.


Note: See TracTickets for help on using tickets.