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

Enable -Wsign-Compare-check_bin/_crypto/_stuffer/_utils/ #3825

Merged
merged 16 commits into from
Feb 16, 2023

Conversation

aditishri18
Copy link
Contributor

@aditishri18 aditishri18 commented Feb 13, 2023

Resolved issues:

related to : #3697

Description of changes:

This PR is a part of series of PR's to enable -Wsign-compare check. This check warns when a comparison between signed and unsigned values produce an incorrect result. Warning shown when the signed value is converted to unsigned value.

This PR contains changes made to files in bin/ , crypto/ , stuffer/ and utils/.

Call-outs:

To be consistent, I changed the data type int to size_t in for loops and where sizeof operator is used.

Testing:

All tests pass.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the s2n-core team label Feb 13, 2023
@aditishri18 aditishri18 marked this pull request as ready for review February 13, 2023 21:28
@aditishri18 aditishri18 changed the title Enable -Wsign Compare check_1 Enable -Wsign-Compare-check_bin/_crypto/_stuffer/_utils/ Feb 13, 2023
@camshaft
Copy link
Contributor

In general, we need to be really careful about conversions that

  • go from signed to unsigned (e.g. int -> uint32_t)
  • go from a large-sized integer to a smaller-sized integer (e.g. uint32_t -> uint16_t)

@aditishri18
Copy link
Contributor Author

What would be a scenario where downcast (e.g. uint32_t -> uint16_t) is required ?

@camshaft
Copy link
Contributor

camshaft commented Feb 14, 2023

What would be a scenario where downcast (e.g. uint32_t -> uint16_t) is required ?

If you have an integer of two sizes you always want to cast to the largest-sized integer that will contain both sides of the comparison. For example, if I want to check if a int and uint32_t are equal, I'd need a int64_t since neither type could hold each others min/max values. For example, you had this before:

S2N_ERROR_IF(len != (int) in->size, S2N_ERR_ENCRYPT);

This means that if in->size is greater than the max value of int (2,147,483,647), then size will wrap and become negative and you can possibly get nonsensical results.

In other words, try to avoid going to a lower bitsize, especially if you haven't checked that the value can actually fit in the lower value. Or just always cast up to the smallest bitsize that will fit the min and max from both operand types.

@aditishri18 aditishri18 requested a review from camshaft February 15, 2023 17:24
@aditishri18
Copy link
Contributor Author

That explains a lot, thanks.

@aditishri18 aditishri18 enabled auto-merge (squash) February 16, 2023 00:55
Copy link
Contributor

@camshaft camshaft left a comment

Choose a reason for hiding this comment

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

sorry one more nit

@aditishri18 aditishri18 disabled auto-merge February 16, 2023 18:39
@aditishri18 aditishri18 enabled auto-merge (squash) February 16, 2023 20:28
@aditishri18 aditishri18 merged commit 101c438 into aws:main Feb 16, 2023
dougch pushed a commit to dougch/s2n-tls that referenced this pull request Feb 17, 2023
dougch pushed a commit to dougch/s2n-tls that referenced this pull request Feb 17, 2023
dougch pushed a commit to dougch/s2n-tls that referenced this pull request Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants