This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]

c++ string.substr() error checking


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all

string.substr() doesn't check for errors, which results in SIGSEVs if 
pos is out of range. The code below should fix this.

basic_string substr (size_type pos = 0, size_type n = npos) const
{
  if (pos >= 0 && pos < this->length()) {
    if (n != npos && n > this->length() - pos) {
      n = this->length() - pos;
    }
    return basic_string (*this, pos, n);
  }
  return basic_string ();
}


Norbert

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE+xq2vxMLs5v5/7eARArtAAJ91gBgw6YUEj/z4M8az8z3LVu9UuwCfWbfe
d9LdloYGKUo5p6aAstubric=
=AZtS
-----END PGP SIGNATURE-----


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