Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOutOfRangeException when decoding string with guard chars #21

Closed
psavelis opened this issue Aug 2, 2017 · 2 comments
Closed

IndexOutOfRangeException when decoding string with guard chars #21

psavelis opened this issue Aug 2, 2017 · 2 comments

Comments

@psavelis
Copy link

psavelis commented Aug 2, 2017

Hi @ullmark ,

I've ended up in a scenario where Decode method throws an exception when we pass any of the characters in the "guard" generated by SetupGuards method.

There's how you reproduce:

// this generates the following guards: rKEa
var hashids = new Hashids("please fix me <3", 15); 

// passing any of the chars defined in the guard may throw and exception
Action invocation = () => hashids.Decode("a");

invocation.ShouldThrow<IndexOutOfRangeException>();

And here's what happens in GetNumbersFrom(string hash) method, please find below:

(...)

// by passing a guard char this replace fails to add a separator (" ")
var hashBreakdown = guardsRegex.Replace(hash, " ");

// hashArray will be empty since there's no  " " to split..
var hashArray = hashBreakdown.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

if (hashArray.Length == 3 || hashArray.Length == 2)
       i = 1;

//  ...and this line fails.
hashBreakdown = hashArray[i];

I'm not sure what the guards stands for in that case (didn't made much effort there, eh) but I believe that's not how it's supposed to behave.

Kind regards,
Pedro.

@ullmark
Copy link
Owner

ullmark commented Aug 9, 2017

Hey! I'll have a look!
But I think I will change the logic around how to setup the alphabet (see issue #20) and guards...
Thanks for the code, I'll make a test and see if it is something I should change!

@hdwatts
Copy link

hdwatts commented Apr 6, 2021

@ullmark Any update on this? We've surrounded our decode checks with a try/catch for IndexOutOfRangeExceptions in the meantime but would prefer a proper solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants