Half-assed attempt at enforcing operator [] preconditions

...inspired by comments to <https://gerrit.libreoffice.org/#/c/3068/>
"String::AppendAscii cleanup in dbaccess," but it quickly becomes apparent that
lots of code rely on s[s.getLength()] == 0, so live with a weakened precondition
check for now.

Change-Id: Ifad96c706b14433df4a084ab8054b32433b8b5b6
This commit is contained in:
Stephan Bergmann
2013-03-28 13:06:36 +01:00
parent 4fb6281270
commit 7eaf1e9388
3 changed files with 20 additions and 4 deletions

View File

@@ -194,7 +194,8 @@ sal_Bool LngParser::Merge(
{ {
rtl::OString sLine( *(*pLines)[ nPos ] ); rtl::OString sLine( *(*pLines)[ nPos ] );
sLine = sLine.trim(); sLine = sLine.trim();
if (( sLine[0] == '[' ) && if (!sLine.isEmpty() &&
( sLine[0] == '[' ) &&
( sLine[sLine.getLength() - 1] == ']' )) ( sLine[sLine.getLength() - 1] == ']' ))
{ {
sGroup = getBracketedContent(sLine).trim(); sGroup = getBracketedContent(sLine).trim();
@@ -220,7 +221,8 @@ sal_Bool LngParser::Merge(
{ {
rtl::OString sLine( *(*pLines)[ nPos ] ); rtl::OString sLine( *(*pLines)[ nPos ] );
sLine = sLine.trim(); sLine = sLine.trim();
if (( sLine[0] == '[' ) && if (!sLine.isEmpty() &&
( sLine[0] == '[' ) &&
( sLine[sLine.getLength() - 1] == ']' )) ( sLine[sLine.getLength() - 1] == ']' ))
{ {
sGroup = getBracketedContent(sLine).trim(); sGroup = getBracketedContent(sLine).trim();

View File

@@ -388,7 +388,14 @@ public:
@since LibreOffice 3.5 @since LibreOffice 3.5
*/ */
sal_Char operator [](sal_Int32 index) const { return getStr()[index]; } sal_Char operator [](sal_Int32 index) const {
assert(index >= 0 && index <= getLength());
//TODO: should really check for < getLength(), but there is quite
// some clever code out there that violates this function's
// documented precondition and relies on s[s.getLength()] == 0 and
// that would need to be fixed first
return getStr()[index];
}
/** /**
Compares two strings. Compares two strings.

View File

@@ -474,7 +474,14 @@ public:
@since LibreOffice 3.5 @since LibreOffice 3.5
*/ */
sal_Unicode operator [](sal_Int32 index) const { return getStr()[index]; } sal_Unicode operator [](sal_Int32 index) const {
assert(index >= 0 && index <= getLength());
//TODO: should really check for < getLength(), but there is quite
// some clever code out there that violates this function's
// documented precondition and relies on s[s.getLength()] == 0 and
// that would need to be fixed first
return getStr()[index];
}
/** /**
Compares two strings. Compares two strings.