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

fix(json): int64 overflow in JSON numop #2340

Closed

Conversation

jackyyyyyssss
Copy link
Contributor

Json_val. NumOp only considers cases where the value of the double type is out of bounds, and does not consider cases where the int64 type is out of bounds. If the int64 type is out of bounds, it will cause the lambda function to exit abnormally and not catch. This exception will cause the kvrocks to crash
27319d3236f39a914025911dac65157
ff70eb032f10ad79a3a379996df76fc

@git-hulk git-hulk changed the title Fix int64 overflow Fix int64 overflow in JSON numop May 30, 2024
@PragmaTwice
Copy link
Member

Thank you for your great finding.

Comment on lines 456 to 458
if (!number_res.GetValue().value.is_int64() && !number_res.GetValue().value.is_double()) {
return rocksdb::Status::InvalidArgument("number out of range");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be fixed in the JsonValue::NumOp. Here we can only fix for value, not the number inside the current json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Summarized all the situations to see if there were any omissions

Copy link

@jackyyyyyssss jackyyyyyssss changed the title Fix int64 overflow in JSON numop fix int64 overflow in JSON numop May 31, 2024
@jackyyyyyssss jackyyyyyssss changed the title fix int64 overflow in JSON numop fix(json):int64 overflow in JSON numop May 31, 2024
@jackyyyyyssss jackyyyyyssss changed the title fix(json):int64 overflow in JSON numop fix(json): int64 overflow in JSON numop May 31, 2024
Comment on lines 44 to 50
inline bool Int64OperationOverFlow(int64_t a, int64_t b, int64_t result, uint8_t operation) {
if (operation == 1) {
return __builtin_add_overflow(a, b, &result);
} else {
return __builtin_mul_overflow(a, b, &result);
}
}
Copy link
Member

@PragmaTwice PragmaTwice May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function looks very weird. What's operation? What's 1 here? Why mul is executed if operation is not 1?

Please remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this function is totally wrong since the result is passed by value.

Copy link
Member

@PragmaTwice PragmaTwice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just add a catch block. Try not to add any meaningless weird code.

@jackyyyyyssss
Copy link
Contributor Author

jackyyyyyssss commented May 31, 2024

Please just add a catch block. Try not to add any meaningless weird code.

Simple catch doesn't seem to work, some overflow cases can return results that don't meet expectations without throwing exceptions
For example, 9223372036854775806<std:: numericlimits:: max() +20 is a negative number and will not throw an exception

@PragmaTwice
Copy link
Member

Please just add a catch block. Try not to add any meaningless weird code.

Simple catch doesn't seem to work, some overflow cases can return results that don't meet expectations without throwing exceptions For example, 9223372036854775806<std:: numericlimits:: max() +20 is a negative number and will not throw an exception

It can be actually well defined. It follows the two's complement. So generally we can leave it as is until we make a big number implementation available.

@jackyyyyyssss
Copy link
Contributor Author

So generally we can leave it as is until we make a big number implementation available.

What do you mean by directly adding a catch (const jsoncons:: json_runtime_error<std:: runtime_error>e){
Return {Status:: NotOK, e.what()};
}Is that enough for this ?

@PragmaTwice
Copy link
Member

So generally we can leave it as is until we make a big number implementation available.

What do you mean by directly adding a catch (const jsoncons:: json_runtime_error<std:: runtime_error>e){ Return {Status:: NotOK, e.what()}; }Is that enough for this ?

Yeah, just preventing kvrocks from crash is enough.

@PragmaTwice
Copy link
Member

Sorry I looked into this problem a little bit, and seems it's a design issue inside the NumOp method.

I opened a PR to address it (#2345).

But anyway thank you very much for your great finding and effort.

@jackyyyyyssss
Copy link
Contributor Author

Sorry I looked into this problem a little bit, and seems it's a design issue inside the NumOp method.

I opened a PR to address it (#2345).

But anyway thank you very much for your great finding and effort.

You have made the same changes to the visual and redis stack processing, and have learned a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants