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 <minggui.cao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Minggui Cao
2018-07-18 17:08:52 +08:00
committed by lijinxia
parent 0252ae9e11
commit 23a5c74ac7
13 changed files with 106 additions and 104 deletions

View File

@@ -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 */

View File

@@ -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--;
}
}

View File

@@ -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 */

View File

@@ -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)
{

View File

@@ -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(&param, 0, sizeof(param));
(void)memset(&param, 0U, sizeof(param));
param.emit = charmem;
param.data = &snparam;

View File

@@ -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++;