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

Use __maybe_unused compiler attribute to fix flakey code-coverage in test_edgesec #432

Merged
merged 6 commits into from
Feb 21, 2023

Conversation

aloisklink
Copy link
Contributor

For some reason, these lines are sometimes not run by tests, and because of that, we get a code-coverage warning:

image

(void)eloop_ctx;
(void)sock_ctx;

I would have thought that these no-op statements are optimized out, even when compiling in debug mode, but I guess sometimes they are kept in?

To fix this, I've taken the __maybe_unused macro from PR #380 and stuck it in a new file called src/utils/attributes.h, which we can use for compiler attributes.
(C23 has built-in support for attributes like [[maybe_unused]], but since we're not using that, we're stuck with doing complicated compiler-specific checks in massive header files)

Add the `__maybe_unused` attribute, which prevents warnings when a
variable is unused
(e.g. if we use the variable only if a given `#ifdef X` is set).

In the edgesec project, we normally use a cast to `(void)` to do the
same thing, e.g. `(void)my_var;`, but code taken from hostap
uses `__maybe_unused`.

This is equivalent to the C23 attribute [`[[maybe_unused]]`][1].

[1]: https://en.cppreference.com/w/c/language/attributes/maybe_unused
Add a CMake INTERFACE/header-only library for `src/radius/common.h`
Add `./src/utils/attributes.h` header file for compiler attributes.

For example, `__attribute__((unused))` is supported on GCC and Clang,
but on other compilers, this is not supported.

Most of these are already in `src/radius/common.h`, so we just need to
move them into a new file to use them in the rest of the edgesec code.
Move `__maybe_unused` from the `src/radius/common.h` file into
`src/utils/attributes.h`, so that we can use it in the rest of
the edgesec project.
Use the `__maybe_unused` compiler attribute to mark unused function
parameters in `tests/test_edgesec.c` as unused, instead of `(void)`.

For some reason, code coverage sometimes thinks that `(void)` lines
are not covered by tests. Using `__maybe_unused` instead should
fix this issue.
@aloisklink aloisklink added bug Something isn't working refactor Refactoring code labels Feb 20, 2023
@codecov
Copy link

codecov bot commented Feb 20, 2023

Codecov Report

Merging #432 (f770f44) into main (656a847) will increase coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #432      +/-   ##
==========================================
+ Coverage   53.09%   53.14%   +0.04%     
==========================================
  Files         144      144              
  Lines       19876    19897      +21     
==========================================
+ Hits        10553    10574      +21     
  Misses       9323     9323              
Impacted Files Coverage Δ
src/edgesec-recap.c 0.00% <ø> (ø)
src/radius/common.h 42.59% <ø> (ø)
src/utils/os.h 100.00% <ø> (ø)
tests/test_edgesec.c 94.95% <100.00%> (-0.36%) ⬇️
tests/utils/test_eloop_threaded.c 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mereacre mereacre self-requested a review February 21, 2023 10:30
Copy link
Contributor

@mereacre mereacre left a comment

Choose a reason for hiding this comment

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

Great.

We should also add https://github.com/nqminds/edgesec/blob/main/src/utils/os.h#L36 i.e.,

__attribute__((packed))

to the attributes.h file.

Move the `#define STRUCT_PACKED __attribute__((packed))` from
`src/utils/os.h` into `src/utils/attributes.h`.

Co-authored-by: Alexandru Mereacre <mereacre@gmail.com>
@aloisklink
Copy link
Contributor Author

Great.

We should also add https://github.com/nqminds/edgesec/blob/main/src/utils/os.h#L36 i.e.,

__attribute__((packed))

to the attributes.h file.

Good idea, I've done it in f770f44

@aloisklink aloisklink added this pull request to the merge queue Feb 21, 2023
Merged via the queue into main with commit 595fe95 Feb 21, 2023
@aloisklink aloisklink deleted the tests/fix-test-edgesec-codecov branch February 21, 2023 11:22
@aloisklink aloisklink mentioned this pull request Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working refactor Refactoring code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants