Fix -Wsign-compare on arm32#2432
Conversation
|
Error message on gentoo/arm7l see https://github.com/kinkie/dockerfiles/actions/runs/26706938522/job/78709886696 |
rousskov
left a comment
There was a problem hiding this comment.
Thank you for fixing this regression!
src/peer_digest.cc:562:35: error: comparison of integer expressions of different signedness: 'uint32_t' {aka 'unsigned int'} and 'ssize_t' {aka 'int'} [-Werror=sign-compare]
Refactor ... for 32-bit safety ... Due to ssize_t differences on 32/64 bit platforms ...
The jump from "expressions of different signedness" in the error message to "32-bit safety" feels unnecessary here because signedness is different on both 32- and 64-bit platforms AFAICT.
I suggest using something like this for the PR title: Fix -Wsign-compare on arm32.
Please mention the problematic commit SHA in the PR description (or title).
See 4efdc65 for an example.
| */ | ||
| Assure(size >= 0); | ||
| if (fetch->mask_offset + size > static_cast<ssize_t>(pd->cd->mask_size)) { | ||
| if (static_cast<uint32_t>(size) > pd->cd->mask_size - fetch->mask_offset) { |
There was a problem hiding this comment.
Please do not cast a ssize_t variable to a usually smaller and strangely-precise uint32_t. Cast it to size_t.
The assertion we added above the if statement makes a size_t cast safe. Technically, it does not make the proposed uint32_t cast safe (although it is safe for other reasons).
| if (static_cast<uint32_t>(size) > pd->cd->mask_size - fetch->mask_offset) { | |
| if (static_cast<size_t>(size) > pd->cd->mask_size - fetch->mask_offset) { |
The change from summation to subtraction is outside this PR scope, but I am not going to object to it because subtraction is slightly better here (than what was originally done in the problematic commit) -- it avoids a suspicion of overflow. I trust you have verified that subtracting is safe here (i.e. that the right hand side expression cannot underflow).
There was a problem hiding this comment.
I trust you have verified that subtracting is safe here (i.e. that the right hand side expression cannot underflow).
I have and it looks safe, but if we want to be extra safe we can Assure on it, what do you think?
There was a problem hiding this comment.
if we want to be extra safe we can Assure on it, what do you think?
Your call, but I think an Assure would be a good idea here.
There was a problem hiding this comment.
Adding it. It won't hurt and it may help
|
@yadij would you mind fast tracking this PR? It's the only blocker I have for releasing 7.6 |
Due to ssize_t differences on 32/64 bit platforms, changes
to peerDigestSwapInMask in commit 556b91a cause
signedness comparison errors.
Refactor to be safe both on 32- and 64-bit platforms