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

Add first .clang-tidy file #2847

Merged
merged 9 commits into from
Jul 5, 2024
Merged

Conversation

joholl
Copy link
Collaborator

@joholl joholl commented May 25, 2024

Add a .clang-tidy file with some of the less opinionated check. Fix the linting issues (one commit per check).

For linting, I don't call clang-tidy directly, but the official python script for multi-core support. You can add -fix for automatic fixing.

make -j compile_commands.json
run-clang-tidy-15 -header-filter $PWD $(find src include -name '*.c' -o -name '*.h') | ansi2txt | tee clang-tidy.log

For now, I focused on src/, but not test/

Fixes: #2826

@joholl joholl mentioned this pull request May 25, 2024
@joholl joholl force-pushed the clang_tidy branch 2 times, most recently from 028b49b to 91bb5f0 Compare June 5, 2024 06:58
@joholl joholl marked this pull request as ready for review June 5, 2024 08:54
@joholl
Copy link
Collaborator Author

joholl commented Jun 5, 2024

@JuergenReppSIT @AndreasFuchsTPM cirrus keeps failing on me, but otherwise the CI is happy. What do you think of this?

Copy link
Member

@AndreasFuchsTPM AndreasFuchsTPM left a comment

Choose a reason for hiding this comment

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

Seams to dislike leading _
This is especially bad in the cases of API incompatibilities.

include/tss2/tss2_esys.h Outdated Show resolved Hide resolved
include/tss2/tss2_sys.h Outdated Show resolved Hide resolved
include/tss2/tss2_tpm2_types.h Outdated Show resolved Hide resolved
@joholl
Copy link
Collaborator Author

joholl commented Jun 7, 2024

@AndreasFuchsTPM TLDR; it is undefined behavior since those identifiers are reserved (for stdlib and compiler i guess).

Here's what the C standard says (section 7.1.3):

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

And further:

If the program declares or defines an identifier in a context in which it is reserved [...] the behavior is undefined.

Clang-tidy checks that via rule bugprone-reserved-identifier.

_PRIVATE

_PRIVATE might not really be a "private" type, but just a utility type for defining TPM2B_PRIVATE. While not useful to the user either, we could make it public (but we should mangle the name somehow).

struct TPM2B_PRIVATE {
    UINT16 size;
    BYTE buffer[sizeof(PRIVATE)];
};

If we really wanted to make it private, we would need to jump through hoops because we have a sizeof(_PRIVATE) in our public header (e.g. create a configure-time macro which provides the size of PRIVATE and use that instead).

Suggestion: rename it TPM_OPAQUE_PRIVATE (and adding docs telling users it is private). Technically, this is a breaking API change. I would argue, using this type as a user is fishy in any case. In my understanding this is supposed to be an opaque blob. We also don't use it in the TSS (except as an argument to sizeof() in the json (de)serialization code.

An alternative is leaving it as-is and technically introducing undefined behavior in every user's namespace.

_TSS2_SYS_OPAQUE_CONTEXT_BLOB

So this is really a private type. I'd argue the OPAQUE portion of the name already makes it clear what this is.

Suggestion: Rename to TSS2_SYS_OPAQUE_CONTEXT_BLOB and writing a docstring telling users to not use it.

__ptr

It is technically legal to have a leading underscore (as _foo) in a limited scope, so e.g. as a parameter (but not on file scope). We probably just want to rename that to ptr. This has no API implications anyway.

Suggestion: rename to ptr

@AndreasFuchsTPM
Copy link
Member

Understood.
__ptr -> ptr no problem. Agreed.

For the rest: I guess they make sense, BUT we'd need to roll 3.0 for this.
Thankfully, it does not change ABI but only API, so we don't have to bump the .so-Version.

The big question is: Is it worth to go to 3.0 over this ?

@JuergenReppSIT
Copy link
Member

The big question is: Is it worth to go to 3.0 over this ?

Maybe we could first split the PR and extract the "real" bugfxes?

@joholl joholl force-pushed the clang_tidy branch 2 times, most recently from 1e31c32 to 3852344 Compare June 10, 2024 12:30
@joholl
Copy link
Collaborator Author

joholl commented Jun 10, 2024

@JuergenReppSIT Alright, I split the changes. The API braking changes are currently on joholl/tpm2-tss@clang_tidy_api_changes. I'm going to submit a PR once this is merged. We could roll that out when we roll a new major version anyway due to another API change (@AndreasFuchsTPM I assume you meant 5.0).

Any more feedback?

@AndreasFuchsTPM
Copy link
Member

@joholl Scan-Build complains about

../src/tss2-fapi/api/Fapi_GetTpmBlobs.c:221:32: warning: Result of 'malloc' is converted to a pointer of type 'uint8_t', which is incompatible with sizeof operand type 'TPM2B_PUBLIC'
                *tpm2bPublic = malloc(sizeof(TPM2B_PUBLIC));
                               ^~~~~~ ~~~~~~~~~~~~~~~~~~~~

Johannes Holland added 9 commits July 5, 2024 09:13
Add clang-tidy config which makes clang-tidy succeed for calling:
   run-clang-tidy-14 src/**/*.c

Signed-off-by: Johannes Holland <[email protected]>
@AndreasFuchsTPM AndreasFuchsTPM merged commit f5d851e into tpm2-software:master Jul 5, 2024
25 checks passed
@AndreasFuchsTPM AndreasFuchsTPM added this to the 4.2.0 milestone Jul 5, 2024
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.

Tss2_PolicyGetDescription() does not null-terminate
3 participants