HV: Fix missing brackets for MISRA C Violations

Patch 7 of 7.
Added changes to make sure Misra C violations are fixed
for rules 11S and 12S.

Signed-off-by: Arindam Roy <arindam.roy@intel.com>
This commit is contained in:
Arindam Roy
2018-07-12 15:03:27 -07:00
committed by wenlingz
parent af806a93bd
commit 1af8586cd1
3 changed files with 106 additions and 58 deletions

View File

@@ -80,8 +80,9 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes)
for (bit_idx = ffz64(pool->bitmap[idx]); for (bit_idx = ffz64(pool->bitmap[idx]);
bit_idx < BITMAP_WORD_SIZE; bit_idx++) { bit_idx < BITMAP_WORD_SIZE; bit_idx++) {
/* Check if selected buffer is free */ /* Check if selected buffer is free */
if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) {
continue; continue;
}
/* Declare temporary variables to be used locally in /* Declare temporary variables to be used locally in
* this block * this block
@@ -109,9 +110,10 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes)
/* Break if selected buffer is not free */ /* Break if selected buffer is not free */
if ((pool->bitmap[tmp_idx] if ((pool->bitmap[tmp_idx]
& (1U << tmp_bit_idx)) != 0U) & (1U << tmp_bit_idx)) != 0U) {
break; break;
} }
}
/* Check if requested_buffs number of free contiguous /* Check if requested_buffs number of free contiguous
* buffers are found in memory pool * buffers are found in memory pool
@@ -207,16 +209,18 @@ static void deallocate_mem(struct mem_pool *pool, void *ptr)
contiguity_bitmask = &pool->contiguity_bitmap[bmp_idx]; contiguity_bitmask = &pool->contiguity_bitmap[bmp_idx];
/* Mark the buffer as free */ /* Mark the buffer as free */
if ((*bitmask & (1U << bit_idx)) != 0U) if ((*bitmask & (1U << bit_idx)) != 0U) {
*bitmask ^= (1U << bit_idx); *bitmask ^= (1U << bit_idx);
else } else {
break; break;
}
/* Reset the Contiguity bit of buffer */ /* Reset the Contiguity bit of buffer */
if ((*contiguity_bitmask & (1U << bit_idx)) != 0U) if ((*contiguity_bitmask & (1U << bit_idx)) != 0U) {
*contiguity_bitmask ^= (1U << bit_idx); *contiguity_bitmask ^= (1U << bit_idx);
else } else {
break; break;
}
/* Increment buff_idx */ /* Increment buff_idx */
buff_idx++; buff_idx++;
@@ -245,8 +249,9 @@ void *malloc(unsigned int num_bytes)
} }
/* Check if memory allocation is successful */ /* Check if memory allocation is successful */
if (memory == NULL) if (memory == NULL) {
pr_err("%s: failed to alloc 0x%x Bytes", __func__, num_bytes); pr_err("%s: failed to alloc 0x%x Bytes", __func__, num_bytes);
}
/* Return memory pointer to caller */ /* Return memory pointer to caller */
return memory; return memory;
@@ -260,8 +265,9 @@ void *alloc_pages(unsigned int page_num)
memory = allocate_mem(&Paging_Memory_Pool, page_num * CPU_PAGE_SIZE); memory = allocate_mem(&Paging_Memory_Pool, page_num * CPU_PAGE_SIZE);
/* Check if memory allocation is successful */ /* Check if memory allocation is successful */
if (memory == NULL) if (memory == NULL) {
pr_err("%s: failed to alloc %d pages", __func__, page_num); pr_err("%s: failed to alloc %d pages", __func__, page_num);
}
return memory; return memory;
} }
@@ -311,8 +317,9 @@ void *memchr(const void *void_s, int c, size_t n)
unsigned char *end = ptr + n; unsigned char *end = ptr + n;
while (ptr < end) { while (ptr < end) {
if (*ptr == val) if (*ptr == val) {
return ((void *)ptr); return ((void *)ptr);
}
ptr++; ptr++;
} }
return NULL; return NULL;
@@ -348,16 +355,19 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen)
uint8_t *dest8; uint8_t *dest8;
uint8_t *src8; uint8_t *src8;
if (slen == 0U || dmax == 0U || dmax < slen) if (slen == 0U || dmax == 0U || dmax < slen) {
ASSERT(false); ASSERT(false);
}
if ((d > s && d <= s + slen - 1) if ((d > s && d <= s + slen - 1)
|| (d < s && s <= d + dmax - 1)) || (d < s && s <= d + dmax - 1)) {
ASSERT(false); ASSERT(false);
}
/*same memory block, no need to copy*/ /*same memory block, no need to copy*/
if (d == s) if (d == s) {
return d; return d;
}
dest8 = (uint8_t *)d; dest8 = (uint8_t *)d;
src8 = (uint8_t *)s; src8 = (uint8_t *)s;

