Skip to content

Commit

Permalink
Changes after Fuzz Testing useFmtOnly (#1094)
Browse files Browse the repository at this point in the history
Fixes some unexpected exceptions being thrown.
  • Loading branch information
rene-ye authored Jun 28, 2019
1 parent 7819c9c commit 0e56dbf
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 121 deletions.
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);
logger.fine("line " + line + ":" + charPositionInLine + " token recognition error at: "
+ offendingSymbol.toString());
}
}
}
Loading

0 comments on commit 0e56dbf

Please sign in to comment.