Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HINCRBY should use 64bit signed integers, not 32bit ints. #957

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading