Skip to content

Commit

Permalink
Resolved issues reported by SonarQube (#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird authored Nov 14, 2022
1 parent 8babee8 commit 7f17532
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ static boolean isDllLoaded() {

static FedAuthDllInfo getAccessTokenForWindowsIntegrated(String stsURL, String servicePrincipalName,
String clientConnectionId, String clientId, long expirationFileTime) throws DLLException {
FedAuthDllInfo dllInfo = ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName,
clientConnectionId, clientId, expirationFileTime, authLogger);
return dllInfo;
return ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName, clientConnectionId, clientId,
expirationFileTime, authLogger);
}

// InitDNSName should be called to initialize the DNSName before calling this function
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/com/microsoft/sqlserver/jdbc/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ final boolean isInitialized() {
*
* If the column has not yet been read from the response then this method reads it.
*/
Object getValue(JDBCType jdbcType, InputStreamGetterArgs getterArgs, Calendar cal,
TDSReader tdsReader, SQLServerStatement statement) throws SQLServerException {
Object getValue(JDBCType jdbcType, InputStreamGetterArgs getterArgs, Calendar cal, TDSReader tdsReader,
SQLServerStatement statement) throws SQLServerException {
Object value = getterDTV.getValue(jdbcType, typeInfo.getScale(), getterArgs, cal, typeInfo, cryptoMetadata,
tdsReader, statement);
setInternalVariant(getterDTV.getInternalVariant());
Expand Down Expand Up @@ -242,12 +242,11 @@ else if (jdbcType.isBinary()) {

// if jdbcType is char or varchar, check if the column is actually char/varchar or nchar/nvarchar
// in order to make updateString() work with encrypted Nchar typpes
if (null != cryptoMetadata && (JDBCType.CHAR == jdbcType || JDBCType.VARCHAR == jdbcType)) {
if (JDBCType.NVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.NCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.LONGNVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()) {
jdbcType = cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType();
}
if (null != cryptoMetadata && (JDBCType.CHAR == jdbcType || JDBCType.VARCHAR == jdbcType)
&& (JDBCType.NVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.NCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.LONGNVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType())) {
jdbcType = cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType();
}

if (Util.shouldHonorAEForParameters(stmtColumnEncriptionSetting, con)) {
Expand All @@ -264,12 +263,12 @@ else if (jdbcType.isBinary()) {

// for update encrypted nchar or nvarchar value on result set, must double the value length,
// otherwise, the data is truncated.
if (null != cryptoMetadata) {
if (JDBCType.NCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.NVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.LONGNVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()) {
this.valueLength = valueLength * 2;
}
if (null != cryptoMetadata
&& (JDBCType.NCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.NVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType().getJDBCType()
|| JDBCType.LONGNVARCHAR == cryptoMetadata.getBaseTypeInfo().getSSType()
.getJDBCType())) {
this.valueLength = valueLength * 2;
}
}
} else {
Expand Down
100 changes: 67 additions & 33 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ static final Object convertBytesToObject(byte[] bytesValue, JDBCType jdbcType,
* @return the required object.
*/
static final Object convertStringToObject(String stringVal, Charset charset, JDBCType jdbcType,
StreamType streamType) throws UnsupportedEncodingException, IllegalArgumentException {
StreamType streamType) throws UnsupportedEncodingException {
switch (jdbcType) {
// Convert String to Numeric types.
case DECIMAL:
Expand Down Expand Up @@ -622,7 +622,7 @@ private static LocalDateTime parseStringIntoLDT(String s) {
int hour;
int minute;
int second;
int a_nanos = 0;
int nanos = 0;
int firstDash;
int secondDash;
int dividingSpace;
Expand Down Expand Up @@ -652,18 +652,18 @@ private static LocalDateTime parseStringIntoLDT(String s) {

// Convert the date
boolean parsedDate = false;
if (firstDash > 0 && secondDash > 0 && secondDash < dividingSpace - 1) {
if (firstDash == YEAR_LENGTH && (secondDash - firstDash > 1 && secondDash - firstDash <= MONTH_LENGTH + 1)
&& (dividingSpace - secondDash > 1 && dividingSpace - secondDash <= DAY_LENGTH + 1)) {
year = Integer.parseInt(s.substring(0, firstDash));
month = Integer.parseInt(s.substring(firstDash + 1, secondDash));
day = Integer.parseInt(s.substring(secondDash + 1, dividingSpace));

if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
parsedDate = true;
}
if (firstDash > 0 && secondDash > 0 && secondDash < dividingSpace - 1 && firstDash == YEAR_LENGTH
&& (secondDash - firstDash > 1 && secondDash - firstDash <= MONTH_LENGTH + 1)
&& (dividingSpace - secondDash > 1 && dividingSpace - secondDash <= DAY_LENGTH + 1)) {
year = Integer.parseInt(s.substring(0, firstDash));
month = Integer.parseInt(s.substring(firstDash + 1, secondDash));
day = Integer.parseInt(s.substring(secondDash + 1, dividingSpace));

if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
parsedDate = true;
}
}

if (!parsedDate) {
throw new java.lang.IllegalArgumentException(formatError);
}
Expand All @@ -685,7 +685,7 @@ private static LocalDateTime parseStringIntoLDT(String s) {
tmpNanos *= 10;
nanoPrecision++;
}
a_nanos = tmpNanos;
nanos = tmpNanos;
} else if (period > 0) {
throw new java.lang.IllegalArgumentException(formatError);
} else {
Expand All @@ -694,7 +694,7 @@ private static LocalDateTime parseStringIntoLDT(String s) {
} else {
throw new java.lang.IllegalArgumentException(formatError);
}
return LocalDateTime.of(year, month, day, hour, minute, second, a_nanos);
return LocalDateTime.of(year, month, day, hour, minute, second, nanos);
}

static final Object convertStreamToObject(BaseInputStream stream, TypeInfo typeInfo, JDBCType jdbcType,
Expand Down Expand Up @@ -779,7 +779,7 @@ static final Object convertStreamToObject(BaseInputStream stream, TypeInfo typeI

// Slightly less fast path for MBCS data that converts directly/easily to ASCII
if (getterArgs.isAdaptive) {
return AsciiFilteredUnicodeInputStream.MakeAsciiFilteredUnicodeInputStream(stream,
return AsciiFilteredUnicodeInputStream.makeAsciiFilteredUnicodeInputStream(stream,
new BufferedReader(new InputStreamReader(stream, typeInfo.getCharset())));
} else {
return new ByteArrayInputStream(
Expand Down Expand Up @@ -808,10 +808,7 @@ static final Object convertStreamToObject(BaseInputStream stream, TypeInfo typeI
//
// Catch them and translate them to a SQLException so that we don't propagate an unexpected exception
// type all the way up to the app, which may not catch it either...
catch (IllegalArgumentException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorConvertingValue"));
throw new SQLServerException(form.format(new Object[] {typeInfo.getSSType(), jdbcType}), null, 0, e);
} catch (UnsupportedEncodingException e) {
catch (IllegalArgumentException | UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_errorConvertingValue"));
throw new SQLServerException(form.format(new Object[] {typeInfo.getSSType(), jdbcType}), null, 0, e);
}
Expand Down Expand Up @@ -891,7 +888,7 @@ private static String fractionalSecondsString(long subSecondNanos, int scale) {
* @return a Java object of the desired type.
*/
static final Object convertTemporalToObject(JDBCType jdbcType, SSType ssType, Calendar timeZoneCalendar,
int daysSinceBaseDate, long ticksSinceMidnight, int fractionalSecondsScale) {
int daysSinceBaseDate, long ticksSinceMidnight, int fractionalSecondsScale) throws SQLServerException {

// In cases where a Calendar object (and therefore Timezone) is not passed to the method,
// use the path below instead to optimize performance.
Expand Down Expand Up @@ -1036,7 +1033,9 @@ static final Object convertTemporalToObject(JDBCType jdbcType, SSType ssType, Ca
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0, null);
}

int localMillisOffset = timeZoneCalendar.get(Calendar.ZONE_OFFSET);
Expand Down Expand Up @@ -1098,7 +1097,10 @@ static final Object convertTemporalToObject(JDBCType jdbcType, SSType ssType, Ca
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0,
null);
}
}

Expand Down Expand Up @@ -1192,17 +1194,22 @@ cal, fractionalSecondsString(subSecondNanos, fractionalSecondsScale),
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0,
null);
}
}

default:
throw new AssertionError("Unexpected JDBCType: " + jdbcType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0, null);
}
}

private static Object convertTemporalToObject(JDBCType jdbcType, SSType ssType, int daysSinceBaseDate,
long ticksSinceMidnight, int fractionalSecondsScale) {
long ticksSinceMidnight, int fractionalSecondsScale) throws SQLServerException {
int subSecondNanos;

// In cases where Timezone values don't need to be considered, use LocalDateTime go avoid
Expand Down Expand Up @@ -1254,7 +1261,9 @@ private static Object convertTemporalToObject(JDBCType jdbcType, SSType ssType,
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0, null);
}

switch (jdbcType.category) {
Expand Down Expand Up @@ -1282,7 +1291,10 @@ private static Object convertTemporalToObject(JDBCType jdbcType, SSType ssType,
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0,
null);
}
}

Expand Down Expand Up @@ -1333,12 +1345,17 @@ private static Object convertTemporalToObject(JDBCType jdbcType, SSType ssType,
}

default:
throw new AssertionError("Unexpected SSType: " + ssType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0,
null);
}
}

default:
throw new AssertionError("Unexpected JDBCType: " + jdbcType);
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedConversionFromTo"));
throw new SQLServerException(form.format(new Object[] {ssType.name(), jdbcType}), null, 0, null);
}
}

Expand Down Expand Up @@ -1471,31 +1488,36 @@ final class AsciiFilteredInputStream extends InputStream {
ASCII_FILTER[i] = (byte) '?';
}

AsciiFilteredInputStream(BaseInputStream containedStream) throws SQLServerException {
AsciiFilteredInputStream(BaseInputStream containedStream) {
if (BaseInputStream.logger.isLoggable(java.util.logging.Level.FINER))
BaseInputStream.logger.finer(containedStream.toString() + " wrapping in AsciiFilteredInputStream");
this.containedStream = containedStream;
}

@Override
public void close() throws IOException {
containedStream.close();
}

@Override
public long skip(long n) throws IOException {
return containedStream.skip(n);
}

@Override
public int available() throws IOException {
return containedStream.available();
}

@Override
public int read() throws IOException {
int value = containedStream.read();
if (value >= 0 && value <= 255)
return ASCII_FILTER[value];
return value;
}

@Override
public int read(byte[] b) throws IOException {
int bytesRead = containedStream.read(b);
if (bytesRead > 0) {
Expand All @@ -1506,6 +1528,7 @@ public int read(byte[] b) throws IOException {
return bytesRead;
}

@Override
public int read(byte b[], int offset, int maxBytes) throws IOException {
int bytesRead = containedStream.read(b, offset, maxBytes);
if (bytesRead > 0) {
Expand All @@ -1516,14 +1539,17 @@ public int read(byte b[], int offset, int maxBytes) throws IOException {
return bytesRead;
}

@Override
public boolean markSupported() {
return containedStream.markSupported();
}

@Override
public void mark(int readLimit) {
containedStream.mark(readLimit);
}

@Override
public void reset() throws IOException {
containedStream.reset();
}
Expand All @@ -1540,27 +1566,29 @@ final class AsciiFilteredUnicodeInputStream extends InputStream {
private final Reader containedReader;
private final Charset asciiCharSet;

static AsciiFilteredUnicodeInputStream MakeAsciiFilteredUnicodeInputStream(BaseInputStream strm,
Reader rd) throws SQLServerException {
static AsciiFilteredUnicodeInputStream makeAsciiFilteredUnicodeInputStream(BaseInputStream strm, Reader rd) {
if (BaseInputStream.logger.isLoggable(java.util.logging.Level.FINER))
BaseInputStream.logger.finer(strm.toString() + " wrapping in AsciiFilteredInputStream");
return new AsciiFilteredUnicodeInputStream(rd);
}

// Note the Reader provided should support mark, reset
private AsciiFilteredUnicodeInputStream(Reader rd) throws SQLServerException {
private AsciiFilteredUnicodeInputStream(Reader rd) {
containedReader = rd;
asciiCharSet = US_ASCII;
}

@Override
public void close() throws IOException {
containedReader.close();
}

@Override
public long skip(long n) throws IOException {
return containedReader.skip(n);
}

@Override
public int available() throws IOException {
// from the JDBC spec
// Note: A stream may return 0 when the method InputStream.available is called whether there is data available
Expand All @@ -1571,15 +1599,18 @@ public int available() throws IOException {

private final byte[] bSingleByte = new byte[1];

@Override
public int read() throws IOException {
int bytesRead = read(bSingleByte);
return (-1 == bytesRead) ? -1 : (bSingleByte[0] & 0xFF);
}

@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}

@Override
public int read(byte b[], int offset, int maxBytes) throws IOException {
char tempBufferToHoldCharDataForConversion[] = new char[maxBytes];
int charsRead = containedReader.read(tempBufferToHoldCharDataForConversion);
Expand All @@ -1593,10 +1624,12 @@ public int read(byte b[], int offset, int maxBytes) throws IOException {
return charsRead;
}

@Override
public boolean markSupported() {
return containedReader.markSupported();
}

@Override
public void mark(int readLimit) {
try {
containedReader.mark(readLimit);
Expand All @@ -1607,6 +1640,7 @@ public void mark(int readLimit) {
}
}

@Override
public void reset() throws IOException {
containedReader.reset();
}
Expand Down
Loading

0 comments on commit 7f17532

Please sign in to comment.