libstdc++/1710: strings.find_last_of() function works only if I explicitly reference the strings.c_str() function after calling it

mdh_gcc@hotmail.com mdh_gcc@hotmail.com
Sun Apr 1 00:00:00 GMT 2001


>Number:         1710
>Category:       libstdc++
>Synopsis:       strings.find_last_of() function works only if I explicitly reference the strings.c_str() function after calling it
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Jan 19 18:36:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Mike Henderson
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
GNU C++ version 2.95.2 19991024 (release) (sparc-sun-solaris2.6) compiled by GNU C version 2.95.2 19991024 (release).

Command line was "g++ -Wall -o bugmain bugmain.cpp"
>Description:
I'm trimming trailing slashes from a string.
The strings.find_last_of() function seems to work only if
I explicitly reference the strings.c_str() function
after calling strings.erase().

This is the output with the cout commented out

 mdh $ HOME=${HOME}///// ./testBug
home.value now /userhome/mdhender/////'
home.value now /userhome/mdhender////'

This is the output with the cout uncommented

mdh $ HOME=${HOME}///// ./testBug
home.value now /userhome/mdhender/////'
value [/userhome/mdhender////]
value [/userhome/mdhender///]
value [/userhome/mdhender//]
value [/userhome/mdhender/]
value [/userhome/mdhender]
home.value now /userhome/mdhender'

In the second case, the .find_last_of() function seems to work properly.
>How-To-Repeat:
//-----------------------------------------------------------------------
#include <string>

class TMdhEnv {
public:
   TMdhEnv(char *envVarName);
   void RTrim(const string& toTrim);

//private:
   string name;
   string value;
};

TMdhEnv::TMdhEnv(char *envVarName)
{
   name = envVarName;

   // get value if available
   char *val = getenv(envVarName);
   if (val) {
      value = val;
   }
}

void
TMdhEnv::RTrim(const string& toTrim)
{
   string::size_type lastOccurance = value.find_last_of(toTrim);
   while (value[lastOccurance + 1] == '\0') {
      value.erase(lastOccurance);
//    cout << "value [" << value.c_str() << "]\n";
      lastOccurance = value.find_last_of(toTrim);
   }
}

void
main(int argc, char *argv[])
{
   // get the environment variable HOME
   TMdhEnv home("HOME");

   if (home.value == "") {
      cout << "error: invalid HOME directory\n";
   } else {
      //
      // just for looks, let's trim trailing slashes from the directory
      //
      cout << "home.value now " << home.value << "'\n";
      home.RTrim("/");
      cout << "home.value now " << home.value << "'\n";
   }
   exit(0);
}
//-----------------------------------------------------------------------
>Fix:
Uncomment the cout line in the RTrim function

void
TMdhEnv::RTrim(const string& toTrim)
{
   string::size_type lastOccurance = value.find_last_of(toTrim);
   while (value[lastOccurance + 1] == '\0') {
      value.erase(lastOccurance);
      cout << "value [" << value.c_str() << "]\n";
      lastOccurance = value.find_last_of(toTrim);
   }
}

>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the Gcc-prs mailing list