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

static_cast<size_t> for QByteArray::size with std::max #4406

Merged
merged 1 commit into from
Oct 13, 2021

Conversation

Be-ing
Copy link
Contributor

@Be-ing Be-ing commented Oct 13, 2021

QByteArray::size returns qsizetype in Qt6, which needs to be
converted to an int to pass to std::max.

@@ -16,7 +16,7 @@ cache_key_t cacheKeyFromMessageDigest(const QByteArray& messageDigest) {
// 5 Hash function Usage
// 5.1 Truncated Message Digest
const auto significantByteCount = math_min(
messageDigest.size(),
static_cast<int>(messageDigest.size()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead better remove the static_cast<int> casts. This should work. I expect that qsizetype is an alias for size_t on all platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried. It does not work with Qt5.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I did not find documentation on qsizetype.

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/qt/qtbase/blob/e05e3c776256d35798f451f31cd71e809786ed78/src/corelib/global/qglobal.h#L258

size_t is unsigned

qsizetype is ptrdiff_t wich is the signed counterpart.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, what do you suggest as an alternative solution? Or just merge this?

Copy link
Contributor

Choose a reason for hiding this comment

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

https://doc.qt.io/qt-5/qtglobal.html#qsizetype-alias

We should either cast to qsizetype or size_t. Since the size() cannot be negative and sizeof is implicitly of type size_t this would be my preferred choice.

@Be-ing Be-ing force-pushed the qsizetype_int_cast branch from 93625fb to 326ed3c Compare October 13, 2021 20:49
Copy link
Contributor

@uklotzde uklotzde left a comment

Choose a reason for hiding this comment

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

Waiting for CI

for (auto i = 0; i < significantByteCount; ++i) {
static_cast<size_t>(messageDigest.size()),
sizeof(cache_key_t));
for (unsigned int i = 0; i < significantByteCount; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (unsigned int i = 0; i < significantByteCount; ++i) {
for (size_t i = 0; i < significantByteCount; ++i) {

The case that size_t is an unsigned type can be considered as design flaw of the early c++ standard.
In Mixxx we have introduced SINT as ptrdiff_t because it allows auto factorization.

typedef std::ptrdiff_t SINT;

So If you looking for a general patter to resolve this issue use ptrdiff_t.

@Be-ing Be-ing force-pushed the qsizetype_int_cast branch from 326ed3c to 6766e4a Compare October 13, 2021 21:05
QByteArray::size returns qsizetype in Qt6, which needs to be
converted to size_t to pass to std::max.
@Be-ing Be-ing force-pushed the qsizetype_int_cast branch from 6766e4a to 1ebc690 Compare October 13, 2021 21:36
@Be-ing Be-ing changed the title static_cast<int> for QByteArray::size with std::max static_cast<size_t> for QByteArray::size with std::max Oct 13, 2021
@Be-ing
Copy link
Contributor Author

Be-ing commented Oct 13, 2021

CI passed. Merge?

@daschuer
Copy link
Member

Thank you.

@daschuer daschuer merged commit 20abb64 into mixxxdj:main Oct 13, 2021
@Be-ing Be-ing deleted the qsizetype_int_cast branch October 13, 2021 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants