Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes after Fuzz Testing useFmtOnly #1094

Merged
merged 9 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static int scanSQLForChar(char ch, String sql, int offset) {

// Fall through - will fail next if and end up in default case
case '-':
if (sql.charAt(offset) == '-') { // If '-- ... \n' comment
if (offset >= 0 && offset < sql.length() && sql.charAt(offset) == '-') { // If '-- ... \n' comment
while (++offset < len) { // Go thru comment.
if (sql.charAt(offset) == '\n' || sql.charAt(offset) == '\r') {
// If end of comment
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerFMTQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import java.util.List;
import java.util.stream.Collectors;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;


Expand Down Expand Up @@ -89,25 +92,42 @@ String getFMTQuery() {
private SQLServerFMTQuery() {};

SQLServerFMTQuery(String userSql) throws SQLServerException {
if (null == userSql || userSql.length() == 0) {
if (null == userSql || 0 == userSql.length()) {
SQLServerException.makeFromDriverError(null, this,
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), "", false);
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), null, false);
}
InputStream stream = new ByteArrayInputStream(userSql.getBytes(StandardCharsets.UTF_8));
SQLServerLexer lexer = null;
try {
lexer = new SQLServerLexer(CharStreams.fromStream(stream));
} catch (IOException e) {
SQLServerException.makeFromDriverError(null, userSql, e.getLocalizedMessage(), "", false);
SQLServerException.makeFromDriverError(null, userSql, e.getLocalizedMessage(), null, false);
}

lexer.removeErrorListeners();
lexer.addErrorListener(new SQLServerErrorListener());
this.tokenList = (ArrayList<? extends Token>) lexer.getAllTokens();
if (tokenList.size() <= 0) {
SQLServerException.makeFromDriverError(null, this,
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), "", false);
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), null, false);
}
SQLServerTokenIterator iter = new SQLServerTokenIterator(tokenList);
this.prefix = SQLServerParser.getCTE(iter);
SQLServerParser.parseQuery(iter, this);
}
}


class SQLServerErrorListener extends BaseErrorListener {
static final private java.util.logging.Logger logger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.SQLServerFMTQuery");

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
String msg, RecognitionException e) {
if (logger.isLoggable(java.util.logging.Level.FINE)) {
logger.fine("Error occured during token parsing: " + msg);
rene-ye marked this conversation as resolved.
Show resolved Hide resolved
logger.fine("line " + line + ":" + charPositionInLine + " token recognition error at: "
+ offendingSymbol.toString());
}
}
}
Loading