ullmark/hashids.net

IndexOutOfRangeException when decoding string with guard chars

psavelis opened this issue · 2 comments

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.

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!

@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.