Permalink
Browse files

Illegal read from istream into string

My 'optimization' for read from istream into
string

  string str;

  str.resize( sz );
  s.read( const_cast<char*>(str.data()), sz );

may lead to wrong data in str for some string
implementations (like ref-counter implementation,
as in libstdc++).
  • Loading branch information...
1 parent 8a47fb2 commit 406b24b83a81ba4ea88f4598f024a7767cb8bc34 @ptr committed Mar 14, 2011
Showing with 23 additions and 23 deletions.
  1. +4 −4 explore/lib/stem/EvPack.cc
  2. +4 −3 explore/lib/stem/NetTransport.cc
  3. +4 −9 explore/lib/yard/btree.cc
  4. +11 −7 explore/lib/yard/yard.cc
@@ -1,7 +1,7 @@
-// -*- C++ -*- Time-stamp: <09/09/18 09:19:59 ptr>
+// -*- C++ -*- Time-stamp: <2011-03-14 17:14:48 ptr>
/*
- * Copyright (c) 1997-1999, 2002-2003, 2005-2006, 2008-2009
+ * Copyright (c) 1997-1999, 2002-2003, 2005-2006, 2008-2011
* Petr Ovtchenkov
*
* Copyright (c) 1999-2001
@@ -33,8 +33,8 @@ void __pack_base::__unpack( istream& s, string& str )
sz = from_net( sz );
str.erase();
if ( sz > 0 ) {
- str.resize( sz );
- s.read( const_cast<char*>(str.data()), sz );
+ str.reserve( sz );
+ copy_n( istreambuf_iterator<char>(s), sz, back_inserter(str) );
}
}
}
@@ -1,4 +1,4 @@
-// -*- C++ -*- Time-stamp: <10/03/08 13:24:54 ptr>
+// -*- C++ -*- Time-stamp: <2011-03-14 17:16:11 ptr>
/*
*
@@ -281,8 +281,9 @@ bool NetTransport_base::pop( Event& _rs )
string& str = _rs.value();
str.erase();
- str.resize( sz );
- net.read( const_cast<char*>(str.data()), sz );
+ str.reserve( sz );
+ copy_n( istreambuf_iterator<char>(net), sz, back_inserter(str) );
+
#ifdef __FIT_STEM_TRACE
try {
lock_guard<mutex> lk(manager()->_lock_tr);
View
@@ -1,4 +1,4 @@
-// -*- C++ -*- Time-stamp: <2011-03-04 16:26:43 ptr>
+// -*- C++ -*- Time-stamp: <2011-03-14 19:18:16 ptr>
/*
*
@@ -45,12 +45,6 @@ void write_data( std::fstream& file, BTree::off_type address, const char* data,
file.write( data, size );
}
-void get_data( std::fstream& file, BTree::off_type address, char* data, std::streamsize size )
-{
- file.seekg( address );
- file.read( data, size );
-}
-
const BTree::key_type BTree::upper_key_bound = { {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff} };
const BTree::key_type BTree::lower_key_bound = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
const std::pair<BTree::key_type,BTree::key_type> BTree::keys_range = { BTree::lower_key_bound, BTree::upper_key_bound };
@@ -479,10 +473,11 @@ std::string BTree::operator []( const key_type& key ) const throw(std::invalid_a
throw std::invalid_argument( "bad key" );
}
- std::string s( i->second.size, '\0' );
+ std::string s;
+ s.reserve( i->second.size );
file_.seekg( i->second.address );
- file_.read( const_cast<char*>(s.data()), i->second.size );
+ copy_n( istreambuf_iterator<char>(file_), i->second.size, back_inserter(s) );
if ( file_.fail() ) {
throw ios_base::failure( "BTree io operation fail" );
View
@@ -1,4 +1,4 @@
-// -*- C++ -*- Time-stamp: <2011-03-03 20:46:33 ptr>
+// -*- C++ -*- Time-stamp: <2011-03-14 17:25:31 ptr>
/*
*
@@ -254,8 +254,10 @@ void revision::get_manifest( manifest_type& m, const revision_id_type& rid ) thr
if ( s.fail() ) { // to avoid str.resize() for bad sz
break;
}
- str.resize( sz );
- s.read( const_cast<char*>(str.data()), sz );
+ str.erase();
+ str.reserve( sz );
+ copy_n( istreambuf_iterator<char>(s), sz, back_inserter(str) );
+
s.read( reinterpret_cast<char*>(rev.u.b), sizeof(revision_id_type) );
if ( s.fail() ) {
break;
@@ -303,8 +305,9 @@ void revision::get_diff( diff_type& d, const revision_id_type& rid ) throw( std:
if ( sz == 0 ) {
break;
}
- str.resize( sz );
- s.read( const_cast<char*>(str.data()), sz );
+ str.erase();
+ str.reserve( sz );
+ copy_n( istreambuf_iterator<char>(s), sz, back_inserter(str) );
s.read( reinterpret_cast<char*>(rev.u.b), sizeof(revision_id_type) );
if ( s.fail() ) {
break;
@@ -317,8 +320,9 @@ void revision::get_diff( diff_type& d, const revision_id_type& rid ) throw( std:
if ( s.fail() ) { // to avoid str.resize() for bad sz
break;
}
- str.resize( sz );
- s.read( const_cast<char*>(str.data()), sz );
+ str.erase();
+ str.reserve( sz );
+ copy_n( istreambuf_iterator<char>(s), sz, back_inserter(str) );
s.read( reinterpret_cast<char*>(rev.u.b), sizeof(revision_id_type) );
if ( s.fail() ) {
break;

0 comments on commit 406b24b

Please sign in to comment.