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

BaseJob: Check if null in encodeIfParam #793

Closed

Conversation

redstrate
Copy link
Contributor

If % is not inside of paramPart - like if it's empty - then std::ranges::find will return a null iterator. The rest of this if check depends on it not being null, so it segfaults when it is.

Noticed this during some random usage in NeoChat.

@KitsuneRal
Copy link
Member

It's never supposed to be a "null" iterator (that thing doesn't really exist, to begin with), it should return paramPart.end(): https://en.cppreference.com/w/cpp/algorithm/ranges/find - something is off here.

@redstrate
Copy link
Contributor Author

It's never supposed to be a "null" iterator (that thing doesn't really exist, to begin with), it should return paramPart.end(): https://en.cppreference.com/w/cpp/algorithm/ranges/find - something is off here.

That is weird, maybe my debugger is lying to me :D Let me re-create the crash again, but maybe it's related to paramPart being empty?

@KitsuneRal
Copy link
Member

Ouch. That might be, the comparison with end() - 3 may go off in that case.

Okay, I actually can rewrite it with a regex, QRegularExpression precompiles itself anyway, so performance-wise it should be about the same, and the readability is better.

If % is not inside of paramPart - like if it's empty - then
std::ranges::find will return the end iterator. The rest of this if
check depends on it being valid, and segfaults when it is. A check has
been added to cover this case.
@redstrate redstrate force-pushed the work/redstrate/fix-basejob-crash branch from 69eb26b to 6c0da0a Compare September 1, 2024 15:47
@redstrate
Copy link
Contributor Author

🤦 yeah, my debugger visualization showed it as a QChar* but that's just what's inside of the iterator. You're right that it's actually the end.

Okay, I actually can rewrite it with a regex, QRegularExpression precompiles itself anyway, so performance-wise it should be about the same, and the readability is better.

Yeah that sounds even better!

@KitsuneRal
Copy link
Member

For posterity - decent people unlike me use std::distance to check the distance from end()... aanyways, going with the regex in this case.

@KitsuneRal KitsuneRal closed this Sep 1, 2024
KitsuneRal added a commit that referenced this pull request Sep 1, 2024
Using a handcrafted condition is both longer and error-prone (see #793).
KitsuneRal added a commit that referenced this pull request Sep 1, 2024
Using a handcrafted condition is both longer and error-prone (see #793).
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.

2 participants