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

Inconsistent Behaviour in http server : Big Integers are getting converted to float before storing thus losing their precision #1163

Open
pg30 opened this issue Oct 20, 2024 · 2 comments
Assignees
Labels
bug Something isn't working difficulty-level -- mid

Comments

@pg30
Copy link
Contributor

pg30 commented Oct 20, 2024

Steps to reproduce

Trying to set math.MinInt64

curl --location --request GET 'localhost:8082/set' \
--header 'Content-Type: application/json' \
--data '{
    "key" : "a",
    "value" : -9223372036854775808
}'
curl --location --request GET 'localhost:8082/get' \
--header 'Content-Type: application/json' \
--data '{
    "key" : "a"
}'

The result of get operation is

Expected output

{
    "status": "success",
    "data": -9223372036854775808
}

Observed output

{
    "status": "success",
    "data": "-9223372036854776000"
}

On further investigation, I observed that its happening while unmarshalling the request body in the http handler.

  1. In ParseHTTPRequest in redisCmdAdapter.go : we are converting the request body to map[string]interface{}
  2. Since the value type is interface{} golang considers the numbers as float which has precision limits upto 15 digits and thus converts it to scientific notation -9.223372036854776e+18 before storing in the DB
@s4kh
Copy link

s4kh commented Oct 21, 2024

Is this one being worked on? For the solution, should we try converting to string and
use big.Int -> func (z *Int) SetString(s string, base int) (*Int, bool)

bigIntVal := new(big.Int)
if strValue, ok := value.(string); ok {
  bigInt, success := bigIntVal.SetString(strValue, 10)
  if !success {
    return errors.New("Error: Failed to convert string to big.Int")
  }
}

deydebaditya pushed a commit to deydebaditya/dice that referenced this issue Nov 1, 2024
@deydebaditya
Copy link

@pg30 Raised a PR for the fix here: #1227
Please do have a look.

deydebaditya added a commit to deydebaditya/dice that referenced this issue Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working difficulty-level -- mid
Projects
None yet
Development

No branches or pull requests

4 participants