Skip to content

Enable -Werror for unit tests and fix resulting compiler warnings#5668

Closed
thulasiramk-2310 wants to merge 23 commits intoaws:mainfrom
thulasiramk-2310:fix-werror-unit-tests
Closed

Enable -Werror for unit tests and fix resulting compiler warnings#5668
thulasiramk-2310 wants to merge 23 commits intoaws:mainfrom
thulasiramk-2310:fix-werror-unit-tests

Conversation

@thulasiramk-2310
Copy link
Copy Markdown
Contributor

Goal

Enable -Werror for all unit test builds and fix the warnings that appear when compiling tests with this flag enabled.

Why

Unit tests previously did not compile with -Werror, which allowed several warnings to accumulate unnoticed.
Applying warnings-as-errors improves build hygiene, enforces consistency with the main library build, and prevents future regressions.

How

Build System

  • Updated CMakeLists.txt to apply -Werror to all unit test targets when UNSAFE_TREAT_WARNINGS_AS_ERRORS=ON is set.

Warning Fixes

  • tests/unit/s2n_safety_test.c

    • Initialized a local buffer to resolve a maybe-uninitialized warning.
  • tests/unit/s2n_tls12_handshake_test.c

    • Replaced invalid array-to-array comparisons with memcmp().
  • tests/unit/s2n_tls13_handshake_state_machine_test.c

    • Replaced array comparisons with proper memcmp() checks.

Verification

  • Rebuilt the entire project with strict warning settings to ensure no further warnings remain.

Callouts

  • This enforcement uses the existing UNSAFE_TREAT_WARNINGS_AS_ERRORS mechanism to keep the change opt-in, as requested in the issue discussion.
  • If maintainers prefer applying this unconditionally, I can update the PR accordingly.

Testing

Built and tested with:

cmake -DUNSAFE_TREAT_WARNINGS_AS_ERRORS=ON ..
make -j

Verified:

  • 100% of unit tests pass (280 tests, 0 failures)
  • All warnings resolved
  • No behavioral regressions introduced

Related

Resolves #5650

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

@maddeleine
Copy link
Copy Markdown
Contributor

Looks like there's also a failure on mac builds. I think you probably just need to add an if-def S2N_CLONE_SUPPORTED around the s2n_unit_test_clone_child_process function to get those builds to pass.

@maddeleine
Copy link
Copy Markdown
Contributor

Looks like there's more mac build errors. If you have a mac handy that you can build this code change on, that would be the fastest way to resolve this. Otherwise we'll just have to go one by one through the errors.

@thulasiramk-2310
Copy link
Copy Markdown
Contributor Author

@maddeleine I’ve pushed a fix for the macOS issue. If CI still fails, I’ll try reproducing it on macOS.

@maddeleine
Copy link
Copy Markdown
Contributor

Yep another error. Looks like we did the same thing with the s2n_random_test. You'll need to add the S2N_CLONE_SUPPORTED to that test as well.

@thulasiramk-2310
Copy link
Copy Markdown
Contributor Author

I’m fixing the remaining macOS/FreeBSD/OpenBSD failures incrementally. Any guidance on what to prioritize or common pitfalls to check next would be appreciated.

@maddeleine
Copy link
Copy Markdown
Contributor

Adding the ifdef S2N_CLONE_SUPPORTED macro in the s2n_random_test will fix the current failure.

@thulasiramk-2310
Copy link
Copy Markdown
Contributor Author

Yes — I've wrapped the clone helper with #if S2N_CLONE_SUPPORTED in s2n_random_test.c , and it fixes the unused-function error locally.

@@ -581,6 +581,9 @@ if (BUILD_TESTING)
-Wshadow -Wcast-align -Wwrite-strings -Wformat-security
-Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-deprecated -std=gnu99
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're going to have to add -Wno-missing-braces to this list on line 582. I don't see any other warnings that need to be suppressed in the build output, so maybe this is the last fix?

@maddeleine
Copy link
Copy Markdown
Contributor

Ah actually there's two more fixes that we need to make:

  1. In the s2n_x509_validator_test.c the function "mock_time" needs to be moved inside of an #if S2N_OCSP_STAPLING_SUPPORTED clause.
  2. In line 195 in our s2n_mem_usage_test.c the print format needs to be changed from %li to %zi because the variable being printed out is actually ssize_t, not long.

@thulasiramk-2310
Copy link
Copy Markdown
Contributor Author

@maddeleine ,I’ve fixed the reported errors and updated the Pull Request.

@thulasiramk-2310
Copy link
Copy Markdown
Contributor Author

@maddeleine This PR ended up with a noisy commit history due to some accidental clang-format runs.

To keep things clean, I’m going to close this PR and open a fresh one with the same fixes but a clean commit history. I’ll link the new PR here shortly.

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.

Turn on -Werror flag for unit tests

2 participants