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

Fix | SonarQube issues #1103

Merged
merged 6 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -747,10 +747,8 @@ private byte[] generateNtlmAuthenticate() throws SQLServerException {
int ntChallengeLen = getByteArrayLength(ntChallengeResp);

// allocate token buffer
ByteBuffer token = ByteBuffer
.allocate(NTLM_AUTHENTICATE_PAYLOAD_OFFSET + NTLM_LMCHALLENAGERESPONSE.length
+ ntChallengeResp.length + domainNameLen + userNameLen + workstationLen)
.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer token = ByteBuffer.allocate(NTLM_AUTHENTICATE_PAYLOAD_OFFSET + NTLM_LMCHALLENAGERESPONSE.length
+ ntChallengeLen + domainNameLen + userNameLen + workstationLen).order(ByteOrder.LITTLE_ENDIAN);

// set NTLM signature and message type
token.put(NTLM_HEADER_SIGNATURE, 0, NTLM_HEADER_SIGNATURE.length);
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerFMTQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,34 @@ String getFMTQuery() {
private SQLServerFMTQuery() {};

SQLServerFMTQuery(String userSql) throws SQLServerException {
if (null == userSql || 0 == userSql.length()) {
SQLServerException.makeFromDriverError(null, this,
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(), null, false);
}
lexer.removeErrorListeners();
lexer.addErrorListener(new SQLServerErrorListener());
this.tokenList = (ArrayList<? extends Token>) lexer.getAllTokens();
if (tokenList.size() <= 0) {
if (null != userSql && 0 != userSql.length()) {
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(), null, false);
}
if (null != lexer) {
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"), null, false);
}
SQLServerTokenIterator iter = new SQLServerTokenIterator(tokenList);
this.prefix = SQLServerParser.getCTE(iter);
SQLServerParser.parseQuery(iter, this);
} else {
SQLServerException.makeFromDriverError(null, userSql,
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), null, false);
}
} else {
SQLServerException.makeFromDriverError(null, this,
SQLServerResource.getResource("R_noTokensFoundInUserQuery"), null, false);
}
SQLServerTokenIterator iter = new SQLServerTokenIterator(tokenList);
this.prefix = SQLServerParser.getCTE(iter);
SQLServerParser.parseQuery(iter, this);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ static void resetIteratorIndex(SQLServerTokenIterator iter, int index) {
}
}

private static String getRoundBracketChunk(SQLServerTokenIterator iter, Token t) throws SQLServerException {
private static String getRoundBracketChunk(SQLServerTokenIterator iter) throws SQLServerException {
StringBuilder sb = new StringBuilder();
sb.append('(');
Stack<String> s = new Stack<>();
s.push("(");
while (!s.empty() && iter.hasNext()) {
t = iter.next();
Token t = iter.next();
if (t.getType() == SQLServerLexer.RR_BRACKET) {
sb.append(")");
s.pop();
Expand All @@ -170,13 +170,13 @@ private static String getRoundBracketChunk(SQLServerTokenIterator iter, Token t)
return sb.toString();
}

private static String getRoundBracketChunkBefore(SQLServerTokenIterator iter, Token t) {
private static String getRoundBracketChunkBefore(SQLServerTokenIterator iter) {
StringBuilder sb = new StringBuilder();
sb.append('(');
Stack<String> s = new Stack<>();
s.push(")");
while (!s.empty()) {
t = iter.previous();
Token t = iter.previous();
if (t.getType() == SQLServerLexer.RR_BRACKET) {
sb.append("(");
s.push(")");
Expand Down Expand Up @@ -220,7 +220,7 @@ private static String findColumnAfterParameter(SQLServerTokenIterator iter) thro
t = iter.next();
if (t.getType() != SQLServerLexer.PARAMETER) {
if (t.getType() == SQLServerLexer.LR_BRACKET) {
sb.append(getRoundBracketChunk(iter, t));
sb.append(getRoundBracketChunk(iter));
} else {
sb.append(t.getText());
}
Expand Down Expand Up @@ -269,7 +269,7 @@ private static String findColumnBeforeParameter(SQLServerTokenIterator iter) {
if (t.getType() != SQLServerLexer.PARAMETER) {
Deque<String> d = new ArrayDeque<>();
if (t.getType() == SQLServerLexer.RR_BRACKET) {
d.push(getRoundBracketChunkBefore(iter, t));
d.push(getRoundBracketChunkBefore(iter));
} else {
d.push(t.getText());
}
Expand Down Expand Up @@ -356,7 +356,7 @@ static Token skipTop(SQLServerTokenIterator iter) throws SQLServerException {
if (t.getType() == SQLServerLexer.TOP) {
t = iter.next();
if (t.getType() == SQLServerLexer.LR_BRACKET) {
getRoundBracketChunk(iter, t);
getRoundBracketChunk(iter);
}
t = iter.next();

Expand Down Expand Up @@ -437,7 +437,7 @@ private static String getTableTargetChunk(SQLServerTokenIterator iter, List<Stri
do {
switch (t.getType()) {
case SQLServerLexer.LR_BRACKET:
sb.append(getRoundBracketChunk(iter, t));
sb.append(getRoundBracketChunk(iter));
break;
case SQLServerLexer.OPENDATASOURCE:
case SQLServerLexer.OPENJSON:
Expand All @@ -450,7 +450,7 @@ private static String getTableTargetChunk(SQLServerTokenIterator iter, List<Stri
SQLServerException.makeFromDriverError(null, null,
SQLServerResource.getResource("R_invalidOpenqueryCall"), null, false);
}
sb.append(getRoundBracketChunk(iter, t));
sb.append(getRoundBracketChunk(iter));
break;
case SQLServerLexer.AS:
sb.append(t.getText());
Expand Down