Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

This program does not typically raise an exception and may be exploited to overwrite memory at a specified location.

Compliant Solution

Wiki MarkupThis compliant solution uses the {{basic_string at()}} method, which behaves in a similar fashion to the index {{operator\[\]}} but throws an {{out_of_range}} exception if {{pos >= size()}}.

Code Block
bgColor#ccccff
langcpp
string bs("01234567");
try {
  size_t i = f();
  bs.at(i) = '\0';
}
catch (...) {
  cerr << "Index out of range" << endl;
}

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

STR39-CPP

high

likely

high

P9

L2

Bibliography

...

\[[Seacord 05|AA. Bibliography#Seacord 05]\] Chapter 2 Strings \[
[ISO/IEC 14882-2003|AA. Bibliography#ISO/IEC 14882-2003] \] Section 21.3.4 basic_string element access

...

STR38-CPP. Use valid references, pointers, and iterators to reference string objects      07. Characters and Strings (STR)      08. Memory Management (MEM)