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 #10059: Clamp config item values to int, disallow negative random deviation, and allow values up to 12 digits including '-' #10444
Fix #10059: Clamp config item values to int, disallow negative random deviation, and allow values up to 12 digits including '-' #10444
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation should mention the clamping and the allowed range.
4f3b6f2
to
e96f2e7
Compare
src/script/api/script_info_docs.hpp
Outdated
@@ -218,17 +218,23 @@ class ScriptInfo { | |||
* store the current configuration of Scripts. Required. | |||
* - description A single line describing the setting. Required. | |||
* - min_value The minimum value of this setting. Required for integer | |||
* settings and not allowed for boolean settings. | |||
* settings and not allowed for boolean settings. The value will be | |||
* clamped in the range [-2147483648, 2147483647] (inclusive). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't check all documentation, but some places use MIN(int32) .. MAX(int32)
style.
Not sure which one is better, but consistency is good.
3101b36
to
026c754
Compare
src/script/script_config.cpp
Outdated
@@ -216,7 +216,7 @@ std::string ScriptConfig::SettingsToString() const | |||
char *s = string; | |||
*s = '\0'; | |||
for (const auto &item : this->settings) { | |||
char no[10]; | |||
char no[12]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all the commit message is not correct compared to this code. The 12 in here contains a trailing \0
, so at most 11 digits could be placed.
Secondly, the whole function is trying hard to stay within the artificial 1024 byte limit, but then returns a theoreticall unbounded string
, so why not use proper C++ constructs to create this settings string?
Thirdly, the ini file reading seems to use a 1024 byte buffer as well, so how can the written value ever be correctly read, because I think there is some key in front of the configuration.
So all-in-all it's more broken than this commit fixes, though I can agree that the second and third issue are not for this PR. The commit message definitely is broken, and the current number is magic-y without any comments, so what about something like:
char no[10 + 1 + 1]; // Maximum of 10 digits for MIN/MAX_INT32, 1 for the sign and 1 for '\0'.
And if the "12" gets used in many more places, a constant would be better as it can be reused, maybe something like INT32_DIGITS_WITH_SIGN_AND_TERMINATION
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I placed INT32_DIGITS_WITH_SIGN_AND_TERMINATION in the script_config.hpp with comment, added includes in script_gui.cpp and game_gui.cpp to read this value. Also edited 2 commit messages.
Also prevent random_deviation to be below 0.
…to 10 digits + 1 for sign
…1 for termination, enough to fit min and max int
026c754
to
eaf65c4
Compare
Motivation / Problem
We can't input custom negative values via query string on a config item and it can't be more than 10 digits.
Crash report is caused by implicit 64 bit to 32 bit conversions during construction of a script's settings.
We can't create labels for settings with a negative value.
Description
Limitations
I suppose none.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.