Opened 12 years ago

Closed 12 years ago

#4410 closed Patches (worksforme)

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

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

Download all attachments as: .zip

Change History (8)

by Marco Guazzone <marco.guazzone@…>, 12 years ago

Attachment: matrix_assign_problem.cpp added

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

comment:1 by Marco Guazzone <marco.guazzone@…>, 12 years ago

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
    

by Marco Guazzone <marco.guazzone@…>, 12 years ago

Possible solution.

comment:2 by Marco Guazzone <marco.guazzone@…>, 12 years ago

There is also a companion post un uBLAS ml:

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

comment:3 by David Bellot, 12 years ago

Owner: changed from Gunter to David Bellot
Status: newassigned

comment:4 by David Bellot, 12 years ago

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 by David Bellot, 12 years ago

Resolution: worksforme
Status: assignedclosed

by Marco Guazzone <marco.guazzone@…>, 12 years ago

Attachment: test_ticket4410.cpp added

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

Note: See TracTickets for help on using tickets.