This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/13650] New: string::compare should not (always) use traits_type::length()


The following function is incorrect:
  template<typename _CharT, typename _Traits, typename _Alloc>
  int
  basic_string <_CharT, _Traits, _Alloc>::
    compare(size_type __pos, size_type __n1, const _CharT* __s,
	    size_type __n2) const

The implementation should only be trying to calculate the length
of __s when __n2 is npos, because __s may have '\0' characters
embedded in it; i.e. it may not be a NTBS.
The use of traits_type::length is still appropriate when __n2
is npos.

Proposed solution:

--- basic_string.tcc.old        2004-01-12 17:12:57.000000000 +1300
+++ basic_string.tcc    2004-01-12 17:13:01.000000000 +1300
@@ -1033,7 +1033,9 @@
       if (__pos > __size)
        __throw_out_of_range("basic_string::compare");

-      size_type __osize = std::min(traits_type::length(__s), __n2);
+      size_type __osize = __n2;
+      if (__n2 == npos)
+          __osize = traits_type::length(__s);
       size_type __rsize = std::min(size_type(__size - __pos), __n1);
       size_type __len = std::min(__rsize, __osize);
       int __r = traits_type::compare(_M_data() + __pos, __s, __len);

-- 
           Summary: string::compare should not (always) use
                    traits_type::length()
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jlabusch at eservglobal dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13650


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]