Fixed out-of-bounds when originalSource was not explicitly supplied.

Task-number: QTCREATORBUG-3613
This commit is contained in:
Erik Verbruggen
2011-01-27 14:51:01 +01:00
parent 13cc74321e
commit 2062747158
2 changed files with 29 additions and 1 deletions

View File

@@ -134,6 +134,27 @@ void FindUsages::reportResult(unsigned tokenIndex, const QList<LookupItem> &cand
reportResult(tokenIndex);
}
QString FindUsages::matchingLine(const Token &tk) const
{
const char *beg = _source.constData();
const char *cp = beg + tk.offset;
for (; cp != beg - 1; --cp) {
if (*cp == '\n')
break;
}
++cp;
const char *lineEnd = cp + 1;
for (; *lineEnd; ++lineEnd) {
if (*lineEnd == '\n')
break;
}
const QString matchingLine = QString::fromUtf8(cp, lineEnd - cp);
return matchingLine;
}
void FindUsages::reportResult(unsigned tokenIndex)
{
const Token &tk = tokenAt(tokenIndex);
@@ -146,7 +167,12 @@ void FindUsages::reportResult(unsigned tokenIndex)
unsigned line, col;
getTokenStartPosition(tokenIndex, &line, &col);
const QString lineText = QString::fromUtf8(_originalSource.split('\n').at(line - 1));
QString lineText;
QList<QByteArray> lines = _originalSource.split('\n');
if (lines.size() < ((int) line - 1))
lineText = matchingLine(tk);
else
lineText = QString::fromUtf8(lines.at(line - 1));
if (col)
--col; // adjust the column position.