Skip to content

IndexOutOfRangeException when decoding string with guard chars #21

@psavelis

Description

@psavelis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions