[C++-sig] type comparison in indexing_suite_detail.hpp

Andreas Beyer beyer at imb-jena.de
Wed Sep 22 15:42:30 CEST 2004


Hi,

I am using boost_python from the CVS-server (today, Sep. 22, 2004) with 
MinGW on WindowsXP.

When compiling the example for the vector_indexing_suite I get the 
following two warnings:

[...]
..\..\..\libs\python\test\vector_indexing_suite.cpp:54:   instantiated 
from here
C:\boost-cvs-22092004/boost/python/suite/indexing/detail/indexing_suite_detail.hpp:591: 
warning: comparison between signed and unsigned integer expressions

[...]
..\..\..\libs\python\test\vector_indexing_suite.cpp:54:   instantiated 
from here
C:\boost-cvs-22092004/boost/python/suite/indexing/detail/indexing_suite_detail.hpp:605: 
warning: comparison between signed and unsigned integer expressions



The follwoing code piece from indexing_suite_detail.hpp causes the 
trouble (from function base_get_slice_data()):

  [...]
  if (Py_None == slice->start) { // this is line 582
         from_ = min_index;
  }
  else {
        long from = extract<long>( slice->start);
        if (from < 0) // Negative slice index
             from += max_index;
        if (from < 0) // Clip lower bounds to zero
             from = 0;
        if (from > max_index) // <<< PROBLEM, line 591
            from = max_index;
        from_ = boost::numeric_cast<Index>(from);
   }

   if (Py_None == slice->stop) {
        to_ = max_index;
   }
   else {
        long to = extract<long>( slice->stop);
        if (to < 0)
             to += max_index;
        if (to < 0)
             to = 0;
        if (to > max_index)  // <<< PROBLEM, line 605
             to = max_index;
        to_ = boost::numeric_cast<Index>(to);
   }
   [...]

In the two indicated lines values of type 'Index' (which is unsigned) 
are compared to long. We know at that point, that 'from' and 'to' are 
not negative. So I suggest to change the order of the cast and of the 
comparison to 'max_index' as follows:

    [...]
    if (from < 0) // Clip lower bounds to zero
        from = 0;
    from_ = boost::numeric_cast<Index>(from);
    if (from_ > max_index) // Clip upper bounds to max_index.
        from_ = max_index;


    [...]
    if (to < 0)
          to = 0;
    to_ = boost::numeric_cast<Index>(to);
          if (to_ > max_index)
    to_ = max_index;
    [...]


This seems to work fine.

	Andreas

Attached is my version of indexing_suite_detail.hpp.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: indexing_suite_detail.hpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20040922/e6aa1132/attachment.txt>


More information about the Cplusplus-sig mailing list