View File

@@ -99,8 +99,9 @@ static const char *get_int(const char *s, int *x)
} }
/* apply sign to result */ /* apply sign to result */
if (negative != 0) if (negative != 0) {
*x = -*x; *x = -*x;
}
return s; return s;
} }
@@ -143,12 +144,14 @@ static const char *get_flags(const char *s, int *flags)
} }
/* Spec says that '-' has a higher priority than '0' */ /* Spec says that '-' has a higher priority than '0' */
if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
*flags &= ~PRINT_FLAG_PAD_ZERO; *flags &= ~PRINT_FLAG_PAD_ZERO;
}
/* Spec says that '+' has a higher priority than ' ' */ /* Spec says that '+' has a higher priority than ' ' */
if ((*flags & PRINT_FLAG_SIGN) != 0) if ((*flags & PRINT_FLAG_SIGN) != 0) {
*flags &= ~PRINT_FLAG_SPACE; *flags &= ~PRINT_FLAG_SPACE;
}
return s; return s;
} }
@@ -167,16 +170,16 @@ static const char *get_length_modifier(const char *s,
*flags |= PRINT_FLAG_SHORT; *flags |= PRINT_FLAG_SHORT;
*mask = 0x0000FFFF; *mask = 0x0000FFFF;
} }
} } else if (*s == 'l') {
/* check for l[l] (long/long long) */ /* check for l[l] (long/long long) */
else if (*s == 'l') {
s++; s++;
if (*s == 'l') { if (*s == 'l') {
*flags |= PRINT_FLAG_LONG_LONG; *flags |= PRINT_FLAG_LONG_LONG;
++s; ++s;
} else } else {
*flags |= PRINT_FLAG_LONG; *flags |= PRINT_FLAG_LONG;
} }
}
return s; return s;
} }
@@ -199,12 +202,14 @@ static int format_number(struct print_param *param)
width = param->vars.valuelen + param->vars.prefixlen; width = param->vars.valuelen + param->vars.prefixlen;
/* calculate additional characters for precision */ /* calculate additional characters for precision */
if ((uint32_t)(param->vars.precision) > width) if ((uint32_t)(param->vars.precision) > width) {
p = param->vars.precision - width; p = param->vars.precision - width;
}
/* calculate additional characters for width */ /* calculate additional characters for width */
if ((uint32_t)(param->vars.width) > (width + p)) if ((uint32_t)(param->vars.width) > (width + p)) {
w = param->vars.width - (width + p); w = param->vars.width - (width + p);
}
/* handle case of right justification */ /* handle case of right justification */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) {
@@ -243,21 +248,24 @@ static int format_number(struct print_param *param)
/* emit prefix (if any), return early in case of an error */ /* emit prefix (if any), return early in case of an error */
res = param->emit(PRINT_CMD_COPY, param->vars.prefix, res = param->emit(PRINT_CMD_COPY, param->vars.prefix,
param->vars.prefixlen, param->data); param->vars.prefixlen, param->data);
if ((param->vars.prefix != NULL) && (res < 0)) if ((param->vars.prefix != NULL) && (res < 0)) {
return res; return res;
}
/* insert additional 0's for precision, return early if an error /* insert additional 0's for precision, return early if an error
* occurred * occurred
*/ */
res = param->emit(PRINT_CMD_FILL, "0", p, param->data); res = param->emit(PRINT_CMD_FILL, "0", p, param->data);
if (res < 0) if (res < 0) {
return res; return res;
}
/* emit the pre-calculated result, return early in case of an error */ /* emit the pre-calculated result, return early in case of an error */
res = param->emit(PRINT_CMD_COPY, param->vars.value, res = param->emit(PRINT_CMD_COPY, param->vars.value,
param->vars.valuelen, param->data); param->vars.valuelen, param->data);
if (res < 0) if (res < 0) {
return res; return res;
}
/* handle left justification */ /* handle left justification */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
@@ -415,41 +423,47 @@ static int print_string(struct print_param *param, const char *s)
/* we need the length of the string if either width or precision is /* we need the length of the string if either width or precision is
* given * given
*/ */
if ((param->vars.precision != 0)|| (param->vars.width != 0)) if ((param->vars.precision != 0)|| (param->vars.width != 0)) {
len = strnlen_s(s, PRINT_STRING_MAX_LEN); len = strnlen_s(s, PRINT_STRING_MAX_LEN);
}
/* precision gives the max. number of characters to emit. */ /* precision gives the max. number of characters to emit. */
if ((param->vars.precision != 0) && (len > param->vars.precision)) if ((param->vars.precision != 0) && (len > param->vars.precision)) {
len = param->vars.precision; len = param->vars.precision;
}
/* calculate the number of additional characters to get the required /* calculate the number of additional characters to get the required
* width * width
*/ */
if (param->vars.width > 0 && param->vars.width > len) if (param->vars.width > 0 && param->vars.width > len) {
w = param->vars.width - len; w = param->vars.width - len;
}
/* emit additional characters for width, return early if an error /* emit additional characters for width, return early if an error
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) if (res < 0) {
return res; return res;
} }
}
/* emit the string, return early if an error occurred */ /* emit the string, return early if an error occurred */
res = param->emit(PRINT_CMD_COPY, s, len, param->data); res = param->emit(PRINT_CMD_COPY, s, len, param->data);
if (res < 0) if (res < 0) {
return res; return res;
}
/* emit additional characters on the right, return early if an error /* emit additional characters on the right, return early if an error
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) if (res < 0) {
return res; return res;
} }
}
return res; return res;
} }
@@ -479,8 +493,9 @@ int do_print(const char *fmt, struct print_param *param,
*/ */
res = param->emit(PRINT_CMD_COPY, start, fmt - start, res = param->emit(PRINT_CMD_COPY, start, fmt - start,
param->data); param->data);
if (res < 0) if (res < 0) {
return res; return res;
}
/* continue only if the '%' character was found */ /* continue only if the '%' character was found */
if (*fmt == '%') { if (*fmt == '%') {
@@ -518,9 +533,8 @@ int do_print(const char *fmt, struct print_param *param,
if (ch == '%') { if (ch == '%') {
res = param->emit(PRINT_CMD_COPY, &ch, 1, res = param->emit(PRINT_CMD_COPY, &ch, 1,
param->data); param->data);
} } else if ((ch == 'd') || (ch == 'i')) {
/* decimal number */ /* decimal number */
else if ((ch == 'd') || (ch == 'i')) {
res = print_decimal(param, res = print_decimal(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0) ?
@@ -556,8 +570,9 @@ int do_print(const char *fmt, struct print_param *param,
} }
/* hexadecimal number */ /* hexadecimal number */
else if ((ch == 'X') || (ch == 'x')) { else if ((ch == 'X') || (ch == 'x')) {
if (ch == 'X') if (ch == 'X') {
param->vars.flags |= PRINT_FLAG_UPPER; param->vars.flags |= PRINT_FLAG_UPPER;
}
res = print_pow2(param, res = print_pow2(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0) ?
@@ -572,8 +587,9 @@ int do_print(const char *fmt, struct print_param *param,
else if (ch == 's') { else if (ch == 's') {
const char *s = __builtin_va_arg(args, char *); const char *s = __builtin_va_arg(args, char *);
if (s == NULL) if (s == NULL) {
s = "(null)"; s = "(null)";
}
res = print_string(param, s); res = print_string(param, s);
} }
/* pointer argument */ /* pointer argument */
@@ -601,9 +617,10 @@ int do_print(const char *fmt, struct print_param *param,
} }
} }
/* return if an error occurred */ /* return if an error occurred */
if (res < 0) if (res < 0) {
return res; return res;
} }
}
/* done. Return the result of the last emit function call */ /* done. Return the result of the last emit function call */
return res; return res;
@@ -622,8 +639,9 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
if (cmd == PRINT_CMD_COPY) { if (cmd == PRINT_CMD_COPY) {
if (sz < 0) { if (sz < 0) {
while ((*s) != 0) { while ((*s) != 0) {
if (n < param->sz - param->wrtn) if (n < param->sz - param->wrtn) {
*p = *s; *p = *s;
}
p++; p++;
s++; s++;
n++; n++;
@@ -631,8 +649,9 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
} else if (sz > 0) { } else if (sz > 0) {
while (((*s) != 0) && n < sz) { while (((*s) != 0) && n < sz) {
if (n < param->sz - param->wrtn) if (n < param->sz - param->wrtn) {
*p = *s; *p = *s;
}
p++; p++;
s++; s++;
n++; n++;
@@ -678,14 +697,17 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args)
param.data = &snparam; param.data = &snparam;
/* execute the printf() */ /* execute the printf() */
if (do_print(fmt, &param, args) < 0) if (do_print(fmt, &param, args) < 0) {
return -1; return -1;
}
/* ensure the written string is NULL terminated */ /* ensure the written string is NULL terminated */
if (snparam.wrtn < sz) if (snparam.wrtn < sz) {
snparam.dst[snparam.wrtn] = '\0'; snparam.dst[snparam.wrtn] = '\0';
else }
else {
snparam.dst[sz - 1] = '\0'; snparam.dst[sz - 1] = '\0';
}
/* return the number of chars which would be written */ /* return the number of chars which would be written */
res = snparam.wrtn; res = snparam.wrtn;

View File

@@ -61,14 +61,18 @@ long strtol_deci(const char *nptr)
acc = 0; acc = 0;
any = 0; any = 0;
do { do {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9') {
c -= '0'; c -= '0';
else }
else {
break; break;
if (c >= base) }
if (c >= base) {
break; break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) }
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1; any = -1;
}
else { else {
any = 1; any = 1;
acc *= base; acc *= base;
@@ -79,10 +83,12 @@ long strtol_deci(const char *nptr)
s++; s++;
} while (true); } while (true);
if (any < 0) if (any < 0) {
acc = (neg != 0) ? LONG_MIN : LONG_MAX; acc = (neg != 0) ? LONG_MIN : LONG_MAX;
else if (neg != 0) }
else if (neg != 0) {
acc = -acc; acc = -acc;
}
return acc; return acc;
} }
@@ -115,18 +121,24 @@ uint64_t strtoul_hex(const char *nptr)
acc = 0; acc = 0;
any = 0; any = 0;
do { do {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9') {
c -= '0'; c -= '0';
else if (c >= 'A' && c <= 'F') }
else if (c >= 'A' && c <= 'F') {
c -= 'A' - 10; c -= 'A' - 10;
else if (c >= 'a' && c <= 'f') }
else if (c >= 'a' && c <= 'f') {
c -= 'a' - 10; c -= 'a' - 10;
else }
else {
break; break;
if (c >= base) }
if (c >= base) {
break; break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) }
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1; any = -1;
}
else { else {
any = 1; any = 1;
acc *= base; acc *= base;
@@ -137,9 +149,9 @@ uint64_t strtoul_hex(const char *nptr)
s++; s++;
} while (true); } while (true);
if (any <= 0) if (any <= 0) {
acc = ULONG_MAX; acc = ULONG_MAX;
}
return acc; return acc;
} }
@@ -191,8 +203,9 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
return NULL; return NULL;
} }
if (s == d) if (s == d) {
return d; return d;
}
overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1)); overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1));
@@ -269,8 +282,9 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
return NULL; return NULL;
} }
if (d == s) if (d == s) {
return d; return d;
}
overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1)); overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1));
@@ -290,8 +304,9 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
} }
*d = *s; *d = *s;
if (*d == '\0') if (*d == '\0') {
return dest_base; return dest_base;
}
d++; d++;
s++; s++;
@@ -337,8 +352,9 @@ size_t strnlen_s(const char *str, size_t maxlen)
count = 0U; count = 0U;
while ((*str) != 0) { while ((*str) != 0) {
if (maxlen == 0U) if (maxlen == 0U) {
break; break;
}
count++; count++;
maxlen--; maxlen--;