Skip to content

Commit

Permalink
tpm.c: Don't try to put -1 in a variable of type TpmModel
Browse files Browse the repository at this point in the history
The TpmModel type is an enum (valid values 0 and 1), which means
the compiler can legitimately decide that comparisons like
'tpm_models[i] == -1' are never true. (For example it could
pick 'unsigned char' as its type for representing the enum.)

Avoid this issue by using TPM_MODEL_MAX to mark entries in
the tpm_models[] array which aren't filled in, instead of -1.

This silences a clang warning:

 tpm.c:43:27: error: comparison of constant -1 with expression of type
      'enum TpmModel' is always false [-Werror,-Wtautological-constant-out-of-range-compare]
        if (tpm_models[i] == -1) {
            ~~~~~~~~~~~~~ ^  ~~

Signed-off-by: Peter Maydell <[email protected]>
Message-id: [email protected]
Signed-off-by: Anthony Liguori <[email protected]>
  • Loading branch information
pm215 authored and Anthony Liguori committed Jul 29, 2013
1 parent 125ee0e commit 8cdd2e0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
};

static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
-1,
TPM_MODEL_MAX,
};

int tpm_register_model(enum TpmModel model)
{
int i;

for (i = 0; i < TPM_MAX_MODELS; i++) {
if (tpm_models[i] == -1) {
if (tpm_models[i] == TPM_MODEL_MAX) {
tpm_models[i] = model;
return 0;
}
Expand Down

0 comments on commit 8cdd2e0

Please sign in to comment.