Skip to content

Commit 7b538d1

Browse files
JuergenReppSITAndreasFuchsTPM
authored andcommitted
tools: Fix several clang-tidy errors
Several clang-tidy error related to conversion from uint to int, rewind, and realloc were fixed. Signed-off-by: Juergen Repp <[email protected]>
1 parent c539cb2 commit 7b538d1

File tree

12 files changed

+74
-36
lines changed

12 files changed

+74
-36
lines changed

lib/files.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ static bool load_tpm_context_file(FILE *fstream, TPMS_CONTEXT *context) {
334334
LOG_WARN("The loaded tpm context does not appear to be in the proper "
335335
"format, assuming old format, this will be converted on the "
336336
"next save.");
337-
rewind(fstream);
337+
if (fseek(fstream, 0, SEEK_SET) != 0) {
338+
LOG_ERR("Could not rewind stream: %s", strerror(errno));
339+
return false;
340+
}
338341
result = files_read_bytes(fstream, (UINT8 *) context, sizeof(*context));
339342
if (!result) {
340343
LOG_ERR("Could not load tpm context file");
@@ -407,7 +410,7 @@ static bool check_magic(FILE *fstream, bool seek_reset) {
407410
bool match = magic == MAGIC;
408411

409412
if (seek_reset) {
410-
int rc = fseek(fstream, -sizeof(magic), SEEK_CUR);
413+
int rc = fseek(fstream, -(long)sizeof(magic), SEEK_CUR);
411414
if (rc != 0) {
412415
LOG_ERR("fseek failed: %s", strerror(errno));
413416
return false;

lib/pcr.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,25 @@ static bool pcr_parse_list(const char *str, size_t len,
7474
current_string = str;
7575
str = memchr(current_string, ',', len);
7676
if (str) {
77-
current_length = str - current_string;
77+
ptrdiff_t diff = str - current_string;
78+
if (diff > INT_MAX)
79+
return false;
80+
current_length = (int)diff;
7881
str++;
7982
len -= current_length + 1;
8083
} else {
81-
current_length = len;
84+
current_length = (int)len;
8285
len = 0;
8386
}
8487

8588
dgst = memchr(current_string, '=', current_length);
8689
if (dgst && ((str == NULL) || (str && dgst < str))) {
87-
pcr_len = dgst - current_string;
90+
pcr_len = (int)(dgst - current_string);
8891
dgst++;
8992
if (str) {
90-
dgst_len = str - dgst - 1;
93+
dgst_len = (int)(str - dgst - 1);
9194
} else {
92-
dgst_len = current_length - pcr_len - 1;
95+
dgst_len = (int)(current_length - pcr_len - 1);
9396
}
9497
} else {
9598
dgst = NULL;

lib/tpm2_convert.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ bool tpm2_base64_encode(BYTE *buffer, size_t buffer_length, char *base64) {
806806
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
807807
EVP_EncodeInit(ctx);
808808

809-
int rc = EVP_EncodeUpdate(ctx, out, &outl, buffer, buffer_length);
809+
int rc = EVP_EncodeUpdate(ctx, out, &outl, buffer, (int)buffer_length);
810810
if(rc < 0) {
811811
LOG_ERR("EVP_DecodeUpdate failed with %d\n", rc);
812812
EVP_ENCODE_CTX_free(ctx);
@@ -824,19 +824,20 @@ bool tpm2_base64_encode(BYTE *buffer, size_t buffer_length, char *base64) {
824824

825825
bool tpm2_base64_decode(char *base64, BYTE *buffer, size_t *buffer_length) {
826826

827-
bool is_base64_bufferlen_valid = strlen(base64) > 1024 ? false : true;
827+
size_t len = strlen(base64);
828+
bool is_base64_bufferlen_valid = len > 1024 ? false : true;
828829
if (!is_base64_bufferlen_valid) {
829830
return false;
830831
}
831832

832833
unsigned char base64u[1024];
833-
memcpy(base64u, base64, strlen(base64));
834+
memcpy(base64u, base64, len);
834835

835836
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
836837
EVP_DecodeInit(ctx);
837838
unsigned char out[1024];
838839
int outl;
839-
int rc = EVP_DecodeUpdate(ctx, out, &outl, base64u, strlen(base64));
840+
int rc = EVP_DecodeUpdate(ctx, out, &outl, base64u, (int)len);
840841
if(rc < 0) {
841842
LOG_ERR("EVP_DecodeUpdate failed with %d\n", rc);
842843
EVP_ENCODE_CTX_free(ctx);

lib/tpm2_eventlog_yaml.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static char *yaml_utf16_to_str(UTF16_CHAR *data, size_t len) {
190190
}
191191

192192
for(size_t i = 0; i < len; ++i, tmp += ret) {
193-
ret = c16rtomb(tmp, le16toh(data[i].c), &st);
193+
ret = c16rtomb(tmp, (int)le16toh(data[i].c), &st);
194194
if (ret < 0) {
195195
LOG_ERR("c16rtomb failed: %s", strerror(errno));
196196
free(mbstr);
@@ -279,7 +279,7 @@ static bool yaml_uefi_hcrtm(const TCG_EVENT2* const event) {
279279
#ifdef HAVE_EFIVAR_EFIVAR_H
280280
char *yaml_devicepath(BYTE* dp, UINT64 dp_len) {
281281
int ret;
282-
ret = efidp_format_device_path(NULL, 0, (const_efidp)dp, dp_len);
282+
ret = (int)efidp_format_device_path(NULL, 0, (const_efidp)dp, (ssize_t)dp_len);
283283
if (ret < 0) {
284284
LOG_ERR("failed to allocate memory: %s\n", strerror(errno));
285285
return NULL;
@@ -295,8 +295,8 @@ char *yaml_devicepath(BYTE* dp, UINT64 dp_len) {
295295
}
296296

297297
/* The void* cast is a hack to support efivar versions < 38 */
298-
ret = efidp_format_device_path((void *)text_path,
299-
text_path_len, (const_efidp)dp, dp_len);
298+
ret = (int)efidp_format_device_path((void *)text_path,
299+
text_path_len, (const_efidp)dp, (ssize_t)dp_len);
300300
if (ret < 0) {
301301
free(text_path);
302302
LOG_ERR("cannot parse device path\n");
@@ -351,7 +351,7 @@ char **yaml_split_escape_string(UINT8 const *description, size_t size)
351351
len = size - i;
352352
}
353353

354-
tmp = realloc(lines, sizeof(char *) * (nlines + 2));
354+
tmp = (char **)realloc(lines, sizeof(char *) * (nlines + 2));
355355
if (!tmp) {
356356
LOG_ERR("failed to allocate memory for description lines: %s\n",
357357
strerror(errno));
@@ -422,7 +422,7 @@ char **yaml_split_escape_string(UINT8 const *description, size_t size)
422422
}
423423

424424
if (escape == NULL) {
425-
lines[nlines][k++] = description[j];
425+
lines[nlines][k++] = (char)description[j];
426426
} else {
427427
while (*escape) {
428428
lines[nlines][k++] = *escape;
@@ -440,7 +440,7 @@ char **yaml_split_escape_string(UINT8 const *description, size_t size)
440440
for (i = 0; lines != NULL && lines[i] != NULL; i++) {
441441
free(lines[i]);
442442
}
443-
free(lines);
443+
free((char **)lines);
444444
return NULL;
445445
}
446446

@@ -575,7 +575,7 @@ static bool yaml_uefi_var(UEFI_VARIABLE_DATA *data, size_t size, UINT32 type,
575575

576576
uint8_t *signature = (uint8_t *)slist +
577577
sizeof(*slist) + le32toh(slist->SignatureHeaderSize);
578-
int signatures = signature_size / le32toh(slist->SignatureSize);
578+
int signatures = (int)(signature_size / le32toh(slist->SignatureSize));
579579
/* iterate through each EFI_SIGNATURE on the list */
580580
int i;
581581
for (i = 0; i < signatures; i++) {

lib/tpm2_identity_util.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: BSD-3-Clause */
22

33
#include <stdbool.h>
4+
#include <stdint.h>
45
#include <stdlib.h>
56
#include <string.h>
67

@@ -249,8 +250,13 @@ static bool aes_encrypt_buffers(TPMT_SYM_DEF_OBJECT *sym,
249250
if (!b) {
250251
continue;
251252
}
253+
size_t diff = total_len - offset;
254+
if (diff > INT_MAX || l > INT_MAX) {
255+
LOG_ERR("Size can't be converted to int");
256+
return false;
257+
}
252258

253-
int output_len = total_len - offset;
259+
int output_len = (int)diff;
254260

255261
rc = EVP_EncryptUpdate(ctx, &cipher_text->buffer[offset], &output_len,
256262
b, l);

lib/tpm2_openssl.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,15 @@ static bool do_file(const char *path, char **pass) {
466466
static bool do_fd(const char *passin, char **pass) {
467467

468468
char *end_ptr = NULL;
469-
int fd = strtoul(passin, &end_ptr, 0);
469+
unsigned long tmp = strtoul(passin, &end_ptr, 0);
470+
471+
if (tmp > INT_MAX) {
472+
LOG_ERR("Invalid fd (out of range), got: \"%s\"", passin);
473+
return false;
474+
}
475+
476+
int fd = (int)tmp;
477+
470478
if (passin[0] != '\0' && end_ptr[0] != '\0') {
471479
LOG_ERR("Invalid fd, got: \"%s\"", passin);
472480
return false;
@@ -793,6 +801,7 @@ static bool load_public_ECC_from_key(EVP_PKEY *key, TPM2B_PUBLIC *pub) {
793801
goto out;
794802
}
795803
pp->curveID = curve_id;
804+
unsigned int tmp;
796805

797806
/*
798807
* Copy the X and Y coordinate data into the ECC unique field,
@@ -813,13 +822,25 @@ static bool load_public_ECC_from_key(EVP_PKEY *key, TPM2B_PUBLIC *pub) {
813822
goto out;
814823
}
815824

816-
X->size = BN_bn2binpad(x, X->buffer, keysize);
825+
tmp = BN_bn2binpad(x, X->buffer, (int)keysize);
826+
827+
if (tmp > INT_MAX) {
828+
LOG_ERR("Invalid result of BN_bn2binpad");
829+
return false;
830+
}
831+
X->size = tmp;
817832
if (X->size != keysize) {
818833
LOG_ERR("Error converting X point BN to binary");
819834
goto out;
820835
}
821836

822-
Y->size = BN_bn2binpad(y, Y->buffer, keysize);
837+
tmp = BN_bn2binpad(y, Y->buffer, (int)keysize);
838+
if (tmp > INT_MAX) {
839+
LOG_ERR("Invalid result of BN_bn2binpad");
840+
return false;
841+
}
842+
843+
Y->size = (int)tmp;
823844
if (Y->size != keysize) {
824845
LOG_ERR("Error converting Y point BN to binary");
825846
goto out;
@@ -1073,7 +1094,7 @@ static bool load_private_ECC_from_key(EVP_PKEY *key, TPM2B_SENSITIVE *priv) {
10731094
goto out;
10741095
}
10751096

1076-
p->size = BN_bn2binpad(b, p->buffer, priv_bytes);
1097+
p->size = BN_bn2binpad(b, p->buffer, (int)priv_bytes);
10771098
if (p->size != priv_bytes) {
10781099
goto out;
10791100
}

lib/tpm2_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ int tpm2_util_hex_to_byte_structure(const char *input_string, UINT16 *byte_lengt
188188
int i = 0;
189189
if (input_string == NULL || byte_length == NULL || byte_buffer == NULL)
190190
return -1;
191-
str_length = strlen(input_string);
191+
str_length = (int)strlen(input_string);
192192
if (str_length % 2)
193193
return -2;
194194
for (i = 0; i < str_length; i++) {

tools/fapi/tss2_gettpm2object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
8181
return 1;
8282
}
8383

84-
if (strcmp(ctx.data, "-")) {
84+
if (strcmp(ctx.data, "-") != 0) {
8585
if (!ctx.overwrite) {
8686
FILE *fp = fopen(ctx.data, "rb");
8787
if (fp) {

tools/fapi/tss2_quote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
141141

142142
/* Read qualifyingData file */
143143
TSS2_RC r;
144-
uint8_t *qualifyingData = NULL;
144+
void *qualifyingData = NULL;
145145
size_t qualifyingDataSize = 0;
146146
if (ctx.qualifyingData) {
147147
r = open_read_and_close (ctx.qualifyingData,

tools/fapi/tss2_template.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static tpm2_option_code tss2_handle_options (
142142
case '?':
143143
goto out;
144144
default:
145-
if (!(*tool_opts)->callbacks.on_opt(c, optarg))
145+
if (!(*tool_opts)->callbacks.on_opt((char)c, optarg))
146146
goto out;
147147
}
148148
}
@@ -293,8 +293,8 @@ TSS2_RC sign_callback(
293293
int cpy_size = 0;
294294
if (strlen(publicKeyHint) > 0) {
295295
const char* tmp = "the key corresponding to the key hint \"%s\" and";
296-
cpy_size = strlen(tmp) - 2 /* remove replaced %s */ +
297-
strlen(publicKeyHint);
296+
cpy_size = (int)(strlen(tmp) - 2 /* remove replaced %s */ +
297+
strlen(publicKeyHint));
298298
rc = snprintf(publicKeyHintStr, cpy_size+1 /* add \0 */, tmp,
299299
publicKeyHint);
300300
if (rc != cpy_size){
@@ -311,8 +311,8 @@ TSS2_RC sign_callback(
311311
"PEM-encoded public key\n");
312312
return TSS2_FAPI_RC_GENERAL_FAILURE;
313313
}
314-
cpy_size = strlen(tmp) - 2 /* remove replaced %s */ +
315-
strlen(publicKeyHintTmp);
314+
cpy_size = (int)(strlen(tmp) - 2 /* remove replaced %s */ +
315+
strlen(publicKeyHintTmp));
316316
rc = snprintf(publicKeyHintStr, cpy_size+1 /* add \0 */, tmp,
317317
publicKeyHintTmp);
318318
if (rc != cpy_size){

0 commit comments

Comments
 (0)