Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/libraries/Common/src/System/Data/Common/AdapterUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// TODO: Enable nullability as part of annotation System.Data.{Odbc,OleDb}
#nullable disable
Comment thread
roji marked this conversation as resolved.
Outdated
#pragma warning disable CS8632

using System.Collections;
using System.Data.SqlTypes;
using System.Diagnostics;
Expand Down Expand Up @@ -471,7 +475,7 @@ internal static Exception InvalidDataLength(long length)
return IndexOutOfRange(SR.Format(SR.SQL_InvalidDataLength, length.ToString(CultureInfo.InvariantCulture)));
}

internal static bool CompareInsensitiveInvariant(string strvalue, string strconst) =>
internal static bool CompareInsensitiveInvariant(string? strvalue, string? strconst) =>
0 == CultureInfo.InvariantCulture.CompareInfo.Compare(strvalue, strconst, CompareOptions.IgnoreCase);

internal static int DstCompare(string strA, string strB) => CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, ADP.DefaultCompareOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// TODO: Remove this after System.Data.{Odbc,OleDb} are null-annotated
#nullable disable
#pragma warning disable CS8632

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -90,8 +95,8 @@ private static class SYNONYM
}

private readonly string _usersConnectionString;
private readonly Dictionary<string, string> _parsetable;
internal readonly NameValuePair _keyChain;
private readonly Dictionary<string, string?> _parsetable;
internal readonly NameValuePair? _keyChain;
internal readonly bool _hasPasswordKeyword;

public string UsersConnectionString(bool hidePassword) =>
Expand All @@ -113,9 +118,10 @@ private string UsersConnectionString(bool hidePassword, bool forceHidePassword)

public bool ConvertValueToBoolean(string keyName, bool defaultValue)
{
string value;
string? value;
// TODO: Is it possible for _parsetable to contain a null value here? If so there's a bug here, investigate.
return _parsetable.TryGetValue(keyName, out value) ?
ConvertValueToBooleanInternal(keyName, value) :
ConvertValueToBooleanInternal(keyName, value!) :
Comment thread
roji marked this conversation as resolved.
Outdated
defaultValue;
}

