Skip to content

Commit 6c38063

Browse files
authored
Better handling of antlr4 errors, fixed use after free (#8903)
1 parent 9f44341 commit 6c38063

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

ydb/library/yql/parser/proto_ast/proto_ast.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ namespace antlr4 {
102102
NProtoAST::IErrorCollector* errors;
103103
bool* error;
104104
public:
105-
static ConsoleErrorListener INSTANCE;
106-
107105
YqlErrorListener(NProtoAST::IErrorCollector* errors, bool* error);
108106

109107
virtual void syntaxError(Recognizer *recognizer, Token * offendingSymbol, size_t line, size_t charPositionInLine,
@@ -137,8 +135,8 @@ namespace NProtoAST {
137135
return Parser.Parse(Lexer, &errors);
138136
} catch (const TTooManyErrors&) {
139137
return nullptr;
140-
} catch (const yexception& e) {
141-
errors.Error(0, 0, e.what());
138+
} catch (...) {
139+
errors.Error(0, 0, CurrentExceptionMessage());
142140
return nullptr;
143141
}
144142
}
@@ -159,7 +157,7 @@ namespace NProtoAST {
159157
public:
160158
TProtoASTBuilder(TStringBuf data, const TString& queryName = "query", google::protobuf::Arena* arena = nullptr)
161159
: QueryName(queryName)
162-
, InputStream(std::string(data)) // Why the hell antlr needs non-const ptr??
160+
, InputStream(data)
163161
, Lexer(&InputStream)
164162
, TokenStream(&Lexer)
165163
, Parser(&TokenStream, arena)
@@ -169,6 +167,7 @@ namespace NProtoAST {
169167
google::protobuf::Message* BuildAST(IErrorCollector& errors) {
170168
// TODO: find a better way to break on lexer errors
171169
typename antlr4::YqlErrorListener listener(&errors, &Parser.error);
170+
Parser.removeErrorListeners();
172171
Parser.addErrorListener(&listener);
173172
try {
174173
auto result = Parser.Parse(&errors);
@@ -179,8 +178,8 @@ namespace NProtoAST {
179178
Parser.removeErrorListener(&listener);
180179
Parser.error = false;
181180
return nullptr;
182-
} catch (const yexception& e) {
183-
errors.Error(0, 0, e.what());
181+
} catch (...) {
182+
errors.Error(0, 0, CurrentExceptionMessage());
184183
Parser.removeErrorListener(&listener);
185184
Parser.error = false;
186185
return nullptr;
@@ -232,8 +231,8 @@ namespace NProtoAST {
232231
}
233232
}
234233
} catch (const TTooManyErrors&) {
235-
} catch (const yexception& e) {
236-
errors.Error(0, 0, e.what());
234+
} catch (...) {
235+
errors.Error(0, 0, CurrentExceptionMessage());
237236
}
238237
}
239238

@@ -272,8 +271,8 @@ namespace NProtoAST {
272271
}
273272
}
274273
} catch (const TTooManyErrors&) {
275-
} catch (const yexception& e) {
276-
errors.Error(0, 0, e.what());
274+
} catch (...) {
275+
errors.Error(0, 0, CurrentExceptionMessage());
277276
}
278277
}
279278

0 commit comments

Comments
 (0)