Boost C++ Libraries: Ticket #4410: Sparse/Packed matrix assignment needs type conversion https://svn.boost.org/trac10/ticket/4410 <p> In file <em>boost/detail/matrix_assign.hpp</em> there are two possible source of type-conversion error interesting both <em>sparse</em> and <em>packed</em> matrices. </p> <p> Let: </p> <div class="wiki-code"><div class="code"><pre> <span class="k">typedef</span> <span class="k">typename</span> <span class="n">M</span><span class="o">::</span><span class="n">value_type</span> <span class="n">value_type</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">F</span><span class="o">&lt;</span><span class="k">typename</span> <span class="n">M</span><span class="o">::</span><span class="p">...,</span> <span class="k">typename</span> <span class="n">E</span><span class="o">::</span><span class="n">value_type</span><span class="o">&gt;</span> <span class="n">functor_type</span><span class="p">;</span> </pre></div></div><p> Then: </p> <ol><li><em>Comparison of an <code>E::value_type</code> with an <code>M::value_type</code></em> <div class="wiki-code"><div class="code"><pre> <span class="k">if</span> <span class="p">(</span><span class="n">v</span> <span class="o">!=</span> <span class="n">value_type</span><span class="cm">/*zero*/</span><span class="p">())</span> <span class="c1">// where v is of type E::value_type</span> </pre></div></div></li></ol><blockquote> <p> E.g., <code>E::value_type</code> is <code>std::complex&lt;float&gt;</code> and <code>M::value_type</code> is <code>std::complex&lt;double&gt;</code>. </p> </blockquote> <ol start="2"><li><em>Assignment of an <code>M::value_type</code> to an <code>E::value_type</code></em> <div class="wiki-code"><div class="code"><pre> <span class="n">functor_type</span><span class="o">::</span><span class="n">apply</span><span class="p">(</span><span class="o">*</span><span class="n">it</span><span class="p">,</span> <span class="n">value_type</span><span class="cm">/*zero*/</span><span class="p">());</span> </pre></div></div>E.g., <code>E::value_type</code> is <code>float</code> and <code>M::value_type</code> is <code>std::complex&lt;float&gt;</code>. </li></ol> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4410 Trac 1.4.3 Marco Guazzone <marco.guazzone@…> Thu, 08 Jul 2010 14:55:35 GMT attachment set https://svn.boost.org/trac10/ticket/4410 https://svn.boost.org/trac10/ticket/4410 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">matrix_assign_problem.cpp</span> </li> </ul> <p> A sample program for showing the problem (the program should not compile). </p> Ticket Marco Guazzone <marco.guazzone@…> Thu, 08 Jul 2010 15:02:39 GMT <link>https://svn.boost.org/trac10/ticket/4410#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4410#comment:1</guid> <description> <p> I propose a possible patch (see attachment: <em>matrix_assign-packed_sparse_storage-type_conversion.patch</em>). </p> <p> Essentially, </p> <ol><li>Expressions of the first type might be changed by casting an <code>E::value_type</code> to a <code>M::value_type</code>, like in this way: <div class="wiki-code"><div class="code"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="n">value_type</span><span class="o">&gt;</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">!=</span> <span class="n">value_type</span><span class="cm">/*zero*/</span><span class="p">())</span> </pre></div></div></li></ol><p> Obviously, this does not work when <code>E::value_type</code> and <code>M::value_type</code> are not <em>castable</em> (e.g., <code>std::complex</code> and <code>double</code>, respectively). </p> <ol start="2"><li>Expressions of the second type might be changed in 2 ways: </li></ol><ul><li>Option A (the one used in the proposed patch) <div class="wiki-code"><div class="code"><pre> <span class="k">typedef</span> <span class="k">typename</span> <span class="n">matrix_traits</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;::</span><span class="n">value_type</span> <span class="n">expr_value_type</span><span class="p">;</span> <span class="n">functor_type</span><span class="o">::</span><span class="n">apply</span><span class="p">(</span><span class="o">*</span><span class="n">it</span><span class="p">,</span> <span class="n">expr_value_type</span><span class="cm">/*zero*/</span><span class="p">());</span> <span class="c1">// NOTE: use of E::value_type in place of M::value_type</span> </pre></div></div></li></ul><ul><li>Option B <div class="wiki-code"><div class="code"><pre> <span class="k">typedef</span> <span class="n">F</span><span class="o">&lt;</span><span class="k">typename</span> <span class="n">M</span><span class="o">::</span><span class="p">...,</span> <span class="n">value_type</span><span class="o">&gt;</span> <span class="n">functor_type</span><span class="p">;</span> <span class="c1">// NOTE: use M::value_type instead of E::value_type</span> <span class="n">functor_type</span><span class="o">::</span><span class="n">apply</span><span class="p">(</span><span class="o">*</span><span class="n">it</span><span class="p">,</span> <span class="n">value_type</span><span class="cm">/*zero*/</span><span class="p">());</span> <span class="c1">// unchanged</span> </pre></div></div></li></ul> </description> <category>Ticket</category> </item> <item> <author>Marco Guazzone <marco.guazzone@…></author> <pubDate>Thu, 08 Jul 2010 15:04:09 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4410 https://svn.boost.org/trac10/ticket/4410 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">matrix_assign-packed_sparse_storage-type_conversion.patch</span> </li> </ul> <p> Possible solution. </p> Ticket Marco Guazzone <marco.guazzone@…> Thu, 08 Jul 2010 15:08:40 GMT <link>https://svn.boost.org/trac10/ticket/4410#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4410#comment:2</guid> <description> <p> There is also a companion post un uBLAS ml: </p> <blockquote> <p> <a class="ext-link" href="http://lists.boost.org/MailArchives/ublas/2010/07/4420.php"><span class="icon">​</span>http://lists.boost.org/MailArchives/ublas/2010/07/4420.php</a> </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>David Bellot</dc:creator> <pubDate>Mon, 16 Aug 2010 22:01:18 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/4410#comment:3 https://svn.boost.org/trac10/ticket/4410#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Gunter</span> to <span class="trac-author">David Bellot</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket David Bellot Mon, 16 Aug 2010 22:03:09 GMT <link>https://svn.boost.org/trac10/ticket/4410#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4410#comment:4</guid> <description> <p> applied patch from Marco Guazzone. Should be OK, however it raises a concern about a possible security hole with static_cast&lt;&gt;, only when people are crazy enough to use ublas as a data storage having nothing to do with linear algebra (like I did once ;-) ) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>David Bellot</dc:creator> <pubDate>Mon, 16 Aug 2010 22:12:10 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4410#comment:5 https://svn.boost.org/trac10/ticket/4410#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> Ticket Marco Guazzone <marco.guazzone@…> Tue, 17 Aug 2010 08:30:39 GMT attachment set https://svn.boost.org/trac10/ticket/4410 https://svn.boost.org/trac10/ticket/4410 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_ticket4410.cpp</span> </li> </ul> <p> Test case: test copy-construction/-assignement of a sparse (symmetric) matrix. The test fails to compile if the patch is not applied. </p> Ticket