Modify

Ticket #4410 (closed Patches: worksforme)

Opened 4 years ago

Last modified 4 years ago

Sparse/Packed matrix assignment needs type conversion

Reported by: Marco Guazzone <marco.guazzone@…> Owned by: david.bellot
Milestone: To Be Determined Component: uBLAS
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

In file boost/detail/matrix_assign.hpp there are two possible source of type-conversion error interesting both sparse and packed matrices.

Let:

  typedef typename M::value_type value_type;
  typedef F<typename M::..., typename E::value_type> functor_type;

Then:

  1. Comparison of an E::value_type with an M::value_type
      if (v != value_type/*zero*/()) // where v is of type E::value_type
    

E.g., E::value_type is std::complex<float> and M::value_type is std::complex<double>.

  1. Assignment of an M::value_type to an E::value_type
      functor_type::apply(*it, value_type/*zero*/());
    
    E.g., E::value_type is float and M::value_type is std::complex<float>.

Attachments

matrix_assign_problem.cpp Download (587 bytes) - added by Marco Guazzone <marco.guazzone@…> 4 years ago.
A sample program for showing the problem (the program should not compile).
matrix_assign-packed_sparse_storage-type_conversion.patch Download (13.4 KB) - added by Marco Guazzone <marco.guazzone@…> 4 years ago.
Possible solution.
test_ticket4410.cpp Download (799 bytes) - added by Marco Guazzone <marco.guazzone@…> 4 years ago.
Test case: test copy-construction/-assignement of a sparse (symmetric) matrix. The test fails to compile if the patch is not applied.

Change History

Changed 4 years ago by Marco Guazzone <marco.guazzone@…>

A sample program for showing the problem (the program should not compile).

comment:1 Changed 4 years ago by Marco Guazzone <marco.guazzone@…>

I propose a possible patch (see attachment: matrix_assign-packed_sparse_storage-type_conversion.patch).

Essentially,

  1. Expressions of the first type might be changed by casting an E::value_type to a M::value_type, like in this way:
      if (static_cast<value_type>(v) != value_type/*zero*/())
    

Obviously, this does not work when E::value_type and M::value_type are not castable (e.g., std::complex and double, respectively).

  1. Expressions of the second type might be changed in 2 ways:
  • Option A (the one used in the proposed patch)
      typedef typename matrix_traits<E>::value_type expr_value_type;
      functor_type::apply(*it, expr_value_type/*zero*/()); // NOTE: use of E::value_type in place of M::value_type
    
  • Option B
      typedef F<typename M::..., value_type> functor_type; // NOTE: use M::value_type instead of E::value_type
      functor_type::apply(*it, value_type/*zero*/()); // unchanged
    

Changed 4 years ago by Marco Guazzone <marco.guazzone@…>

Possible solution.

comment:2 Changed 4 years ago by Marco Guazzone <marco.guazzone@…>

There is also a companion post un uBLAS ml:

 http://lists.boost.org/MailArchives/ublas/2010/07/4420.php

comment:3 Changed 4 years ago by david.bellot

  • Owner changed from guwi17 to david.bellot
  • Status changed from new to assigned

comment:4 Changed 4 years ago by david.bellot

applied patch from Marco Guazzone. Should be OK, however it raises a concern about a possible security hole with static_cast<>, only when people are crazy enough to use ublas as a data storage having nothing to do with linear algebra (like I did once ;-) )

comment:5 Changed 4 years ago by david.bellot

  • Status changed from assigned to closed
  • Resolution set to worksforme

Changed 4 years ago by Marco Guazzone <marco.guazzone@…>

Test case: test copy-construction/-assignement of a sparse (symmetric) matrix. The test fails to compile if the patch is not applied.

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.