refactor: make s2n_array_len constant #4789
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of changes:
Our grep_simple_mistakes script requires that we use s2n_array_len everywhere instead of
(sizeof(array) / sizeof(array[0]))
, but s2n_array_len is no longer defined as(sizeof(array) / sizeof(array[0]))
, so it's no longer constant. That means that you can't do like:You could do
(sizeof(array) / sizeof(struct s2n_blob))
as a workaround, but that's unnecessary and more brittle.The null check is also unnecessary because arrays can't be null. Only pointers can be null, and s2n_array_len doesn't work on pointers anyway. You could argue that we need a
sizeof(array) > 0
check instead, but funnily enougharray[0]
actually doesn't seg fault for an empty array: https://godbolt.org/z/s4vcrr6fP. I guess that makes sense, since you don't have to dereference anything. I'd hope at least some compilers or static analyzers would yell at you though.Testing:
I'm just removing an unnecessary null check from a macro. I'm not sure how I'd test this.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.