From 23a5c74ac7e8dff96850851639647dfa2403b9bf Mon Sep 17 00:00:00 2001 From: Minggui Cao Date: Wed, 18 Jul 2018 17:08:52 +0800 Subject: [PATCH] HV: handle integral issues as MISRA-C report mainly focus on: like U/UL as unsigned suffix; char and int mix usage; also change some function's params for data type consistent. Signed-off-by: Minggui Cao Acked-by: Eddie Dong --- hypervisor/arch/x86/trusty.c | 8 +-- hypervisor/arch/x86/trusty2.c | 2 +- hypervisor/boot/sbl/hob_parse.c | 14 ++-- hypervisor/boot/sbl/multiboot.c | 31 +++++---- hypervisor/include/arch/x86/hob_parse.h | 8 +-- hypervisor/include/arch/x86/trusty.h | 4 +- hypervisor/include/lib/sprintf.h | 2 +- hypervisor/lib/div.c | 4 +- hypervisor/lib/mdelay.c | 4 +- hypervisor/lib/memory.c | 10 +-- hypervisor/lib/spinlock.c | 2 +- hypervisor/lib/sprintf.c | 89 +++++++++++++------------ hypervisor/lib/string.c | 32 ++++----- 13 files changed, 106 insertions(+), 104 deletions(-) diff --git a/hypervisor/arch/x86/trusty.c b/hypervisor/arch/x86/trusty.c index 81b2fea35..b599d876f 100644 --- a/hypervisor/arch/x86/trusty.c +++ b/hypervisor/arch/x86/trusty.c @@ -359,7 +359,7 @@ static bool setup_trusty_info(struct vcpu *vcpu, (void)memcpy_s(&mem->first_page.data.key_info, sizeof(g_key_info), &g_key_info, sizeof(g_key_info)); - (void)memset(mem->first_page.data.key_info.dseed_list, 0, + (void)memset(mem->first_page.data.key_info.dseed_list, 0U, sizeof(mem->first_page.data.key_info.dseed_list)); /* Derive dvseed from dseed for Trusty */ key_info = &mem->first_page.data.key_info; @@ -370,7 +370,7 @@ static bool setup_trusty_info(struct vcpu *vcpu, BUP_MKHI_BOOTLOADER_SEED_LEN, NULL, 0U, vcpu->vm->GUID, sizeof(vcpu->vm->GUID)) == 0) { - (void)memset(key_info, 0, sizeof(struct key_info)); + (void)memset(key_info, 0U, sizeof(struct key_info)); pr_err("%s: derive dvseed failed!", __func__); return false; } @@ -439,7 +439,7 @@ bool initialize_trusty(struct vcpu *vcpu, uint64_t param) struct vm *vm = vcpu->vm; struct trusty_boot_param boot_param; - memset(&boot_param, 0, sizeof(boot_param)); + memset(&boot_param, 0U, sizeof(boot_param)); if (copy_from_gpa(vcpu->vm, &boot_param, param, sizeof(boot_param))) { pr_err("%s: Unable to copy trusty_boot_param\n", __func__); return false; @@ -497,7 +497,7 @@ void trusty_set_dseed(void *dseed, uint8_t dseed_num) (dseed_num > BOOTLOADER_SEED_MAX_ENTRIES)) { g_key_info.num_seeds = 1U; - (void)memset(g_key_info.dseed_list[0].seed, 0xA5, + (void)memset(g_key_info.dseed_list[0].seed, 0xA5U, sizeof(g_key_info.dseed_list[0].seed)); return; } diff --git a/hypervisor/arch/x86/trusty2.c b/hypervisor/arch/x86/trusty2.c index b1d3aad6e..6864e198d 100644 --- a/hypervisor/arch/x86/trusty2.c +++ b/hypervisor/arch/x86/trusty2.c @@ -53,7 +53,7 @@ uint64_t gpa2hpa_for_trusty(struct vm *vm, uint64_t gpa) * */ void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig, - int64_t size, uint64_t gpa_rebased) + uint64_t size, uint64_t gpa_rebased) { uint64_t nworld_pml4e = 0UL; uint64_t sworld_pml4e = 0UL; diff --git a/hypervisor/boot/sbl/hob_parse.c b/hypervisor/boot/sbl/hob_parse.c index c1a8ce8cc..4b80c931e 100644 --- a/hypervisor/boot/sbl/hob_parse.c +++ b/hypervisor/boot/sbl/hob_parse.c @@ -10,7 +10,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob) { uint8_t i; - uint8_t dseed_index = 0; + uint8_t dseed_index = 0U; struct seed_entry *entry; struct seed_info dseed_list[BOOTLOADER_SEED_MAX_ENTRIES]; @@ -19,7 +19,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob) goto fail; } - if (seed_hob->total_seed_count == 0) { + if (seed_hob->total_seed_count == 0U) { pr_warn("Total seed count is 0. Use fake seed!"); goto fail; } @@ -27,7 +27,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob) entry = (struct seed_entry *)((uint8_t *)seed_hob + sizeof(struct seed_list_hob)); - for (i = 0; i < seed_hob->total_seed_count; i++) { + for (i = 0U; i < seed_hob->total_seed_count; i++) { /* retrieve dseed */ if ((SEED_ENTRY_TYPE_SVNSEED == entry->type) && (SEED_ENTRY_USAGE_DSEED == entry->usage)) { @@ -52,7 +52,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob) dseed_index++; /* erase original seed in seed entry */ - (void)memset(entry->seed, 0, sizeof(struct seed_info)); + (void)memset(entry->seed, 0U, sizeof(struct seed_info)); } entry = (struct seed_entry *)((uint8_t *)entry + @@ -60,10 +60,10 @@ void parse_seed_list(struct seed_list_hob *seed_hob) } trusty_set_dseed(dseed_list, dseed_index); - (void)memset(dseed_list, 0, sizeof(dseed_list)); + (void)memset(dseed_list, 0U, sizeof(dseed_list)); return; fail: - trusty_set_dseed(NULL, 0); - (void)memset(dseed_list, 0, sizeof(dseed_list)); + trusty_set_dseed(NULL, 0U); + (void)memset(dseed_list, 0U, sizeof(dseed_list)); } diff --git a/hypervisor/boot/sbl/multiboot.c b/hypervisor/boot/sbl/multiboot.c index fea31092e..5b4e0d868 100644 --- a/hypervisor/boot/sbl/multiboot.c +++ b/hypervisor/boot/sbl/multiboot.c @@ -11,7 +11,7 @@ #define BOOT_ARGS_LOAD_ADDR 0x24EFC000 -#define ACRN_DBG_BOOT 6 +#define ACRN_DBG_BOOT 6U /* There are two sources for vm0 kernel cmdline: * - cmdline from sbl. mbi->cmdline @@ -22,12 +22,12 @@ static char kernel_cmdline[MEM_2K]; /* now modules support: FIRMWARE & RAMDISK & SeedList */ static void parse_other_modules(struct vm *vm, - struct multiboot_module *mods, int mods_count) + struct multiboot_module *mods, uint32_t mods_count) { - int i = 0; + uint32_t i; - for (i = 0; i < mods_count; i++) { - int type_len = 0; + for (i = 0U; i < mods_count; i++) { + uint32_t type_len; const char *start = HPA2HVA((uint64_t)mods[i].mm_string); const char *end; void *mod_addr = HPA2HVA((uint64_t)mods[i].mm_mod_start); @@ -43,13 +43,13 @@ static void parse_other_modules(struct vm *vm, } end = start; - while (*end != ' ' && (*end) != 0) { + while (*end != ' ' && (*end) != '\0') { end++; } type_len = end - start; if (strncmp("FIRMWARE", start, type_len) == 0) { - char dyn_bootargs[100] = {0}; + char dyn_bootargs[100] = {'\0'}; void *load_addr = GPA2HVA(vm, (uint64_t)vm->sw.linux_info.bootargs_load_addr); uint32_t args_size = vm->sw.linux_info.bootargs_size; @@ -63,23 +63,23 @@ static void parse_other_modules(struct vm *vm, /*copy boot args to load addr, set src=load addr*/ if (copy_once != 0) { copy_once = 0; - (void)strcpy_s(load_addr, MEM_2K, - vm->sw.linux_info.bootargs_src_addr); + (void)strcpy_s(load_addr, MEM_2K, (const + char *)vm->sw.linux_info.bootargs_src_addr); vm->sw.linux_info.bootargs_src_addr = load_addr; } (void)strcpy_s(load_addr + args_size, - 100, dyn_bootargs); + 100U, dyn_bootargs); vm->sw.linux_info.bootargs_size = strnlen_s(load_addr, MEM_2K); } else if (strncmp("RAMDISK", start, type_len) == 0) { vm->sw.linux_info.ramdisk_src_addr = mod_addr; vm->sw.linux_info.ramdisk_load_addr = - mods[i].mm_mod_start; + (void *)(uint64_t)mods[i].mm_mod_start; vm->sw.linux_info.ramdisk_size = mod_size; } else if (strncmp("SeedList", start, type_len) == 0) { - parse_seed_list(mod_addr); + parse_seed_list((struct seed_list_hob *)mod_addr); } else { pr_warn("not support mod, cmd: %s", start); } @@ -151,14 +151,13 @@ int init_vm0_boot_info(struct vm *vm) vm->sw.kernel_info.kernel_load_addr = (void *)HVA2GPA(vm, get_kernel_load_addr(vm->sw.kernel_info.kernel_src_addr)); - /* * If there is cmdline from mbi->mi_cmdline, merge it with * mods[0].mm_string */ if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE) != 0U) { char *cmd_src, *cmd_dst; - int off = 0; + uint32_t off; cmd_dst = kernel_cmdline; cmd_src = HPA2HVA((uint64_t)mbi->mi_cmdline); @@ -166,7 +165,7 @@ int init_vm0_boot_info(struct vm *vm) strnlen_s(cmd_src, MEM_2K)); off = strnlen_s(cmd_dst, MEM_2K); cmd_dst[off] = ' '; /* insert space */ - off += 1; + off += 1U; cmd_dst += off; cmd_src = HPA2HVA((uint64_t)mods[0].mm_string); @@ -186,7 +185,7 @@ int init_vm0_boot_info(struct vm *vm) vm->sw.linux_info.bootargs_load_addr = (void *)BOOT_ARGS_LOAD_ADDR; - if (mbi->mi_mods_count > 1) { + if (mbi->mi_mods_count > 1U) { /*parse other modules, like firmware /ramdisk */ parse_other_modules(vm, mods + 1, mbi->mi_mods_count - 1); } diff --git a/hypervisor/include/arch/x86/hob_parse.h b/hypervisor/include/arch/x86/hob_parse.h index a5b819975..35f64b5a1 100644 --- a/hypervisor/include/arch/x86/hob_parse.h +++ b/hypervisor/include/arch/x86/hob_parse.h @@ -7,11 +7,11 @@ #ifndef HOB_PARSE_H_ #define HOB_PARSE_H_ -#define SEED_ENTRY_TYPE_SVNSEED 0x1 -#define SEED_ENTRY_TYPE_RPMBSEED 0x2 +#define SEED_ENTRY_TYPE_SVNSEED 0x1U +#define SEED_ENTRY_TYPE_RPMBSEED 0x2U -#define SEED_ENTRY_USAGE_USEED 0x1 -#define SEED_ENTRY_USAGE_DSEED 0x2 +#define SEED_ENTRY_USAGE_USEED 0x1U +#define SEED_ENTRY_USAGE_DSEED 0x2U struct seed_list_hob { uint8_t revision; diff --git a/hypervisor/include/arch/x86/trusty.h b/hypervisor/include/arch/x86/trusty.h index d92b07d19..5cc864cd4 100644 --- a/hypervisor/include/arch/x86/trusty.h +++ b/hypervisor/include/arch/x86/trusty.h @@ -14,7 +14,7 @@ #define RPMB_MAX_PARTITION_NUMBER 6 #define MMC_PROD_NAME_WITH_PSN_LEN 15 -#define BUP_MKHI_BOOTLOADER_SEED_LEN 64 +#define BUP_MKHI_BOOTLOADER_SEED_LEN 64U /* Trusty EPT rebase gpa: 511G */ #define TRUSTY_EPT_REBASE_GPA (511UL * 1024UL * 1024UL * 1024UL) @@ -96,7 +96,7 @@ struct key_info { #ifdef WORKAROUND_FOR_TRUSTY_4G_MEM void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig, - int64_t size, uint64_t gpa_rebased); + uint64_t size, uint64_t gpa_rebased); struct secure_world_memory { /* The secure world base address of GPA for secure world*/ uint64_t base_gpa; diff --git a/hypervisor/include/lib/sprintf.h b/hypervisor/include/lib/sprintf.h index 2e3ef976a..17504797a 100644 --- a/hypervisor/include/lib/sprintf.h +++ b/hypervisor/include/lib/sprintf.h @@ -24,7 +24,7 @@ struct print_param { /* Contains variables which are recalculated for each argument. */ struct { /* A bitfield with the parsed format flags. */ - int flags; + uint32_t flags; /* The parsed format width. */ int width; /* The parsed format precision. */ diff --git a/hypervisor/lib/div.c b/hypervisor/lib/div.c index 1ebed962d..e3b634dc0 100644 --- a/hypervisor/lib/div.c +++ b/hypervisor/lib/div.c @@ -43,11 +43,11 @@ int udiv32(uint32_t dividend, uint32_t divisor, struct udiv_result *res) /* test for "division by 0" condition */ if (divisor == 0U) { res->q.dwords.low = 0xffffffffU; - return !0; + return 1; } /* trivial case: divisor==dividend */ if (divisor == dividend) { - res->q.dwords.low = 1; + res->q.dwords.low = 1U; return 0; } /* trivial case: divisor>dividend */ diff --git a/hypervisor/lib/mdelay.c b/hypervisor/lib/mdelay.c index 6a3819526..8f5fbde35 100644 --- a/hypervisor/lib/mdelay.c +++ b/hypervisor/lib/mdelay.c @@ -9,9 +9,9 @@ void mdelay(uint32_t loop_count) { /* Loop until done */ - while (loop_count != 0) { + while (loop_count != 0U) { /* Delay for 1 ms */ - udelay(1000); + udelay(1000U); loop_count--; } } diff --git a/hypervisor/lib/memory.c b/hypervisor/lib/memory.c index 5179e5637..c77ad5d8f 100644 --- a/hypervisor/lib/memory.c +++ b/hypervisor/lib/memory.c @@ -95,7 +95,7 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes) /* Check requested_buffs number of buffers availability * in memory-pool right after selected buffer */ - for (i = 1; i < requested_buffs; i++) { + for (i = 1U; i < requested_buffs; i++) { /* Check if tmp_bit_idx is out-of-range */ tmp_bit_idx++; if (tmp_bit_idx == BITMAP_WORD_SIZE) { @@ -244,8 +244,8 @@ void *malloc(unsigned int num_bytes) */ memory = allocate_mem(&Memory_Pool, num_bytes); } else { - int page_num = - (num_bytes + CPU_PAGE_SIZE - 1) >> CPU_PAGE_SHIFT; + uint32_t page_num = + (num_bytes + CPU_PAGE_SIZE - 1U) >> CPU_PAGE_SHIFT; /* Request memory allocation through alloc_page */ memory = alloc_pages(page_num); } @@ -276,7 +276,7 @@ void *alloc_pages(unsigned int page_num) void *alloc_page(void) { - return alloc_pages(1); + return alloc_pages(1U); } void *calloc(unsigned int num_elements, unsigned int element_size) @@ -286,7 +286,7 @@ void *calloc(unsigned int num_elements, unsigned int element_size) /* Determine if memory was allocated */ if (memory != NULL) { /* Zero all the memory */ - (void)memset(memory, 0, num_elements * element_size); + (void)memset(memory, 0U, num_elements * element_size); } /* Return pointer to memory */ diff --git a/hypervisor/lib/spinlock.c b/hypervisor/lib/spinlock.c index 13d742640..833a374b9 100644 --- a/hypervisor/lib/spinlock.c +++ b/hypervisor/lib/spinlock.c @@ -8,7 +8,7 @@ inline void spinlock_init(spinlock_t *lock) { - (void)memset(lock, 0, sizeof(spinlock_t)); + (void)memset(lock, 0U, sizeof(spinlock_t)); } void spinlock_obtain(spinlock_t *lock) { diff --git a/hypervisor/lib/sprintf.c b/hypervisor/lib/sprintf.c index a5b9ef43b..29254e77a 100644 --- a/hypervisor/lib/sprintf.c +++ b/hypervisor/lib/sprintf.c @@ -108,12 +108,12 @@ static const char *get_int(const char *s, int *x) return s; } -static const char *get_flags(const char *s, int *flags) +static const char *get_flags(const char *s, uint32_t *flags) { /* contains the flag characters */ static const char flagchars[] = "#0- +"; /* contains the numeric flags for the characters above */ - static const int fl[sizeof(flagchars)] = { + static const uint32_t fl[sizeof(flagchars)] = { PRINT_FLAG_ALTERNATE_FORM, /* # */ PRINT_FLAG_PAD_ZERO, /* 0 */ PRINT_FLAG_LEFT_JUSTIFY, /* - */ @@ -124,7 +124,7 @@ static const char *get_flags(const char *s, int *flags) bool found; /* parse multiple flags */ - while ((*s) != 0) { + while ((*s) != '\0') { /* * Get index of flag. * Terminate loop if no flag character was found. @@ -146,12 +146,12 @@ static const char *get_flags(const char *s, int *flags) } /* Spec says that '-' has a higher priority than '0' */ - if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { + if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) { *flags &= ~PRINT_FLAG_PAD_ZERO; } /* Spec says that '+' has a higher priority than ' ' */ - if ((*flags & PRINT_FLAG_SIGN) != 0) { + if ((*flags & PRINT_FLAG_SIGN) != 0U) { *flags &= ~PRINT_FLAG_SPACE; } @@ -159,18 +159,18 @@ static const char *get_flags(const char *s, int *flags) } static const char *get_length_modifier(const char *s, - int *flags, uint64_t *mask) + uint32_t *flags, uint64_t *mask) { if (*s == 'h') { /* check for h[h] (char/short) */ s++; if (*s == 'h') { *flags |= PRINT_FLAG_CHAR; - *mask = 0x000000FF; + *mask = 0x000000FFU; ++s; } else { *flags |= PRINT_FLAG_SHORT; - *mask = 0x0000FFFF; + *mask = 0x0000FFFFU; } } else if (*s == 'l') { /* check for l[l] (long/long long) */ @@ -206,17 +206,19 @@ static int format_number(struct print_param *param) width = param->vars.valuelen + param->vars.prefixlen; /* calculate additional characters for precision */ - if ((uint32_t)(param->vars.precision) > width) { - p = param->vars.precision - width; + p = (uint32_t)(param->vars.precision); + if (p > width) { + p = p - width; } /* calculate additional characters for width */ - if ((uint32_t)(param->vars.width) > (width + p)) { - w = param->vars.width - (width + p); + w = (uint32_t)(param->vars.width); + if (w > (width + p)) { + w = w - (width + p); } /* handle case of right justification */ - if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { + if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) { /* assume ' ' as padding character */ pad = ' '; @@ -226,7 +228,7 @@ static int format_number(struct print_param *param) * used for padding, the prefix is emitted after the padding. */ - if ((param->vars.flags & PRINT_FLAG_PAD_ZERO) != 0) { + if ((param->vars.flags & PRINT_FLAG_PAD_ZERO) != 0U) { /* use '0' for padding */ pad = '0'; @@ -274,7 +276,7 @@ static int format_number(struct print_param *param) } /* handle left justification */ - if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { + if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) { /* emit trailing blanks, return early in case of an error */ res = param->emit(PRINT_CMD_FILL, " ", w, param->data); if (res < 0) { @@ -305,14 +307,15 @@ static int print_pow2(struct print_param *param, mask = (1UL << shift) - 1UL; /* determine digit translation table */ - digits = ((param->vars.flags & PRINT_FLAG_UPPER) != 0) ? + digits = ((param->vars.flags & PRINT_FLAG_UPPER) != 0U) ? &upper_hex_digits : &lower_hex_digits; /* apply mask for short/char */ v &= param->vars.mask; /* determine prefix for alternate form */ - if ((v == 0UL) && ((param->vars.flags & PRINT_FLAG_ALTERNATE_FORM) != 0)) { + if ((v == 0UL) && + ((param->vars.flags & PRINT_FLAG_ALTERNATE_FORM) != 0U)) { prefix[0] = '0'; param->vars.prefix = prefix; param->vars.prefixlen = 1U; @@ -363,20 +366,20 @@ static int print_decimal(struct print_param *param, int64_t value) * assign sign and correct value if value is negative and * value must be interpreted as signed */ - if (((param->vars.flags & PRINT_FLAG_UINT32) == 0) && (value < 0)) { + if (((param->vars.flags & PRINT_FLAG_UINT32) == 0U) && (value < 0)) { v.qword = (uint64_t)-value; param->vars.prefix = "-"; - param->vars.prefixlen = 1; + param->vars.prefixlen = 1U; } /* determine sign if explicit requested in the format string */ if (param->vars.prefix == NULL) { - if ((param->vars.flags & PRINT_FLAG_SIGN) != 0) { + if ((param->vars.flags & PRINT_FLAG_SIGN) != 0U) { param->vars.prefix = "+"; - param->vars.prefixlen = 1; - } else if ((param->vars.flags & PRINT_FLAG_SPACE) != 0) { + param->vars.prefixlen = 1U; + } else if ((param->vars.flags & PRINT_FLAG_SPACE) != 0U) { param->vars.prefix = " "; - param->vars.prefixlen = 1; + param->vars.prefixlen = 1U; } else { /* No prefix specified. */ } @@ -385,7 +388,7 @@ static int print_decimal(struct print_param *param, int64_t value) /* process 64 bit value as long as needed */ while (v.dwords.high != 0U) { /* determine digits from right to left */ - udiv64(v.qword, 10, &d); + udiv64(v.qword, 10U, &d); pos--; *pos = d.r.dwords.low + '0'; v.qword = d.q.qword; @@ -397,11 +400,11 @@ static int print_decimal(struct print_param *param, int64_t value) * able to handle a division and multiplication by the constant * 10. */ - nv.dwords.low = v.dwords.low / 10; + nv.dwords.low = v.dwords.low / 10U; pos--; - *pos = (v.dwords.low - (10 * nv.dwords.low)) + '0'; + *pos = (v.dwords.low - (10U * nv.dwords.low)) + '0'; v.dwords.low = nv.dwords.low; - } while (v.dwords.low != 0); + } while (v.dwords.low != 0U); /* assign parameter and apply width and precision */ param->vars.value = pos; @@ -451,7 +454,7 @@ static int print_string(struct print_param *param, const char *s) /* emit additional characters for width, return early if an error * occurred */ - if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { + if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) { res = param->emit(PRINT_CMD_FILL, " ", w, param->data); if (res < 0) { return res; @@ -467,7 +470,7 @@ static int print_string(struct print_param *param, const char *s) /* emit additional characters on the right, return early if an error * occurred */ - if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { + if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) { res = param->emit(PRINT_CMD_FILL, " ", w, param->data); if (res < 0) { return res; @@ -488,11 +491,11 @@ int do_print(const char *fmt, struct print_param *param, const char *start; /* main loop: analyse until there are no more characters */ - while ((*fmt) != 0) { + while ((*fmt) != '\0') { /* mark the current position and search the next '%' */ start = fmt; - while (((*fmt) != 0) && (*fmt != '%')) { + while (((*fmt) != '\0') && (*fmt != '%')) { fmt++; } @@ -513,7 +516,7 @@ int do_print(const char *fmt, struct print_param *param, fmt++; /* initialize the variables for the next argument */ - (void)memset(&(param->vars), 0, sizeof(param->vars)); + (void)memset(&(param->vars), 0U, sizeof(param->vars)); param->vars.mask = 0xFFFFFFFFFFFFFFFFUL; /* @@ -547,7 +550,7 @@ int do_print(const char *fmt, struct print_param *param, /* decimal number */ res = print_decimal(param, ((param->vars.flags & - PRINT_FLAG_LONG_LONG) != 0) ? + PRINT_FLAG_LONG_LONG) != 0U) ? __builtin_va_arg(args, long long) : (long long) @@ -559,7 +562,7 @@ int do_print(const char *fmt, struct print_param *param, param->vars.flags |= PRINT_FLAG_UINT32; res = print_decimal(param, ((param->vars.flags & - PRINT_FLAG_LONG_LONG) != 0) ? + PRINT_FLAG_LONG_LONG) != 0U) ? __builtin_va_arg(args, unsigned long long) : (unsigned long long) @@ -570,13 +573,13 @@ int do_print(const char *fmt, struct print_param *param, else if (ch == 'o') { res = print_pow2(param, ((param->vars.flags & - PRINT_FLAG_LONG_LONG) != 0) ? + PRINT_FLAG_LONG_LONG) != 0U) ? __builtin_va_arg(args, unsigned long long) : (unsigned long long) __builtin_va_arg(args, uint32_t), - 3); + 3U); } /* hexadecimal number */ else if ((ch == 'X') || (ch == 'x')) { @@ -585,13 +588,13 @@ int do_print(const char *fmt, struct print_param *param, } res = print_pow2(param, ((param->vars.flags & - PRINT_FLAG_LONG_LONG) != 0) ? + PRINT_FLAG_LONG_LONG) != 0U) ? __builtin_va_arg(args, unsigned long long) : (unsigned long long) __builtin_va_arg(args, uint32_t), - 4); + 4U); } /* string argument */ else if (ch == 's') { @@ -610,7 +613,7 @@ int do_print(const char *fmt, struct print_param *param, * void *),4); */ res = print_pow2(param, (uint64_t) - __builtin_va_arg(args, void *), 4); + __builtin_va_arg(args, void *), 4U); } /* single character argument */ else if (ch == 'c') { @@ -648,7 +651,7 @@ static int charmem(int cmd, const char *s, int sz, void *hnd) /* copy mode ? */ if (cmd == PRINT_CMD_COPY) { if (sz < 0) { - while ((*s) != 0) { + while ((*s) != '\0') { if (n < param->sz - param->wrtn) { *p = *s; } @@ -658,7 +661,7 @@ static int charmem(int cmd, const char *s, int sz, void *hnd) } } else if (sz > 0) { - while (((*s) != 0) && n < sz) { + while (((*s) != '\0') && n < sz) { if (n < param->sz - param->wrtn) { *p = *s; } @@ -701,10 +704,10 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args) struct snprint_param snparam; /* initialize parameters */ - (void)memset(&snparam, 0, sizeof(snparam)); + (void)memset(&snparam, 0U, sizeof(snparam)); snparam.dst = dst; snparam.sz = sz; - (void)memset(¶m, 0, sizeof(param)); + (void)memset(¶m, 0U, sizeof(param)); param.emit = charmem; param.data = &snparam; diff --git a/hypervisor/lib/string.c b/hypervisor/lib/string.c index c22f1cce7..0edbe07e4 100644 --- a/hypervisor/lib/string.c +++ b/hypervisor/lib/string.c @@ -9,7 +9,7 @@ #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */ #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */ -#define ISSPACE(c) ((((c) & 0xFFU) == ' ') || (((c) & 0xFFU) == '\t')) +#define ISSPACE(c) ((c == ' ') || (c == '\t')) /* * Convert a string to a long integer - decimal support only. @@ -18,7 +18,7 @@ long strtol_deci(const char *nptr) { const char *s = nptr; uint64_t acc; - int c; + char c; uint64_t cutoff; int neg = 0, any, cutlim; int base = 10; @@ -61,7 +61,7 @@ long strtol_deci(const char *nptr) cutoff = (neg != 0) ? -(uint64_t)LONG_MIN : LONG_MAX; cutlim = cutoff % (uint64_t)base; cutoff /= (uint64_t)base; - acc = 0; + acc = 0U; any = 0; do { if (c >= '0' && c <= '9') { @@ -102,7 +102,7 @@ uint64_t strtoul_hex(const char *nptr) { const char *s = nptr; uint64_t acc; - int c; + char c; uint64_t cutoff; int base = 16, any, cutlim; @@ -121,7 +121,7 @@ uint64_t strtoul_hex(const char *nptr) cutoff = (uint64_t)ULONG_MAX / (uint64_t)base; cutlim = (uint64_t)ULONG_MAX % (uint64_t)base; - acc = 0; + acc = 0U; any = 0; do { if (c >= '0' && c <= '9') { @@ -161,11 +161,11 @@ int atoi(const char *str) char *strchr(const char *s, int ch) { - while ((*s != 0) && (*s != ch)) { + while ((*s != '\0') && (*s != ch)) { ++s; } - return ((*s) != 0) ? ((char *)s) : 0; + return ((*s) != '\0') ? ((char *)s) : NULL; } /** @@ -197,7 +197,7 @@ char *strcpy_s(char *d, size_t dmax, const char *s) size_t dest_avail; uint64_t overlap_guard; - if (s == NULL || d == NULL || dmax == 0) { + if (s == NULL || d == NULL || dmax == 0U) { pr_err("%s: invalid src, dest buffer or length.", __func__); return NULL; } @@ -211,8 +211,8 @@ char *strcpy_s(char *d, size_t dmax, const char *s) dest_avail = dmax; dest_base = d; - while (dest_avail > 0) { - if (overlap_guard == 0) { + while (dest_avail > 0U) { + if (overlap_guard == 0U) { pr_err("%s: overlap happened.", __func__); *(--d) = '\0'; return NULL; @@ -291,8 +291,8 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen) dest_base = d; dest_avail = dmax; - while (dest_avail > 0) { - if (overlap_guard == 0) { + while (dest_avail > 0U) { + if (overlap_guard == 0U) { pr_err("%s: overlap happened.", __func__); *(--d) = '\0'; return NULL; @@ -352,7 +352,7 @@ size_t strnlen_s(const char *str, size_t maxlen) } count = 0U; - while ((*str) != 0) { + while ((*str) != '\0') { if (maxlen == 0U) { break; } @@ -365,7 +365,7 @@ size_t strnlen_s(const char *str, size_t maxlen) return count; } -static char hexdigit(int decimal_val) +static char hexdigit(uint8_t decimal_val) { static const char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; @@ -376,7 +376,7 @@ static char hexdigit(int decimal_val) int strcmp(const char *s1, const char *s2) { - while (((*s1) != 0) && ((*s2) != 0) && ((*s1) == (*s2))) { + while (((*s1) != '\0') && ((*s2) != '\0') && ((*s1) == (*s2))) { s1++; s2++; } @@ -386,7 +386,7 @@ int strcmp(const char *s1, const char *s2) int strncmp(const char *s1, const char *s2, size_t n) { - while (((n - 1) != 0) && ((*s1) != 0) && ((*s2) != 0) + while (((n - 1) != 0U) && ((*s1) != '\0') && ((*s2) != '\0') && ((*s1) == (*s2))) { s1++; s2++;