Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost tokenizer does not work, and shows up invalid read of size on valgrind
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-07-14 12:32:09


AMDG

Avi Bahra wrote:
> Using latest boost version 39 on gcc 4.2.1 on SUSE linux
> I am trying to load a file, and then split the lines into a vector of
> strings.
> However when this is run, it showed that the last string was corrupt.
> When I ran this with valgrind, the very first error shows an invalid read of
> size 1, in guts of boost::char_separator.
>
> <snip>
>
> here is program:
>
> BOOST_AUTO_TEST_CASE( test_log_append ) {
>
> string logFile = "test/logfile.txt";
>
> // Load the log file into a vector, of strings, and test content
> ifstream ifs(logFile.c_str());
> BOOST_REQUIRE_MESSAGE(ifs, "Could not open log file\n");
>
> stringstream ss; ss << ifs.rdbuf(); // Read the
> whole file into a string
> char_separator<char> sep("\n"); // Split the file
> content unix=\n pc =\n\r
> typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
> tokenizer tokens(ss.str(),
> sep);
> // <<<<<<<< valgrind barfs here
>

ss.str() returns a temporary std::string.
boost::tokenizer stores a reference to the string.
The temporary string is destroyed and tokenizer is left with a dangling
reference.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net