Skip to content

Commit a7def5a

Browse files
GKalamernikovGorgi Kalamernikov
andauthored
Issue 85: Fix for min buffer sizes (#86)
* add test * fix the bug --------- Co-authored-by: Gorgi Kalamernikov <ggk@mca.dev>
1 parent 5cf05bd commit a7def5a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/Hashids.net/Hashids.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ private long[] GetNumbersFrom(string hash)
567567
{
568568
var result = NumbersFrom(hash);
569569

570-
Span<char> hashBuffer = hash.Length < MAX_STACKALLOC_SIZE ? stackalloc char[hash.Length] : new char[hash.Length];
570+
int bufferSizeToAllocate = Math.Max(hash.Length, _minHashLength);
571+
Span<char> hashBuffer = bufferSizeToAllocate < MAX_STACKALLOC_SIZE ? stackalloc char[bufferSizeToAllocate] : new char[bufferSizeToAllocate];
571572
var hashLength = GenerateHashFrom(result, ref hashBuffer);
572573
if (hashLength == -1)
573574
return Array.Empty<long>();

test/Hashids.net.test/IssueSpecificTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,14 @@ void Issue75_TooShortHashWithLargerHashLengthShouldNotThrowException()
8686
var hashids = new Hashids("salt", 40);
8787
Assert.Throws<NoResultException>(() => hashids.DecodeSingle("ab"));
8888
}
89+
90+
[Fact]
91+
void Issue85_hash_shorter_than_min_length_should_not_throw_exception()
92+
{
93+
Hashids hashids = new Hashids(salt: "Dqa2s3RJBYPHUzg&R5qkF3Z4HLaWp#A^kMc^DqKVmqag2tasQjhz-PSM23=4", minHashLength: 8);
94+
int[] numbers = hashids.Decode("5111111"); // Length = 7
95+
96+
Assert.Empty(numbers);
97+
}
8998
}
9099
}

0 commit comments

Comments
 (0)