Boost C++ Libraries: Ticket #7739: Divide by zero error in hash_map.hpp https://svn.boost.org/trac10/ticket/7739 <p> In the code snippet, while doing <strong>calculate_hash_value(iter-&gt;first) % num_buckets_</strong> If value of <strong>num_buckets_ is 0</strong>, then it leads to Divide By zero error. </p> <p> void rehash(std::size_t num_buckets) </p> <blockquote> <p> { </p> <blockquote> <p> if (num_buckets == num_buckets_) </p> <blockquote> <p> return; </p> </blockquote> <p> num_buckets_ = num_buckets; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> iterator end_iter = values_.end(); </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> ----- SOME CODE ------------ </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> <em> Put all values back into the hash. iterator iter = values_.begin(); while (iter != end_iter) { </em></p> <blockquote> <p> <strong>std::size_t bucket = calculate_hash_value(iter-&gt;first) % num_buckets_;</strong> </p> </blockquote> </blockquote> </blockquote> <p> if (buckets_[bucket].last == end_iter) </p> <blockquote> <p> { </p> <blockquote> <p> buckets_[bucket].first = buckets_[bucket].last = iter++; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> ------- SOME CODE ----------- </p> </blockquote> <p> Attached patch is the fix for it. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7739 Trac 1.4.3 Gaurav Gupta <g.gupta@…> Tue, 27 Nov 2012 04:11:17 GMT attachment set https://svn.boost.org/trac10/ticket/7739 https://svn.boost.org/trac10/ticket/7739 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">hash_map.hpp_patch</span> </li> </ul> <p> Fix for the Divide by zero error in hash_map.hpp </p> Ticket Steven Watanabe Tue, 27 Nov 2012 18:18:06 GMT <link>https://svn.boost.org/trac10/ticket/7739#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:1</guid> <description> <p> This is not a bug, as num_buckets cannot be zero in the first place. </p> </description> <category>Ticket</category> </item> <item> <author>Gaurav Gupta <g.gupta@…></author> <pubDate>Wed, 28 Nov 2012 05:29:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7739#comment:1" title="Comment 1">steven_watanabe</a>: </p> <blockquote class="citation"> <p> This is not a bug, as num_buckets cannot be zero in the first place. </p> </blockquote> <p> Its True, num_buckets canot be zero, but this check is safe here, as many tools (like Prevent )report this as <strong>DIVIDE By Zero</strong> error. </p> <p> Also In my patch , I have provided fix at two places, other is in following code snippet. </p> <pre class="wiki">void erase(iterator it) { BOOST_ASSERT(it != values_.end()); '''size_t bucket = calculate_hash_value(it-&gt;first) % num_buckets_;''' bool is_first = (it == buckets_[bucket].first); bool is_last = (it == buckets_[bucket].last); ----- SOME CODE ------ values_erase(it); --size_; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Wed, 28 Nov 2012 14:53:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:3</guid> <description> <p> I agree with Steven on this. </p> <p> If num_buckets cannot be zero, and "tools like Prevent" report a "divide by Zero" error, then that tool should be fixed. </p> <p> Not the Boost code (which is not in error). </p> </description> <category>Ticket</category> </item> <item> <author>Gaurav Gupta <g.gupta@…></author> <pubDate>Thu, 29 Nov 2012 05:31:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:4</guid> <description> <p> What about code block inside void erase(iterator it) method? </p> <p> In this num_buckets_ can be zero? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Fri, 30 Nov 2012 06:40:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/7739#comment:4" title="Comment 4">Gaurav Gupta &lt;g.gupta@…&gt;</a>: </p> <blockquote class="citation"> <p> What about code block inside void erase(iterator it) method? </p> <p> In this num_buckets_ can be zero? </p> </blockquote> <p> Nope. </p> <p> Pre: it != end() Therefore: !empty() Therefore: num_buckets_ != 0 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Sat, 29 Dec 2012 11:12:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:6</guid> <description> <p> 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? </p> </description> <category>Ticket</category> </item> <item> <author>Gaurav Gupta <g.gupta@…></author> <pubDate>Wed, 02 Jan 2013 05:09:16 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:7</guid> <description> <p> Yes, Please add assert. thank you. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Sat, 25 May 2013 11:46:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7739#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7739#comment:8</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/84485" title="Add assertions that num_buckets_ is non-zero. Refs #7739">[84485]</a>) Add assertions that num_buckets_ is non-zero. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7739" title="#7739: Bugs: Divide by zero error in hash_map.hpp (closed: fixed)">#7739</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>chris_kohlhoff</dc:creator> <pubDate>Mon, 27 May 2013 12:36:47 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7739#comment:9 https://svn.boost.org/trac10/ticket/7739#comment:9 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/84530" title="Merge from trunk. Fixes #8421, #8602, #7739, #8613, #7939. ...">[84530]</a>) Merge from trunk. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8421" title="#8421: Bugs: docs show wrong overload (closed: fixed)">#8421</a>, <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8602" title="#8602: Bugs: Documentation references nonexistent boost::asio::waitable_timer (closed: fixed)">#8602</a>, <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7739" title="#7739: Bugs: Divide by zero error in hash_map.hpp (closed: fixed)">#7739</a>, <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8613" title="#8613: Bugs: [Windows] ... (closed: fixed)">#8613</a>, <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7939" title="#7939: Patches: remove warning/error in asio/impl/src.cpp (closed: fixed)">#7939</a>. </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84482" title="Fix potential deadlock in signal_set implementation.">r84482</a> | chris_kohlhoff | 2013-05-25 21:35:10 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Fix potential deadlock in signal_set implementation. </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84483" title="Fix error in acceptor example. Refs #8421">r84483</a> | chris_kohlhoff | 2013-05-25 21:38:01 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Fix error in acceptor example. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8421" title="#8421: Bugs: docs show wrong overload (closed: fixed)">#8421</a> </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84484" title="Fix waitable timer documentation. Refs #8602">r84484</a> | chris_kohlhoff | 2013-05-25 21:41:19 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Fix waitable timer documentation. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8602" title="#8602: Bugs: Documentation references nonexistent boost::asio::waitable_timer (closed: fixed)">#8602</a> </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84485" title="Add assertions that num_buckets_ is non-zero. Refs #7739">r84485</a> | chris_kohlhoff | 2013-05-25 21:46:20 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Add assertions that num_buckets_ is non-zero. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7739" title="#7739: Bugs: Divide by zero error in hash_map.hpp (closed: fixed)">#7739</a> </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84486" title="Automatically disable SSL compression. To mitigate the risk of ...">r84486</a> | chris_kohlhoff | 2013-05-25 21:50:52 +1000 (Sat, 25 May 2013) | 8 lines </p> <p> Automatically disable SSL compression. </p> <p> 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: </p> <blockquote> <p> my_context.clear_options(asio::ssl::context::no_compression); </p> </blockquote> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84487" title="Fix bug on Windows where certain operations might generate an ...">r84487</a> | chris_kohlhoff | 2013-05-25 21:52:54 +1000 (Sat, 25 May 2013) | 3 lines </p> <p> Fix bug on Windows where certain operations might generate an error_code with an invalid (i.e. NULL) error_category. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8613" title="#8613: Bugs: [Windows] ... (closed: fixed)">#8613</a> </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84488" title="Fix problem in #warning directive. Refs #7939">r84488</a> | chris_kohlhoff | 2013-05-25 21:57:36 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Fix problem in #warning directive. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7939" title="#7939: Patches: remove warning/error in asio/impl/src.cpp (closed: fixed)">#7939</a> </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84492" title="Fix potential data race due to reading the reactor pointer outside the ...">r84492</a> | chris_kohlhoff | 2013-05-25 22:35:43 +1000 (Sat, 25 May 2013) | 2 lines </p> <p> Fix potential data race due to reading the reactor pointer outside the lock. </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84494" title="Regenerate documentation.">r84494</a> | chris_kohlhoff | 2013-05-25 23:03:48 +1000 (Sat, 25 May 2013) | 1 line </p> <p> Regenerate documentation. </p> <hr /> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/84529" title="Add documentation for new features.">r84529</a> | chris_kohlhoff | 2013-05-27 22:17:19 +1000 (Mon, 27 May 2013) | 1 line </p> <p> Add documentation for new features. </p> <hr /> Ticket