Skip to content

Commit 4dd7695

Browse files
authored
Guard against null passwords 2/2
1 parent a714049 commit 4dd7695

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/Wpf.Ui/Controls/PasswordBox/PasswordHelper.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ private class PasswordHelper
1919
{
2020
private readonly PasswordBox _passwordBox;
2121
private string _currentText;
22-
private string _newPassword;
23-
private string _currentPassword;
22+
private string? _newPassword;
23+
private string? _currentPassword;
2424

2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="PasswordHelper"/> class.
@@ -44,7 +44,7 @@ public PasswordHelper(PasswordBox passwordBox)
4444
/// 2. When password is revealed (plain text mode)
4545
/// 3. When password is hidden (masked character mode)
4646
/// </remarks>
47-
public string GetNewPassword()
47+
public string? GetNewPassword()
4848
{
4949
_currentPassword = _passwordBox.Password;
5050
_newPassword = _currentPassword;
@@ -53,8 +53,8 @@ public string GetNewPassword()
5353

5454
if (IsDeletingText())
5555
{
56-
int charsToRemove = _currentPassword.Length - _currentText.Length;
57-
_newPassword = _currentPassword.Remove(selectionIndex, charsToRemove);
56+
int charsToRemove = (_currentPassword?.Length ?? 0) - _currentText.Length;
57+
_newPassword = _currentPassword?.Remove(selectionIndex, charsToRemove);
5858
return _newPassword;
5959
}
6060

@@ -78,26 +78,26 @@ public string GetNewPassword()
7878
/// 2. Character was replaced (overwrite)
7979
/// 3. Characters were removed
8080
/// </remarks>
81-
private string HandleHiddenModeChanges(int selectionIndex)
81+
private string? HandleHiddenModeChanges(int selectionIndex)
8282
{
8383
char passwordChar = _passwordBox.PasswordChar;
84-
int currentLength = _currentPassword.Length;
84+
int currentLength = _currentPassword?.Length ?? 0;
8585

8686
if (_currentText.Length > currentLength)
8787
{
8888
// Characters were inserted
8989
int insertedCount = _currentText.Length - currentLength;
9090
string insertedText = _currentText.Substring(selectionIndex - insertedCount, insertedCount);
91-
_newPassword = _currentPassword.Insert(selectionIndex - insertedCount, insertedText);
91+
_newPassword = _currentPassword?.Insert(selectionIndex - insertedCount, insertedText);
9292
}
9393
else if (_currentText.Length == currentLength)
9494
{
9595
// Character was replaced (overwrite)
9696
for (int i = 0; i < _currentText.Length; i++)
9797
{
98-
if (_currentText[i] != passwordChar && i < _newPassword.Length)
98+
if (_currentText[i] != passwordChar && i < (_newPassword?.Length ?? 0))
9999
{
100-
_newPassword = _newPassword.Remove(i, 1).Insert(i, _currentText[i].ToString());
100+
_newPassword = _newPassword?.Remove(i, 1).Insert(i, _currentText[i].ToString());
101101
break;
102102
}
103103
}
@@ -106,7 +106,7 @@ private string HandleHiddenModeChanges(int selectionIndex)
106106
{
107107
// Characters were removed (fallback)
108108
int removedCount = currentLength - _currentText.Length;
109-
_newPassword = _currentPassword.Remove(selectionIndex, removedCount);
109+
_newPassword = _currentPassword?.Remove(selectionIndex, removedCount);
110110
}
111111

112112
return _newPassword;
@@ -121,7 +121,7 @@ private bool IsDeletingText()
121121
Debug.Assert(_currentText == _passwordBox.Text, "Text mismatch");
122122
Debug.Assert(_currentPassword == _passwordBox.Password, "Password mismatch");
123123

124-
return _currentText.Length < _currentPassword.Length;
124+
return _currentText.Length < (_currentPassword?.Length ?? 0);
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)