Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ $(LIB_BUILD): $(LIB_OBJ)
$(LIBLINKER) $(LIB_LFLAGS) $(LIB_BUILD) $(LIB_OBJ)

# The auto-generated code from bison and flex contains some parts the compiler complains about with -Wall.
# bison_parser.cpp #includes flex_lexer.h, so bison_parser.o must wait for flex_lexer.cpp regeneration
# to finish (which also produces flex_lexer.h). Without this dep, parallel make races and bison_parser.o
# can start before hsql_lex is declared, producing 'hsql_lex was not declared' errors.
$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp $(SRCPARSER)/bison_parser.cpp
$(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-register
$(SRCPARSER)/bison_parser.o: $(SRCPARSER)/bison_parser.cpp
$(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-unused-but-set-variable
$(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-register
$(SRCPARSER)/bison_parser.o: $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp
$(CXX) $(LIB_CFLAGS) -c -o $@ $<

%.o: %.cpp $(PARSER_CPP) $(LIB_H)
$(CXX) $(LIB_CFLAGS) -c -o $@ $<
Expand Down
19 changes: 12 additions & 7 deletions src/SQLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ bool SQLParser::tokenize(const std::string& sql, std::vector<int16_t>* tokens) {
YYSTYPE yylval;
YYLTYPE yylloc;

// Step through the string until EOF is read.
// Note: hsql_lex returns int, but we know that its range is within 16 bit.
int16_t token = hsql_lex(&yylval, &yylloc, scanner);
while (token != 0) {
// Step through the string until EOF is read. Each lex pass that returns
// an sval-bearing token (SQL_IDENTIFIER, SQL_STRING, SQL_NAMED_PARAM)
// allocates yylval.sval via strdup / hsql::substr; free it before the
// next lex call overwrites yylval. The previous loop shape lexed twice
// before freeing, which leaked the first sval-bearing token and missed
// SQL_NAMED_PARAM entirely. The set mirrors bison's `%destructor
// { free($$); } <sval>` for the same tokens.
// Note: hsql_lex returns int, but we know its range is within 16 bit.
while (true) {
int16_t token = hsql_lex(&yylval, &yylloc, scanner);
if (token == 0) break;
tokens->push_back(token);
token = hsql_lex(&yylval, &yylloc, scanner);

if (token == SQL_IDENTIFIER || token == SQL_STRING) {
if (token == SQL_IDENTIFIER || token == SQL_STRING || token == SQL_NAMED_PARAM) {
free(yylval.sval);
}
}
Expand Down
Loading