-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
[3.x] Fix crash on get_joy_button_index_from_string
and get_joy_axis_index_from_string
for non-existing key
#59195
Conversation
I would add this too to be safe. Someone might think in the future that the |
I think the bug may also exist in the JoyButton Input::_get_output_button(String output) {
for (int i = 0; i < (int)JoyButton::SDL_MAX; i++) {
if (output == _joy_buttons[i]) {
return JoyButton(i);
}
}
return JoyButton::INVALID;
}
JoyAxis Input::_get_output_axis(String output) {
for (int i = 0; i < (int)JoyAxis::SDL_MAX; i++) {
if (output == _joy_axes[i]) {
return JoyAxis(i);
}
}
return JoyAxis::INVALID;
} |
@akien-mga It seems that static const char *_joy_buttons[(size_t)JoyButton::SDL_MAX] = {
"a",
"b",
"x",
"y",
"back",
"guide",
"start",
"leftstick",
"rightstick",
"leftshoulder",
"rightshoulder",
"dpup",
"dpdown",
"dpleft",
"dpright",
"misc1",
"paddle1",
"paddle2",
"paddle3",
"paddle4",
"touchpad",
};
static const char *_joy_axes[(size_t)JoyAxis::SDL_MAX] = {
"leftx",
"lefty",
"rightx",
"righty",
"lefttrigger",
"righttrigger",
};
|
Ah that's correct, then it seems fine as is. |
main/input_default.cpp
Outdated
return i; | ||
} | ||
} | ||
ERR_FAIL_V(-1); | ||
ERR_FAIL_V_MSG(-1, vformat(RTR("Could not find a button index matching the string \"%s\"."), p_button)); |
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.
We don't translate error messages so this shouldn't use RTR()
. (my suggestion was using C++ R-strings to avoid having to escape "
, but it's fine with the escape too)
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.
Ah,, I misunderstood your suggestion. Ok I applied it.
main/input_default.cpp
Outdated
return i; | ||
} | ||
} | ||
ERR_FAIL_V(-1); | ||
ERR_FAIL_V_MSG(-1, vformat(RTR("Could not find a axis index matching the string \"%s\"."), p_axis)); |
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.
*an axis index
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.
Oh I missed it. Sorry 😂
As _buttons and _axes have both valid string and nullptr. When iterating over them, if given key exists it will work correctly. But if given key does not exist, it will end up with String::operator=(nullptr). As String constructor from nullptr exists, I use it.
Thanks! |
Cherry-picked for 3.4.4. |
This revision fixes issue #59175.
As _buttons and _axes have both valid string and nullptr.
When iterating over them, if a given key exists it will work correctly.
But if given key does not exist, it will end up with
String::operator=(nullptr)
.As String constructor from nullptr exists, I use it.
JOY_BUTTON_MAX
have value 128 and only used parts of array have max length only 23.When iterating over them, adding check for nullptr and early-quit is also considerable.
Bugsquad edit: