Skip to content

Decoding input smaller than minHashLength unhandled exceptions are thrown #85

@krstevkoki

Description

@krstevkoki

Seems like that when decoding input with length smaller than minHashLength the Decode throws System.ArgumentException & System.ArgumentOutOfRangeException exceptions

Expected Behavior

I guess it would be to validate the input before doing a .Decode() and either throw meaningful exception or return empty array

Current Behavior

Currently. the library sometimes throws (undocumented?) System.ArgumentException & System.ArgumentOutOfRangeException exceptions.

Possible Solution

Have a proper validation of the input before doing any actions or something else

Steps to Reproduce

  1. new HashidsNet.Hashids(salt: "Dqa2s3RJBYPHUzg&R5qkF3Z4HLaWp#A^kMc^DqKVmqag2tasQjhz-PSM23=4", minHashLength: 9)
  2. .Decode("5111111")
  3. System.ArgumentException: Destination is too short. (Parameter 'destination')

&

  1. new HashidsNet.Hashids(salt: "Dqa2s3RJBYPHUzg&R5qkF3Z4HLaWp#A^kMc^DqKVmqag2tasQjhz-PSM23=4", minHashLength: 10)
  2. .Decode("5111111")
  3. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Context (Environment)

.NET version: ASP.NET Core 6.0 - basic API controller
Hashids.net version: 1.6.1
StackTrace 1:

System.ArgumentException: Destination is too short. (Parameter 'destination')
   at System.Text.StringBuilder.CopyTo(Int32 sourceIndex, Span`1 destination, Int32 count)
   at HashidsNet.Hashids.GenerateHashFrom(ReadOnlySpan`1 numbers, Span`1& result)
   at HashidsNet.Hashids.GetNumbersFrom(String hash)
   at HashidsNet.Hashids.Decode(String hash)
   at WebApplication1.Controllers.WeatherForecastsApi2Controller.GetWeatherForecast2()

StackTrace 2:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
   at System.Text.StringBuilder.CopyTo(Int32 sourceIndex, Span`1 destination, Int32 count)
   at HashidsNet.Hashids.GenerateHashFrom(ReadOnlySpan`1 numbers, Span`1& result)
   at HashidsNet.Hashids.GetNumbersFrom(String hash)
   at HashidsNet.Hashids.Decode(String hash)
   at WebApplication1.Controllers.WeatherForecastsApi2Controller.GetWeatherForecast2()

Possible Implementation

/

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