This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


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

problem with basic_string(const charT*, size_type) ctor



hi -

I've run into what appears to be a problem with the basic_string constructor
basic_string(const charT* s, size_type n).
Here's an example:

---------------------------------------------------------
#include <string>
#include <iostream>

main ()
{
  char s[] = "\000\001\002\003";

  std::string s1 (s, 4);
  std::cout << s1.size () << "\n";
}
---------------------------------------------------------

I expect this program to print `4', but instead, it prints `0'.

Rather than relying on the user-supplied length, the basic_string
constructor is taking the smaller of that length and the length of the
buffer interpreted as a C string.  Since the buffer here starts with
a null, the constructor treats the buffer as having zero length.

But this does not seem to be what the standard requires.  In table 40,
the postcondition for this constructor is defined to be `size() == n',
with no mention made of any dependence of this on the data in the
character array.  So i think that the behavior presently implemented
is wrong --- the length should be taken purely from what the user
supplied.  (I also checked the library issues list, and didn't see
any mention of this topic.)

The member

      basic_string& 
      append(const _CharT* __s, size_type __n)

whose behavior is defined by the standard in terms of this constructor,
also exhibits the same problem.  (Interestingly, other members with
the same style signature -- assign, insert, replace -- do not show this
behavior.)

Appended is a patch.  One of the testsuite programs was explicitly
testing this behavior which i believe to be wrong, so i had to remove
that test to keep from breaking tests.

thanks,
sss


1999-11-27  scott snyder  <snyder@fnal.gov>

	* bits/basic_string.h (append(const _Char*, size_type)): The
	length of the appended string is given exactly by the second arg,
	regardless of the data in the character array.
	* bits/string.tcc (basic_string(const _CharT*, size_type, const
	_Alloc&)): Likewise.
	* testsuite/21_strings/append.cc (test01): Remove erroneous test
	of basic_string::append.


Index: bits/basic_string.h
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/basic_string.h,v
retrieving revision 1.49
diff -u -p -r1.49 basic_string.h
--- basic_string.h	1999/11/02 05:12:01	1.49
+++ basic_string.h	1999/11/27 21:24:10
@@ -478,11 +478,10 @@ namespace std {
       basic_string& 
       append(const _CharT* __s, size_type __n)
       {
-	size_type __size =  min(traits_type::length(__s), __n);
-	size_type __len = __size + this->size();
+	size_type __len = __n + this->size();
 	if (__len > this->capacity())
 	  this->reserve(__len); 
-	return this->replace(_M_iend(), _M_iend(), __s, __s + __size); 
+	return this->replace(_M_iend(), _M_iend(), __s, __s + __n); 
       }
 
       basic_string& 
Index: bits/string.tcc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/string.tcc,v
retrieving revision 1.54
diff -u -p -r1.54 string.tcc
--- string.tcc	1999/07/23 09:27:42	1.54
+++ string.tcc	1999/11/27 21:24:38
@@ -196,7 +196,7 @@ namespace std
   template<typename _CharT, typename _Traits, typename _Alloc>
     basic_string<_CharT, _Traits, _Alloc>::
     basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
-    : _M_dataplus(_S_construct(__s, __s + min(traits_type::length(__s), __n), 
+    : _M_dataplus(_S_construct(__s, __s + __n, 
 			       __a), __a)
     { }
 
Index: testsuite/21_strings/append.cc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/testsuite/21_strings/append.cc,v
retrieving revision 1.2
diff -u -p -r1.2 append.cc
--- append.cc	1999/07/14 09:32:29	1.2
+++ append.cc	1999/11/27 21:25:00
@@ -121,9 +121,11 @@ bool test01(void)
   str06.append("corpus, ", 6);
   test &= str06 == "corpus, corpus";
 
+#if 0
   str06 = str02;
   str06.append("corpus, ", 12);
   test &= str06 == "corpus, corpus, ";
+#endif
 
 
   // string& append(size_type n, char c)

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