Skip to content

Commit ee39ed1

Browse files
committed
Fix some OOB posibilities
2 parents 458599b + b05d11a commit ee39ed1

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

llvm/include/llvm/AsmParser/LLLexer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ namespace llvm {
120120
private:
121121
lltok::Kind LexToken();
122122

123+
// Return closest pointer after `Ptr` that is an end of a label.
124+
// Returns nullptr if `Ptr` doesn't point into a label.
125+
const char *getLabelTail(const char *Ptr);
123126
int getNextChar();
124127
const char *skipNChars(unsigned N);
125128
void advancePositionTo(const char *Ptr);

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ static bool isLabelChar(char C) {
155155
C == '.' || C == '_';
156156
}
157157

158-
/// isLabelTail - Return true if this pointer points to a valid end of a label.
159-
static const char *isLabelTail(const char *CurPtr) {
160-
while (true) {
161-
if (CurPtr[0] == ':') return CurPtr+1;
162-
if (!isLabelChar(CurPtr[0])) return nullptr;
163-
++CurPtr;
164-
}
165-
}
166-
167158
//===----------------------------------------------------------------------===//
168159
// Lexer definition.
169160
//===----------------------------------------------------------------------===//
@@ -174,28 +165,27 @@ LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
174165
CurPtr = CurBuf.begin();
175166
}
176167

168+
const char *LLLexer::getLabelTail(const char *Ptr) {
169+
while (Ptr != CurBuf.end()) {
170+
if (Ptr[0] == ':')
171+
return Ptr + 1;
172+
if (!isLabelChar(Ptr[0]))
173+
return nullptr;
174+
++Ptr;
175+
}
176+
return nullptr;
177+
}
178+
177179
int LLLexer::getNextChar() {
178-
char CurChar = *CurPtr++;
180+
if (CurPtr == CurBuf.end())
181+
return EOF;
179182
// Increment line number if this is the first character after a newline
180-
// CurPtr points to the char after CurChar, so two positions before that
181-
if ((CurPtr - 2) >= CurBuf.begin() && *(CurPtr - 2) == '\n') {
183+
if (CurPtr > CurBuf.begin() && *(CurPtr-1) == '\n'){
182184
CurLineNum++;
183185
CurColNum = 0;
184186
} else
185187
CurColNum++;
186-
187-
switch (CurChar) {
188-
default: return (unsigned char)CurChar;
189-
case 0:
190-
// A nul character in the stream is either the end of the current buffer or
191-
// a random nul in the file. Disambiguate that here.
192-
if (CurPtr-1 != CurBuf.end())
193-
return 0; // Just whitespace.
194-
195-
// Otherwise, return end of file.
196-
--CurPtr; // Another call to lex will return EOF again.
197-
return EOF;
198-
}
188+
return *CurPtr++;
199189
}
200190

201191
const char *LLLexer::skipNChars(unsigned N) {
@@ -210,7 +200,7 @@ void LLLexer::advancePositionTo(const char *Ptr) {
210200
if (CurPtr > Ptr) {
211201
--CurPtr;
212202
--CurColNum;
213-
// Since CurPtr is one char ahead of the stored position, chech if the
203+
// Since CurPtr is one char ahead of the stored position, check if the
214204
// previous char is not a newline
215205
if (CurPtr != CurBuf.begin() && *(CurPtr - 1) == '\n') {
216206
--CurLineNum;
@@ -264,7 +254,7 @@ lltok::Kind LLLexer::LexToken() {
264254
case '%': return LexPercent();
265255
case '"': return LexQuote();
266256
case '.':
267-
if (const char *Ptr = isLabelTail(CurPtr)) {
257+
if (const char *Ptr = getLabelTail(CurPtr)) {
268258
advancePositionTo(Ptr);
269259
StrVal.assign(TokStart, CurPtr-1);
270260
return lltok::LabelStr;
@@ -347,7 +337,7 @@ lltok::Kind LLLexer::LexAt() {
347337
}
348338

349339
lltok::Kind LLLexer::LexDollar() {
350-
if (const char *Ptr = isLabelTail(TokStart)) {
340+
if (const char *Ptr = getLabelTail(TokStart)) {
351341
advancePositionTo(Ptr);
352342
StrVal.assign(TokStart, CurPtr - 1);
353343
return lltok::LabelStr;
@@ -1198,7 +1188,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
11981188
if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
11991189
!isdigit(static_cast<unsigned char>(CurPtr[0]))) {
12001190
// Okay, this is not a number after the -, it's probably a label.
1201-
if (const char *End = isLabelTail(CurPtr)) {
1191+
if (const char *End = getLabelTail(CurPtr)) {
12021192
StrVal.assign(TokStart, End-1);
12031193
advancePositionTo(End);
12041194
return lltok::LabelStr;
@@ -1225,7 +1215,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
12251215

12261216
// Check to see if this really is a string label, e.g. "-1:".
12271217
if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1228-
if (const char *End = isLabelTail(CurPtr)) {
1218+
if (const char *End = getLabelTail(CurPtr)) {
12291219
StrVal.assign(TokStart, End-1);
12301220
advancePositionTo(End);
12311221
return lltok::LabelStr;

0 commit comments

Comments
 (0)