Expand Down Expand Up @@ -143,7 +149,7 @@ private static bool CompareInsensitiveInvariant(string strvalue, string strconst
(0 == StringComparer.OrdinalIgnoreCase.Compare(strvalue, strconst));

[System.Diagnostics.Conditional("DEBUG")]
static partial void DebugTraceKeyValuePair(string keyname, string keyvalue, Dictionary<string, string> synonyms);
static partial void DebugTraceKeyValuePair(string keyname, string? keyvalue, Dictionary<string, string>? synonyms);

private static string GetKeyName(StringBuilder buffer)
{
Expand Down Expand Up @@ -191,7 +197,7 @@ private enum ParserState
NullTermination,
};

internal static int GetKeyValuePair(string connectionString, int currentPosition, StringBuilder buffer, bool useOdbcRules, out string keyname, out string keyvalue)
internal static int GetKeyValuePair(string connectionString, int currentPosition, StringBuilder buffer, bool useOdbcRules, out string? keyname, out string? keyvalue)
{
int startposition = currentPosition;

Expand Down Expand Up @@ -396,7 +402,7 @@ internal static int GetKeyValuePair(string connectionString, int currentPosition
}

#pragma warning disable CA2249 // Consider using 'string.Contains' instead of 'string.IndexOf'. This file is built into libraries that don't have string.Contains(char).
private static bool IsValueValidInternal(string keyvalue)
private static bool IsValueValidInternal(string? keyvalue)
{
if (null != keyvalue)
{
Expand All @@ -410,7 +416,8 @@ private static bool IsValueValidInternal(string keyvalue)
return true;
}

private static bool IsKeyNameValid(string keyname)
// TODO: Annotate with [NotNullWhen(true)] when annotating System.Data.{Odbc,OleDb}
private static bool IsKeyNameValid(string? keyname)
{
if (null != keyname)
{
Expand All @@ -426,7 +433,7 @@ private static bool IsKeyNameValid(string keyname)
#pragma warning restore CA2249 // Consider using 'string.Contains' instead of 'string.IndexOf'

#if DEBUG
private static Dictionary<string, string> SplitConnectionString(string connectionString, Dictionary<string, string> synonyms, bool firstKey)
private static Dictionary<string, string> SplitConnectionString(string connectionString, Dictionary<string, string>? synonyms, bool firstKey)
{
var parsetable = new Dictionary<string, string>();
Regex parser = (firstKey ? s_connectionStringRegexOdbc : s_connectionStringRegex);
Expand All @@ -447,7 +454,7 @@ private static Dictionary<string, string> SplitConnectionString(string connectio
foreach (Capture keypair in match.Groups[KeyIndex].Captures)
{
string keyname = (firstKey ? keypair.Value : keypair.Value.Replace("==", "=")).ToLowerInvariant();
string keyvalue = keyvalues[indexValue++].Value;
string? keyvalue = keyvalues[indexValue++].Value;
if (0 < keyvalue.Length)
{
if (!firstKey)
Expand All @@ -470,8 +477,8 @@ private static Dictionary<string, string> SplitConnectionString(string connectio
keyvalue = null;
}
DebugTraceKeyValuePair(keyname, keyvalue, synonyms);
string synonym;
string realkeyname = null != synonyms ?
string? synonym;
string? realkeyname = null != synonyms ?
(synonyms.TryGetValue(keyname, out synonym) ? synonym : null) : keyname;

if (!IsKeyNameValid(realkeyname))
Expand All @@ -480,14 +487,14 @@ private static Dictionary<string, string> SplitConnectionString(string connectio
}
if (!firstKey || !parsetable.ContainsKey(realkeyname))
{
parsetable[realkeyname] = keyvalue; // last key-value pair wins (or first)
parsetable[realkeyname] = keyvalue!; // last key-value pair wins (or first)
}
}
}
return parsetable;
}

private static void ParseComparison(Dictionary<string, string> parsetable, string connectionString, Dictionary<string, string> synonyms, bool firstKey, Exception e)
private static void ParseComparison(Dictionary<string, string?> parsetable, string connectionString, Dictionary<string, string>? synonyms, bool firstKey, Exception? e)
{
try
{
Expand All @@ -496,7 +503,7 @@ private static void ParseComparison(Dictionary<string, string> parsetable, strin
{
string keyname = entry.Key;
string value1 = entry.Value;
string value2;
string? value2;
bool parsetableContainsKey = parsetable.TryGetValue(keyname, out value2);
Debug.Assert(parsetableContainsKey, $"{nameof(ParseInternal)} code vs. regex mismatch keyname <{keyname}>");
Debug.Assert(value1 == value2, $"{nameof(ParseInternal)} code vs. regex mismatch keyvalue <{value1}> <{value2}>");
Expand Down Expand Up @@ -539,11 +546,11 @@ private static void ParseComparison(Dictionary<string, string> parsetable, strin
}
#endif

private static NameValuePair ParseInternal(Dictionary<string, string> parsetable, string connectionString, bool buildChain, Dictionary<string, string> synonyms, bool firstKey)
private static NameValuePair? ParseInternal(Dictionary<string, string?> parsetable, string connectionString, bool buildChain, Dictionary<string, string>? synonyms, bool firstKey)
{
Debug.Assert(null != connectionString, "null connectionstring");
StringBuilder buffer = new StringBuilder();
NameValuePair localKeychain = null, keychain = null;
NameValuePair? localKeychain = null, keychain = null;
#if DEBUG
try
{
Expand All @@ -554,7 +561,7 @@ private static NameValuePair ParseInternal(Dictionary<string, string> parsetable
{
int startPosition = nextStartPosition;

string keyname, keyvalue;
string? keyname, keyvalue;
nextStartPosition = GetKeyValuePair(connectionString, startPosition, buffer, firstKey, out keyname, out keyvalue);
if (string.IsNullOrEmpty(keyname))
{
Expand All @@ -566,8 +573,8 @@ private static NameValuePair ParseInternal(Dictionary<string, string> parsetable
Debug.Assert(IsKeyNameValid(keyname), "ParseFailure, invalid keyname");
Debug.Assert(IsValueValidInternal(keyvalue), "parse failure, invalid keyvalue");
#endif
string synonym;
string realkeyname = null != synonyms ?
string? synonym;
string? realkeyname = null != synonyms ?
(synonyms.TryGetValue(keyname, out synonym) ? synonym : null) :
keyname;

Expand Down Expand Up @@ -602,13 +609,13 @@ private static NameValuePair ParseInternal(Dictionary<string, string> parsetable
return keychain;
}

internal NameValuePair ReplacePasswordPwd(out string constr, bool fakePassword)
internal NameValuePair? ReplacePasswordPwd(out string constr, bool fakePassword)
{
bool expanded = false;
int copyPosition = 0;
NameValuePair head = null, tail = null, next = null;
NameValuePair? head = null, tail = null, next = null;
StringBuilder builder = new StringBuilder(_usersConnectionString.Length);
for (NameValuePair current = _keyChain; null != current; current = current.Next)
for (NameValuePair? current = _keyChain; null != current; current = current.Next)
{
if ((KEY.Password != current.Name) && (SYNONYM.Pwd != current.Name))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// TODO: Remove this after System.Data.{Odbc,OleDb} are null-annotated
#pragma warning disable CS8632

namespace System.Data.Common
{
// DbConnectionPoolKey: Base class implementation of a key to connection pool groups
Expand Down Expand Up @@ -37,14 +40,14 @@ internal virtual string ConnectionString
}
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj == null || obj.GetType() != typeof(DbConnectionPoolKey))
{
return false;
}

DbConnectionPoolKey key = obj as DbConnectionPoolKey;
DbConnectionPoolKey? key = obj as DbConnectionPoolKey;

return (key != null && _connectionString == key._connectionString);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.



// TODO: Remove this after System.Data.{Odbc,OleDb} are null-annotated
#pragma warning disable CS8632
//------------------------------------------------------------------------------

using System.Text;
Expand All @@ -24,7 +24,7 @@ would set a or b as a starting quote character.
If a is the starting quote char then c would be the ending quote char
otherwise if b is the starting quote char then d would be the ending quote character.
*/
internal static string[] ParseMultipartIdentifier(string name, string leftQuote, string rightQuote, string property, bool ThrowOnEmptyMultipartName)
internal static string?[] ParseMultipartIdentifier(string name, string leftQuote, string rightQuote, string property, bool ThrowOnEmptyMultipartName)
{
return ParseMultipartIdentifier(name, leftQuote, rightQuote, '.', MaxParts, true, property, ThrowOnEmptyMultipartName);
}
Expand All @@ -47,7 +47,7 @@ private enum MPIState
* limit: number of names to parse out
* removequote:to remove the quotes on the returned string
*/
private static void IncrementStringCount(string name, string[] ary, ref int position, string property)
private static void IncrementStringCount(string name, string?[] ary, ref int position, string property)
{
++position;
int limit = ary.Length;
Expand All @@ -63,7 +63,7 @@ private static bool IsWhitespace(char ch)
return char.IsWhiteSpace(ch);
}

internal static string[] ParseMultipartIdentifier(string name, string leftQuote, string rightQuote, char separator, int limit, bool removequotes, string property, bool ThrowOnEmptyMultipartName)
internal static string?[] ParseMultipartIdentifier(string name, string leftQuote, string rightQuote, char separator, int limit, bool removequotes, string property, bool ThrowOnEmptyMultipartName)
{
if (limit <= 0)
{
Expand All @@ -75,13 +75,13 @@ internal static string[] ParseMultipartIdentifier(string name, string leftQuote,
throw ADP.InvalidMultipartNameIncorrectUsageOfQuotes(property, name);
}

string[] parsedNames = new string[limit]; // return string array
string?[] parsedNames = new string?[limit]; // return string array
int stringCount = 0; // index of current string in the buffer
MPIState state = MPIState.MPI_Value; // Initialize the starting state

StringBuilder sb = new StringBuilder(name.Length); // String buffer to hold the string being currently built, init the string builder so it will never be resized
StringBuilder whitespaceSB = null; // String buffer to hold whitespace used when parsing nonquoted strings 'a b . c d' = 'a b' and 'c d'
char rightQuoteChar = ' '; // Right quote character to use given the left quote character found.
StringBuilder? whitespaceSB = null; // String buffer to hold whitespace used when parsing nonquoted strings 'a b . c d' = 'a b' and 'c d'
char rightQuoteChar = ' '; // Right quote character to use given the left quote character found.
for (int index = 0; index < name.Length; ++index)
{
char testchar = name[index];
Expand Down Expand Up @@ -182,7 +182,7 @@ internal static string[] ParseMultipartIdentifier(string name, string leftQuote,
}
else
{
whitespaceSB.Append(testchar);
whitespaceSB!.Append(testchar);
}
break;
}
Expand Down
13 changes: 8 additions & 5 deletions src/libraries/Common/src/System/Data/Common/NameValuePair.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// TODO: Remove this after System.Data.{Odbc,OleDb} are null-annotated
#pragma warning disable CS8632

using System.Diagnostics;

namespace System.Data.Common
{
internal sealed class NameValuePair
{
private readonly string _name;
private readonly string _value;
private readonly string? _value;
private readonly int _length;
private NameValuePair _next;
private NameValuePair? _next;

internal NameValuePair(string name, string value, int length)
internal NameValuePair(string name, string? value, int length)
{
Debug.Assert(!string.IsNullOrEmpty(name), "empty keyname");
_name = name;
Expand All @@ -30,9 +33,9 @@ internal int Length
}

internal string Name => _name;
internal string Value => _value;
internal string? Value => _value;

internal NameValuePair Next
internal NameValuePair? Next
{
get { return _next; }
set
Expand Down
Loading