Skip to content

Commit

Permalink
Merge branch 'main' into badrishc/issue960
Browse files Browse the repository at this point in the history
  • Loading branch information
badrishc authored Jan 26, 2025
2 parents fe2d654 + 000f7f1 commit fbc67fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions libs/server/Objects/Hash/HashObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void HashIncrement(ref ObjectInput input, ref SpanByteAndMemory output)
var valueExists = TryGetValue(key, out var value);
if (op == HashOperation.HINCRBY)
{
if (!NumUtils.TryParse(incrSlice.ReadOnlySpan, out int incr))
if (!NumUtils.TryParse(incrSlice.ReadOnlySpan, out long incr))
{
while (!RespWriteUtils.TryWriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref curr, end))
ObjectUtils.ReallocateOutput(ref output, ref isMemory, ref ptr, ref ptrHandle, ref curr, ref end);
Expand All @@ -394,7 +394,7 @@ private void HashIncrement(ref ObjectInput input, ref SpanByteAndMemory output)

if (valueExists)
{
if (!NumUtils.TryParse(value, out int result))
if (!NumUtils.TryParse(value, out long result))
{
while (!RespWriteUtils.TryWriteError(CmdStrings.RESP_ERR_HASH_VALUE_IS_NOT_INTEGER, ref curr,
end))
Expand All @@ -406,7 +406,7 @@ private void HashIncrement(ref ObjectInput input, ref SpanByteAndMemory output)
result += incr;

var resultSpan = (Span<byte>)stackalloc byte[NumUtils.MaximumFormatInt64Length];
var success = Utf8Formatter.TryFormat((long)result, resultSpan, out int bytesWritten,
var success = Utf8Formatter.TryFormat(result, resultSpan, out int bytesWritten,
format: default);
Debug.Assert(success);

Expand Down
16 changes: 15 additions & 1 deletion test/Garnet.test/RespHashTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@ public void CanDoHashExpireWithOptions(string command, string option)

#region LightClientTests


/// <summary>
/// HSET used explictly always returns the number of fields that were added.
/// HSET can manage more than one field in the input
Expand All @@ -1317,7 +1318,6 @@ public void CanSetAndGetOnepairLC(int bytesSent)
ClassicAssert.AreEqual(expectedResponse, actualValue);
}


[Test]
[TestCase(30)]
[TestCase(50)]
Expand Down Expand Up @@ -1794,6 +1794,20 @@ public void CanDoWrongNumOfParametersInHINCRBYFLOATLC()
ClassicAssert.AreEqual(expectedResponse, actualValue);
}

[Test]
public void CanIncrementBeyond32bits()
{
using var lightClientRequest = TestUtils.CreateRequest();
var response = lightClientRequest.SendCommand($"HSET myhash int64 {1L + int.MaxValue}");
var expectedResponse = ":1\r\n";
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);

response = lightClientRequest.SendCommand("HINCRBY myhash int64 1");
expectedResponse = $":{2L + int.MaxValue}\r\n";
actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
}
#endregion

private static string FormatWrongNumOfArgsError(string commandName) => $"-{string.Format(CmdStrings.GenericErrWrongNumArgs, commandName)}\r\n";
Expand Down

0 comments on commit fbc67fa

Please sign in to comment.