CPlusPlus: deal with QByteArray::(c)begin() return nullptr

You should either use begin() and end(), or data() and size(), and
either way you shouldn't dereference the first iterator if the size is
zero.

Roberto's parser in 3rdparty/cplusplus assumes you've passed at least
one character (I'm guessing the null terminator) and does pointer
manipulation there:

void Lexer::setSource(const char *firstChar, const char *lastChar)
{
    _firstChar = firstChar;
    _lastChar = lastChar;
    _currentChar = _firstChar - 1;
    _currentCharUtf16 = ~0;
    _tokenStart = _currentChar;
    _yychar = '\n';
}

Note the _firstChar - 1 math is technically UB if firstChar is the
actual first character of any buffer allocation or string.

Fixes: QTCREATORBUG-30044
Change-Id: I76ffba14ece04f24b43efffd17abcb8102497813
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Thiago Macieira
2024-01-19 08:10:38 -08:00
parent 1a29f87440
commit 7b04bdf491
2 changed files with 3 additions and 1 deletions

View File

@@ -27,6 +27,7 @@
#include "Literals.h"
#include "DiagnosticClient.h"
#include "cppassert.h"
#include <utils/textutils.h>
#include <stack>
@@ -87,6 +88,7 @@ int TranslationUnit::sourceLength() const
void TranslationUnit::setSource(const char *source, int size)
{
CPP_CHECK(source);
_firstSourceChar = source;
_lastSourceChar = source + size;
}

View File

@@ -527,7 +527,7 @@ Document::Ptr Document::create(const FilePath &filePath)
void Document::setUtf8Source(const QByteArray &source)
{
_source = source;
_translationUnit->setSource(_source.constBegin(), _source.size());
_translationUnit->setSource(_source.constData(), _source.size());
}
LanguageFeatures Document::languageFeatures() const