This is the mail archive of the libstdc++@gcc.gnu.org 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]
Other format: [Raw text]

Re: [v3] char_traits error checking


On Tue, Mar 12, 2002 at 04:18:57PM +0000, Nathan Myers wrote:
> On Tue, Mar 12, 2002 at 10:49:36AM -0500, Phil Edwards wrote:
> > 
> > I looked at this as part of PR 5258.  The way the ctors are arranged in
> > basic_string.tcc doesn't seem to give us any opportunities to do checking on
> > the arguments:  the string data is built (and dereferenced) in the member
> > init list, so no "if NULL throw logic_error" is possible unless we break
> > that out into a ctor body.
> 
> Not a problem -- change ": member(p)" to ": member(__throw_if_null(p))".

True, of course.  I was hoping to make this as fast as possible, given
that this is an extremely common code path.

There's a suggested patch at the end.  This bootstraps and DTRT in a quick
test, and the resulting text in the assertion is pretty straightforward
("__s != __null") even for beginners.

And no overhead for production code, assuming that

    ({ foo; })

generates the same assembly as

    foo;


> > As for the member functions, if we make "no NULL pointers" a string class
> > invariant / postcondition, those problems are neatly sidestepped.
> 
> For string, it's worth giving a decent diagnostic for null pointers
> even though we don't have to.  Beginners rely very heavily on string.

That's what I meant by postcondition.


Phil


Index: basic_string.tcc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/include/bits/basic_string.tcc,v
retrieving revision 1.21
diff -u -3 -p -r1.21 basic_string.tcc
--- basic_string.tcc	8 Mar 2002 06:05:18 -0000	1.21
+++ basic_string.tcc	12 Mar 2002 18:03:30 -0000
@@ -213,13 +213,15 @@ 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 + __n, __a), __a)
+    : _M_dataplus(_S_construct(({assert(__s != NULL); __s;}),
+                               __s + __n, __a), __a)
     { }
 
   template<typename _CharT, typename _Traits, typename _Alloc>
     basic_string<_CharT, _Traits, _Alloc>::
     basic_string(const _CharT* __s, const _Alloc& __a)
-    : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a)
+    : _M_dataplus(_S_construct(({assert(__s != NULL); __s;}),
+                               __s + traits_type::length(__s), __a), __a)
     { }
 
   template<typename _CharT, typename _Traits, typename _Alloc>


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