diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index bb9008f017..c4fbceef1e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -12539,7 +12539,9 @@ private byte[] SerializeUnencryptedValue(object value, MetaType type, byte scale if (type.FixedLength == 4) { if (0 > dt.days || dt.days > UInt16.MaxValue) + { throw SQL.SmallDateTimeOverflow(MetaType.ToDateTime(dt.days, dt.time, 4).ToString(CultureInfo.InvariantCulture)); + } if (stateObj._bIntBytes == null) { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index 03889347ae..51a9f012d3 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -688,7 +688,7 @@ internal void Connect(ServerInfo serverInfo, _connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection); _connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake); - UInt32 result = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); + uint result = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId"); // for DNS Caching phase 1 @@ -760,7 +760,7 @@ internal void Connect(ServerInfo serverInfo, ThrowExceptionAndWarning(_physicalStateObj); } - UInt32 retCode = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); + uint retCode = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId); Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId"); SqlClientEventSource.Log.TryTraceEvent(" Sending prelogin handshake"); @@ -863,7 +863,7 @@ internal void RemoveEncryption() { Debug.Assert((_encryptionOption & EncryptionOptions.OPTIONS_MASK) == EncryptionOptions.LOGIN, "Invalid encryption option state"); - UInt32 error = 0; + uint error = 0; // Remove SSL (Encryption) SNI provider since we only wanted to encrypt login. error = SNINativeMethodWrapper.SNIRemoveProvider(_physicalStateObj.Handle, Provider.SSL_PROV); @@ -889,8 +889,8 @@ internal void EnableMars() // Cache physical stateObj and connection. _pMarsPhysicalConObj = _physicalStateObj; - UInt32 error = 0; - UInt32 info = 0; + uint error = 0; + uint info = 0; // Add SMUX (MARS) SNI provider. error = SNINativeMethodWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, Provider.SMUX_PROV, ref info); @@ -1138,7 +1138,7 @@ private void SendPreLoginHandshake( break; case (int)PreLoginOptions.THREADID: - Int32 threadID = TdsParserStaticMethods.GetCurrentThreadIdForTdsLoginOnly(); + int threadID = TdsParserStaticMethods.GetCurrentThreadIdForTdsLoginOnly(); payload[payloadLength++] = (byte)((0xff000000 & threadID) >> 24); payload[payloadLength++] = (byte)((0x00ff0000 & threadID) >> 16); @@ -1170,7 +1170,7 @@ private void SendPreLoginHandshake( payload[payloadLength++] = (byte)((0x0000ff00 & actId.Sequence) >> 8); payload[payloadLength++] = (byte)((0x00ff0000 & actId.Sequence) >> 16); payload[payloadLength++] = (byte)((0xff000000 & actId.Sequence) >> 24); - int actIdSize = GUID_SIZE + sizeof(UInt32); + int actIdSize = GUID_SIZE + sizeof(uint); offset += actIdSize; optionDataSize += actIdSize; @@ -1936,12 +1936,12 @@ internal void CheckResetConnection(TdsParserStateObject stateObj) if (_fPreserveTransaction) { // if we are reseting, set bit in header by or'ing with other value - stateObj._outBuff[1] = (Byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION_PRESERVE_TRANSACTION); + stateObj._outBuff[1] = (byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION_PRESERVE_TRANSACTION); } else { // if we are reseting, set bit in header by or'ing with other value - stateObj._outBuff[1] = (Byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION); + stateObj._outBuff[1] = (byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION); } if (!_fMARS) @@ -4267,7 +4267,7 @@ private TdsOperationStatus TryProcessLoginAck(TdsParserStateObject stateObj, out } _is2012 = true; break; - case (uint)TdsEnums.TDS8_MAJOR << 24 | TdsEnums.TDS8_MINOR: + case TdsEnums.TDS8_MAJOR << 24 | TdsEnums.TDS8_MINOR: if (increment != TdsEnums.TDS8_INCREMENT) { throw SQL.InvalidTDSVersion(); @@ -4888,13 +4888,13 @@ internal TdsOperationStatus TryProcessReturnValue(int length, } // always read as sql types - Debug.Assert(valLen < (ulong)(Int32.MaxValue), "ProcessReturnValue received data size > 2Gb"); + Debug.Assert(valLen < (ulong)(int.MaxValue), "ProcessReturnValue received data size > 2Gb"); - int intlen = valLen > (ulong)(Int32.MaxValue) ? Int32.MaxValue : (int)valLen; + int intlen = valLen > (ulong)(int.MaxValue) ? int.MaxValue : (int)valLen; if (rec.metaType.IsPlp) { - intlen = Int32.MaxValue; // If plp data, read it all + intlen = int.MaxValue; // If plp data, read it all } if (isNull) @@ -4926,7 +4926,7 @@ internal TdsOperationStatus TryProcessTceCryptoMetadata(TdsParserStateObject sta // Read the ordinal into cipher table ushort index = 0; - UInt32 userType; + uint userType; TdsOperationStatus result; // For return values there is not cipher table and no ordinal. @@ -5066,7 +5066,7 @@ private void WriteCollation(SqlCollation collation, TdsParserStateObject stateOb } else { - _physicalStateObj.WriteByte(sizeof(UInt32) + sizeof(byte)); + _physicalStateObj.WriteByte(sizeof(uint) + sizeof(byte)); WriteUnsignedInt(collation._info, _physicalStateObj); _physicalStateObj.WriteByte(collation._sortId); } @@ -6627,7 +6627,7 @@ internal TdsOperationStatus TrySkipValue(SqlMetaDataPriv md, int columnOrdinal, TdsOperationStatus result; if (md.metaType.IsPlp) { - result = TrySkipPlpValue(UInt64.MaxValue, stateObj, out _); + result = TrySkipPlpValue(ulong.MaxValue, stateObj, out _); if (result != TdsOperationStatus.Done) { return result; @@ -6750,7 +6750,7 @@ private TdsOperationStatus TryReadSqlStringValue(SqlBuffer value, byte type, int case TdsEnums.SQLNVARCHAR: case TdsEnums.SQLNTEXT: { - String s = null; + string s = null; if (isPlp) { @@ -7087,7 +7087,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value, if (isPlp) { // We must read the column value completely, no matter what length is passed in - length = Int32.MaxValue; + length = int.MaxValue; } //DEVNOTE: When modifying the following routines (for deserialization) please pay attention to @@ -7791,23 +7791,23 @@ internal Task WriteSqlVariantValue(object value, int length, int offset, TdsPars switch (mt.TDSType) { case TdsEnums.SQLFLT4: - WriteFloat((Single)value, stateObj); + WriteFloat((float)value, stateObj); break; case TdsEnums.SQLFLT8: - WriteDouble((Double)value, stateObj); + WriteDouble((double)value, stateObj); break; case TdsEnums.SQLINT8: - WriteLong((Int64)value, stateObj); + WriteLong((long)value, stateObj); break; case TdsEnums.SQLINT4: - WriteInt((Int32)value, stateObj); + WriteInt((int)value, stateObj); break; case TdsEnums.SQLINT2: - WriteShort((Int16)value, stateObj); + WriteShort((short)value, stateObj); break; case TdsEnums.SQLINT1: @@ -7874,15 +7874,15 @@ internal Task WriteSqlVariantValue(object value, int length, int offset, TdsPars case TdsEnums.SQLMONEY: { - WriteCurrency((Decimal)value, 8, stateObj); + WriteCurrency((decimal)value, 8, stateObj); break; } case TdsEnums.SQLNUMERICN: { stateObj.WriteByte(mt.Precision); //propbytes: precision - stateObj.WriteByte((byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale - WriteDecimal((Decimal)value, stateObj); + stateObj.WriteByte((byte)((decimal.GetBits((decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale + WriteDecimal((decimal)value, stateObj); break; } @@ -7940,27 +7940,27 @@ internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject sta { case TdsEnums.SQLFLT4: WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj); - WriteFloat((Single)value, stateObj); + WriteFloat((float)value, stateObj); break; case TdsEnums.SQLFLT8: WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj); - WriteDouble((Double)value, stateObj); + WriteDouble((double)value, stateObj); break; case TdsEnums.SQLINT8: WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj); - WriteLong((Int64)value, stateObj); + WriteLong((long)value, stateObj); break; case TdsEnums.SQLINT4: WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj); - WriteInt((Int32)value, stateObj); + WriteInt((int)value, stateObj); break; case TdsEnums.SQLINT2: WriteSqlVariantHeader(4, metatype.TDSType, metatype.PropBytes, stateObj); - WriteShort((Int16)value, stateObj); + WriteShort((short)value, stateObj); break; case TdsEnums.SQLINT1: @@ -8039,7 +8039,7 @@ internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject sta case TdsEnums.SQLMONEY: { WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj); - WriteCurrency((Decimal)value, 8, stateObj); + WriteCurrency((decimal)value, 8, stateObj); break; } @@ -8047,8 +8047,8 @@ internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject sta { WriteSqlVariantHeader(21, metatype.TDSType, metatype.PropBytes, stateObj); stateObj.WriteByte(metatype.Precision); //propbytes: precision - stateObj.WriteByte((byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale - WriteDecimal((Decimal)value, stateObj); + stateObj.WriteByte((byte)((decimal.GetBits((decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale + WriteDecimal((decimal)value, stateObj); break; } @@ -8104,7 +8104,7 @@ private byte[] SerializeSqlMoney(SqlMoney value, int length, TdsParserStateObjec private void WriteSqlMoney(SqlMoney value, int length, TdsParserStateObject stateObj) { // UNDONE: can I use SqlMoney.ToInt64()? - int[] bits = Decimal.GetBits(value.Value); + int[] bits = decimal.GetBits(value.Value); // this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by) bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000))); @@ -8115,7 +8115,7 @@ private void WriteSqlMoney(SqlMoney value, int length, TdsParserStateObject stat if (length == 4) { - Decimal decimalValue = value.Value; + decimal decimalValue = value.Value; // validate the value can be represented as a small money if (decimalValue < TdsEnums.SQL_SMALL_MONEY_MIN || decimalValue > TdsEnums.SQL_SMALL_MONEY_MAX) @@ -8175,10 +8175,10 @@ private byte[] SerializeCurrency(Decimal value, int length, TdsParserStateObject return bytes; } - private void WriteCurrency(Decimal value, int length, TdsParserStateObject stateObj) + private void WriteCurrency(decimal value, int length, TdsParserStateObject stateObj) { SqlMoney m = new SqlMoney(value); - int[] bits = Decimal.GetBits(m.Value); + int[] bits = decimal.GetBits(m.Value); // this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by) bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000))); @@ -8296,7 +8296,7 @@ private byte[] SerializeDateTimeOffset(DateTimeOffset value, byte scale, int len private void WriteDateTimeOffset(DateTimeOffset value, byte scale, int length, TdsParserStateObject stateObj) { WriteDateTime2(value.UtcDateTime, scale, length - 2, stateObj); - Int16 offset = (Int16)value.Offset.TotalMinutes; + short offset = (short)value.Offset.TotalMinutes; stateObj.WriteByte((byte)(offset & 0xff)); stateObj.WriteByte((byte)((offset >> 8) & 0xff)); } @@ -8488,7 +8488,7 @@ struct { private void WriteDecimal(decimal value, TdsParserStateObject stateObj) { - stateObj._decimalBits = Decimal.GetBits(value); + stateObj._decimalBits = decimal.GetBits(value); Debug.Assert(stateObj._decimalBits != null, "decimalBits should be filled in at TdsExecuteRPC time"); /* @@ -9625,7 +9625,7 @@ internal byte[] GetDTCAddress(int timeout, TdsParserStateObject stateObj) long dtcLength = dtcReader.GetBytes(0, 0, null, 0, 0); // - if (dtcLength <= Int32.MaxValue) + if (dtcLength <= int.MaxValue) { int cb = (int)dtcLength; @@ -10396,7 +10396,7 @@ internal Task TdsExecuteRPC(SqlCommand cmd, IList<_SqlRPC> rpcArray, int timeout else { Debug.Assert(value is SqlChars, "Unknown value for Ansi datatype"); - s = new String(((SqlChars)value).Value); + s = new string(((SqlChars)value).Value); } } else @@ -10490,12 +10490,12 @@ internal Task TdsExecuteRPC(SqlCommand cmd, IList<_SqlRPC> rpcArray, int timeout // Split the input name. TypeName is returned as single 3 part name during DeriveParameters. // NOTE: ParseUdtTypeName throws if format is incorrect - String[] names = SqlParameter.ParseTypeName(param.UdtTypeName, isUdtTypeName: true); - if (!ADP.IsEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length) + string[] names = SqlParameter.ParseTypeName(param.UdtTypeName, isUdtTypeName: true); + if (!string.IsNullOrEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length) { throw ADP.ArgumentOutOfRange("names"); } - if (!ADP.IsEmpty(names[1]) && TdsEnums.MAX_SERVERNAME < names[names.Length - 2].Length) + if (!string.IsNullOrEmpty(names[1]) && TdsEnums.MAX_SERVERNAME < names[names.Length - 2].Length) { throw ADP.ArgumentOutOfRange("names"); } @@ -11552,10 +11552,10 @@ internal void WriteBulkCopyMetaData(_SqlMetaDataSet metadataCollection, int coun } // Write the flags - UInt16 flags; - flags = (UInt16)(md.Updatability << 2); - flags |= (UInt16)(md.IsNullable ? (UInt16)TdsEnums.Nullable : (UInt16)0); - flags |= (UInt16)(md.IsIdentity ? (UInt16)TdsEnums.Identity : (UInt16)0); + ushort flags; + flags = (ushort)(md.Updatability << 2); + flags |= (ushort)(md.IsNullable ? (ushort)TdsEnums.Nullable : (ushort)0); + flags |= (ushort)(md.IsIdentity ? (ushort)TdsEnums.Identity : (ushort)0); // Write the next byte of flags if (_serverSupportsColumnEncryption) @@ -11995,7 +11995,7 @@ private int GetNotificationHeaderSize(SqlNotificationRequest notificationRequest { throw ADP.ArgumentNull("CallbackId"); } - else if (UInt16.MaxValue < callbackId.Length) + else if (ushort.MaxValue < callbackId.Length) { throw ADP.ArgumentOutOfRange("CallbackId"); } @@ -12004,7 +12004,7 @@ private int GetNotificationHeaderSize(SqlNotificationRequest notificationRequest { throw ADP.ArgumentNull("Service"); } - else if (UInt16.MaxValue < service.Length) + else if (ushort.MaxValue < service.Length) { throw ADP.ArgumentOutOfRange("Service"); } @@ -12049,9 +12049,9 @@ private void WriteQueryNotificationHeaderData(SqlNotificationRequest notificatio // we did verification in GetNotificationHeaderSize, so just assert here. Debug.Assert(callbackId != null, "CallbackId is null"); - Debug.Assert(UInt16.MaxValue >= callbackId.Length, "CallbackId length is out of range"); + Debug.Assert(ushort.MaxValue >= callbackId.Length, "CallbackId length is out of range"); Debug.Assert(service != null, "Service is null"); - Debug.Assert(UInt16.MaxValue >= service.Length, "Service length is out of range"); + Debug.Assert(ushort.MaxValue >= service.Length, "Service length is out of range"); Debug.Assert(-1 <= timeout, "Timeout"); SqlClientEventSource.Log.TryNotificationTraceEvent(" NotificationRequest: userData: '{0}', options: '{1}', timeout: '{2}'", notificationRequest.UserData, notificationRequest.Options, notificationRequest.Timeout); @@ -12230,9 +12230,9 @@ private bool IsBOMNeeded(MetaType type, object value) return true; } } - else if ((currentType == typeof(String)) && (((String)value).Length > 0)) + else if ((currentType == typeof(string)) && (((String)value).Length > 0)) { - if ((value != null) && (((String)value)[0] & 0xff) != 0xff) + if ((value != null) && (((string)value)[0] & 0xff) != 0xff) return true; } else if (currentType == typeof(SqlXml)) @@ -12375,8 +12375,7 @@ private Task WriteUnterminatedSqlValue(object value, MetaType type, int actualLe } if (value is SqlChars) { - String sch = new String(((SqlChars)value).Value); - + string sch = new string(((SqlChars)value).Value); return WriteEncodingChar(sch, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false); } else @@ -12437,7 +12436,7 @@ private Task WriteUnterminatedSqlValue(object value, MetaType type, int actualLe if (type.FixedLength == 4) { - if (0 > dt.DayTicks || dt.DayTicks > UInt16.MaxValue) + if (0 > dt.DayTicks || dt.DayTicks > ushort.MaxValue) throw SQL.SmallDateTimeOverflow(dt.ToString()); WriteShort(dt.DayTicks, stateObj); @@ -12708,7 +12707,6 @@ public override void Write(char value) public override void Write(char[] buffer, int index, int count) { - ValidateWriteParameters(buffer, index, count); Debug.Assert(_size >= _written); @@ -12988,11 +12986,11 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int { case TdsEnums.SQLFLTN: if (type.FixedLength == 4) - WriteFloat((Single)value, stateObj); + WriteFloat((float)value, stateObj); else { Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!"); - WriteDouble((Double)value, stateObj); + WriteDouble((double)value, stateObj); } break; @@ -13017,7 +13015,6 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int { WriteInt(actualLength, stateObj); // chunk length } - return stateObj.WriteByteArray((byte[])value, actualLength, offset, canAccumulate: false); } } @@ -13047,13 +13044,13 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int if (type.FixedLength == 1) stateObj.WriteByte((byte)value); else if (type.FixedLength == 2) - WriteShort((Int16)value, stateObj); + WriteShort((short)value, stateObj); else if (type.FixedLength == 4) - WriteInt((Int32)value, stateObj); + WriteInt((int)value, stateObj); else { Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture)); - WriteLong((Int64)value, stateObj); + WriteLong((long)value, stateObj); } break; @@ -13153,7 +13150,7 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int } case TdsEnums.SQLNUMERICN: Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes"); - WriteDecimal((Decimal)value, stateObj); + WriteDecimal((decimal)value, stateObj); break; case TdsEnums.SQLDATETIMN: @@ -13163,7 +13160,7 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int if (type.FixedLength == 4) { - if (0 > dt.days || dt.days > UInt16.MaxValue) + if (0 > dt.days || dt.days > ushort.MaxValue) throw SQL.SmallDateTimeOverflow(MetaType.ToDateTime(dt.days, dt.time, 4).ToString(CultureInfo.InvariantCulture)); WriteShort(dt.days, stateObj); @@ -13179,7 +13176,7 @@ private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int case TdsEnums.SQLMONEYN: { - WriteCurrency((Decimal)value, type.FixedLength, stateObj); + WriteCurrency((decimal)value, type.FixedLength, stateObj); break; } @@ -13954,8 +13951,8 @@ internal TdsOperationStatus TrySkipPlpValue(ulong cb, TdsParserStateObject state while ((totalBytesSkipped < cb) && (stateObj._longlenleft > 0)) { - if (stateObj._longlenleft > Int32.MaxValue) - bytesSkipped = Int32.MaxValue; + if (stateObj._longlenleft > int.MaxValue) + bytesSkipped = int.MaxValue; else bytesSkipped = (int)stateObj._longlenleft; bytesSkipped = ((cb - totalBytesSkipped) < (ulong)bytesSkipped) ? (int)(cb - totalBytesSkipped) : bytesSkipped;