DevToys icon indicating copy to clipboard operation
DevToys copied to clipboard

App crash when generating passwords with a large list of excluded characters

Open gabrielpastori1 opened this issue 9 months ago • 3 comments

Current behavior

The application exhibits two distinct issues depending on the "Exclude characters" input:

Total Crash: When providing a long list of specific characters to exclude (e.g., @, :, /, %, &, ?, #, +, !, $, ^, *), the application crashes immediately upon clicking "Generate password(s)".

Incorrect Output: When using a shorter exclusion filter such as ^, *, the generator fails to produce a random string and instead returns only strings of asterisks (*) regardless of the configured password length.

How to reproduce it (as minimally and precisely as possible)

Scenario 1: App Crash

  1. Open the Password Generator tool.
    
  2. Set Password Length to 36.
    
  3. Enable all character toggles (Lowercase, Uppercase, Digits, Special Characters).
    
  4. In the "Exclude the following characters..." field, paste: @, :, /, %, &, ?, #, +, !, $, ^, *
    
  5. Click "Generate password(s)".
    
  6. The app closes/crashes instantly.
    

Scenario 2: Logic Failure (Asterisk Loop)

  1. Use the same settings as above.
  2. Change the exclusion field to: ^, *
  3. Click "Generate password(s)".
  4. The output field fills with lines containing only ************************************.

Expected behavior

The app should handle excluded characters without crashing, even if the list is long.

If the exclusion list is valid, the generator should return a random string of characters that are not in the exclusion list, while still respecting the requested password length.

If an exclusion list makes it impossible to generate a password (e.g., excluding all possible characters), the app should show a validation error instead of crashing or returning a broken string.

Screenshots

Image Image

Workaround

No response

Affected platforms

Windows

Affected DevToys kind

DevToys (app with GUI)

DevToys Version

Versão 1.0.14.0 | X64 | RELEASE | 3ad0ff2d | 3ad0ff2d

Relevant Assets/Logs

No response

gabrielpastori1 avatar Mar 03 '26 04:03 gabrielpastori1

I'd like to work on this one. Planning to reproduce it with a unit test in DevToys.Tools and fix the generator logic there.

malinfossum avatar Aug 28 '26 13:08 malinfossum

I dug into this. The version in the report (1.0.14.0, commit 3ad0ff2d) is DevToys v1, and both behaviors trace to ExcludeCharacters in v1's PasswordGeneratorToolViewModel, which interpolates the exclusion field directly into a regex character class: Regex.Replace(from, $@"[{excluded}]", "").

  • Asterisk output: ^, * becomes [^, *] — the leading ^ negates the class, so everything except ,, space, and * is stripped from every character set. The only surviving pool is "*", which then fills the password.
  • Crash: the long exclusion list empties the special-characters set (!@#$%^&*), but each "guarantee one character per enabled set" branch guards on randomChars[0].Length instead of its own set, then indexes into the empty string — unhandled IndexOutOfRangeException.

I verified the 2.0 implementation (PasswordGeneratorHelper in DevToys.Tools) against both repro scenarios: it filters with a HashSet<char> (no regex) and guards the empty-pool case, so neither symptom reproduces there.

One gap vs. the expected behavior described here: if the exclusion list removes every available character, 2.0 silently outputs nothing instead of showing a validation message. Happy to submit a PR to DevToys.Tools adding that warning plus regression tests for these scenarios, if wanted.

malinfossum avatar Aug 28 '26 13:08 malinfossum

Fix for the 2.0 side of this is up in DevToys-app/DevToys.Tools#77 (warning when the exclusion list removes every available character). The v1 crash is a separate code path, as noted above. Happy to adjust anything if the approach doesn't fit.

malinfossum avatar Sep 30 '26 15:09 malinfossum