Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76096 - in trunk: boost libs/conversion/test
From: antoshkka_at_[hidden]
Date: 2011-12-21 12:03:54


Author: apolukhin
Date: 2011-12-21 12:03:52 EST (Wed, 21 Dec 2011)
New Revision: 76096
URL: http://svn.boost.org/trac/boost/changeset/76096

Log:
Fixes #6298
Fixes #6264
Text files modified:
   trunk/boost/lexical_cast.hpp | 7 ++
   trunk/libs/conversion/test/lexical_cast_empty_input_test.cpp | 81 +++++++++++++++++++++++++++++++++++----
   2 files changed, 76 insertions(+), 12 deletions(-)

Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2011-12-21 12:03:52 EST (Wed, 21 Dec 2011)
@@ -586,7 +586,7 @@
             --end;
             value = 0;
 
- if ( *end < czero || *end >= czero + 10 || begin > end)
+ if (begin > end || *end < czero || *end >= czero + 10)
                 return false;
             value = *end - czero;
             --end;
@@ -684,6 +684,7 @@
             , const CharT opening_brace, const CharT closing_brace)
         {
             using namespace std;
+ if (begin == end) return false;
             const CharT minus = lcast_char_constants<CharT>::minus;
             const CharT plus = lcast_char_constants<CharT>::plus;
             const int inifinity_size = 8;
@@ -1183,7 +1184,7 @@
                 bool const result = !(stream << input).fail();
                 start = stringbuffer.pbase();
                 finish = stringbuffer.pptr();
- return result && (start != finish);
+ return result;
             }
 
             template <class T>
@@ -1358,6 +1359,7 @@
             template <typename Type>
             bool shr_unsigned(Type& output)
             {
+ if (start == finish) return false;
                 CharT const minus = lcast_char_constants<CharT>::minus;
                 CharT const plus = lcast_char_constants<CharT>::plus;
                 bool has_minus = false;
@@ -1392,6 +1394,7 @@
             template <typename Type>
             bool shr_signed(Type& output)
             {
+ if (start == finish) return false;
                 CharT const minus = lcast_char_constants<CharT>::minus;
                 CharT const plus = lcast_char_constants<CharT>::plus;
                 typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;

Modified: trunk/libs/conversion/test/lexical_cast_empty_input_test.cpp
==============================================================================
--- trunk/libs/conversion/test/lexical_cast_empty_input_test.cpp (original)
+++ trunk/libs/conversion/test/lexical_cast_empty_input_test.cpp 2011-12-21 12:03:52 EST (Wed, 21 Dec 2011)
@@ -24,27 +24,86 @@
 
 void test_empty_iterator_range()
 {
- boost::iterator_range<const char*> v;
+ boost::iterator_range<const char*> v;
     BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
     BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
     BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
     BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
     BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<std::string>(v), bad_lexical_cast);
+ BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
     BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
     BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
 }
 
 void test_empty_string()
 {
- BOOST_CHECK_THROW(lexical_cast<int>(std::string()), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<float>(std::string()), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<double>(std::string()), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<long double>(std::string()), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<unsigned int>(std::string()), bad_lexical_cast);
- BOOST_CHECK_EQUAL(lexical_cast<std::string>(std::string()), std::string());
- BOOST_CHECK_THROW(lexical_cast<unsigned short>(std::string()), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<char>(std::string()), bad_lexical_cast);
+ std::string v;
+ BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
+ BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
+ BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
+}
+
+struct Escape
+{
+ Escape(const std::string& s)
+ : str_(s)
+ {}
+
+ std::string str_;
+};
+
+inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
+{
+ return o << rhs.str_;
+}
+
+void test_empty_user_class()
+{
+ Escape v("");
+ BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
+ BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
+ BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
+}
+
+namespace std {
+inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v)
+{
+ std::ostream_iterator<long> it(out);
+ std::copy(v.begin(), v.end(), it);
+ assert(out);
+ return out;
+}
+}
+
+void test_empty_vector()
+{
+ std::vector<long> v;
+ BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
+ BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
+ BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
 }
 
 unit_test::test_suite *init_unit_test_suite(int, char *[])
@@ -53,6 +112,8 @@
         BOOST_TEST_SUITE("lexical_cast. Empty input unit test");
     suite->add(BOOST_TEST_CASE(&test_empty_iterator_range));
     suite->add(BOOST_TEST_CASE(&test_empty_string));
+ suite->add(BOOST_TEST_CASE(&test_empty_user_class));
+ suite->add(BOOST_TEST_CASE(&test_empty_vector));
 
     return suite;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk