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 String::begins_with when both strings are empty #89194

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

miv391
Copy link
Contributor

@miv391 miv391 commented Mar 5, 2024

There are two versions of String::begins_with:

bool String::begins_with(const String &p_string) const
bool String::begins_with(const char *p_string) const

In master, they work differently when checking if "".begins_with("").

String empty("");
bool b1 = empty.begins_with(empty);
bool b2 = empty.begins_with("");

b1 will be true, but b2 will be incorrectly false.

This PR adds more tests for begins_with and fixes the version using a const char * parameter.

I also added tests for cases where the parameter is null. Null parameter is possible only with the version using const char * parameter. anything.begins_with(nullptr) is expected to return false.

I added more tests also for ends_with and made some simplifications for the test case.

It is possible that this breaks something if there exists code that expects "".begins_with("") returning false.

@miv391 miv391 requested review from a team as code owners March 5, 2024 18:51
@AThousandShips AThousandShips added bug topic:core cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Mar 5, 2024
@AThousandShips AThousandShips added this to the 4.3 milestone Mar 5, 2024
@Mickeon
Copy link
Contributor

Mickeon commented Mar 5, 2024

Would be interesting to see where the const char * is used if easily possible just to ensure nothing breaks (which I'm doubtful of).

@miv391
Copy link
Contributor Author

miv391 commented Mar 5, 2024

Would be interesting to see where the const char * is used if easily possible just to ensure nothing breaks (which I'm doubtful of).

It is used a lot, for example vscode finds about 800 lines like this:

if (p_path.begins_with("res://")) {

I didn't find any uses of something.begins_with(""), but there could still be uses of something.begins_with(p) where p points to empty string. But I guess probability of something breaking is very low, although not zero.

@Mickeon
Copy link
Contributor

Mickeon commented Mar 5, 2024

Through sheer boredom I took a closer look and there is literally not a single instance of an array of chars being passed around that is not a constant. It's likely why this went unnoticed, too.

@akien-mga akien-mga merged commit dc55f8b into godotengine:master Mar 5, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@miv391 miv391 deleted the fix-string-begins-with branch March 6, 2024 06:14
@akien-mga
Copy link
Member

Cherry-picked for 4.2.2.

@akien-mga akien-mga removed the cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release label Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants